惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
博客园 - 叶小钗
Last Week in AI
Last Week in AI
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC
P
Proofpoint News Feed
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
aimingoo的专栏
aimingoo的专栏
月光博客
月光博客
量子位
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta
Apple Machine Learning Research
Apple Machine Learning Research
Jina AI
Jina AI
博客园 - Franky
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
D
DataBreaches.Net
博客园_首页
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog

Supabase Blog

AI Agents Know About Supabase. They Don't Always Use It Right. Custom OIDC Providers for Supabase Auth 100,000 GitHub stars Supabase docs over SSH Navigating Regional Network Blocks Supabase Joins the Stripe Projects Developer Preview Log Drains: Now available on Pro Supabase Storage: major performance, security, and reliability updates Supabase incident on February 12, 2026 Hydra joins Supabase X / Twitter OAuth 2.0 is now available for Supabase Auth BKND joins Supabase Supabase is now an official Claude connector Supabase PrivateLink is now available Introducing: Postgres Best Practices When to use Read Replicas vs. bigger compute Introducing TRAE SOLO integration with Supabase Supabase Security Retro: 2025 Sync Stripe Data to Your Supabase Database in One Click Building ChatGPT Apps with Supabase Edge Functions and mcp-use Own Your Observability: Supabase Metrics API Introducing iceberg-js: A JavaScript Client for Apache Iceberg Introducing Supabase for Platforms Adding Async Streaming to Postgres Foreign Data Wrappers Build "Sign in with Your App" using Supabase Auth Introducing Seven New Email Templates for Supabase Auth The new Supabase power for Kiro Introducing Supabase ETL Introducing Analytics Buckets Introducing Vector Buckets
GitHub OAuth in your Python Flask app
Andrew Smith · 2023-11-21 · via Supabase Blog

GitHub OAuth in your Python Flask app

In this guide we'll learn how to quickly build an OAuth2.0 integration into a simple Flask app using Supabase-py. This will enable your users to login to your web app using their GitHub account.

This article assumes you are familiar with creating an application in Flask. It also assumes that you have read the Supabase documentation and are familiar the with concept of Authentication.

We'll use the following tools:

  • Flask - we used version 2.3.3 for this article
  • Supabase Dashboard - create an account if you don't have one already

To begin, inside your Flask application install the supabase library using the following command in the terminal:

Open the project in your preferred code editor and create a file called flask_storage.py with the following content:


_17

from gotrue import SyncSupportedStorage

_17

from flask import session

_17

_17

class FlaskSessionStorage(SyncSupportedStorage):

_17

def __init__(self):

_17

self.storage = session

_17

_17

def get_item(self, key: str) -> str | None:

_17

if key in self.storage:

_17

return self.storage[key]

_17

_17

def set_item(self, key: str, value: str) -> None:

_17

self.storage[key] = value

_17

_17

def remove_item(self, key: str) -> None:

_17

if key in self.storage:

_17

self.storage.pop(key, None)


In this file, we're extending the SyncSupportedStorage class from the gotrue library which comes bundled with the supabase library. Here we're telling the Supabase authentication library (gotrue) how to retrieve, store and remove a session that will store our JSON Web Token (JWT).

Create another file called supabase_client.py and in this file, we'll initiate our Supabase client.


_22

import os

_22

from flask import g

_22

from werkzeug.local import LocalProxy

_22

from supabase.client import Client, ClientOptions

_22

from flask_storage import FlaskSessionStorage

_22

_22

url = os.environ.get("SUPABASE_URL", "")

_22

key = os.environ.get("SUPABASE_KEY", "")

_22

_22

def get_supabase() -> Client:

_22

if "supabase" not in g:

_22

g.supabase = Client(

_22

url,

_22

key,

_22

options=ClientOptions(

_22

storage=FlaskSessionStorage(),

_22

flow_type="pkce"

_22

),

_22

)

_22

return g.supabase

_22

_22

supabase: Client = LocalProxy(get_supabase)


Let's focus on the get_supabase function. Here we are checking if we have an instance of the client stored in our global object g, if not we create the client and store it in the global object under the supabase name. You will notice in the ClientOptions that we are specifying the FlaskSessionStorage class we created earlier and we are also specifying a very important option that allows us to handle the OAuth flow on the server side, the flow_type="pkce".

Supabase Auth supports Sign in with GitHub on the web, native Android applications, and Chrome extensions.

For detailed set up and implementation instructions please refer to the docs.

Inside our application code app.py, we can create the sign-in route to trigger the OAuth sign-in request.


_11

@app.route("/signin/github")

_11

def signin_with_github():

_11

res = supabase.auth.sign_in_with_oauth(

_11

{

_11

"provider": "github",

_11

"options": {

_11

"redirect_to": f"{request.host_url}callback"

_11

},

_11

}

_11

)

_11

return redirect(res.url)


In this function options object we specify a redirect_to parameter which will point to the callback route we will create in the next step. This function will generate a url for us to use to redirect the user to, in this case we are using github as our OAuth provider so we will be redirected to the GitHub OAuth consent screen.

Let's add another route to our app.py file for the callback endpoint we specified in our sign in route.


_10

@app.route("/callback")

_10

def callback():

_10

code = request.args.get("code")

_10

next = request.args.get("next", "/")

_10

_10

if code:

_10

res = supabase.auth.exchange_code_for_session({"auth_code": code})

_10

_10

return redirect(next)


Here we're getting the code query parameter from the request object, if this is available we then exchange the code for a session so that the user will be signed in. Under the hood the supabase python library will handle storing this session (JWT) into a cookie and sign the user in.

In this post we explained how to setup a flask session storage to work with the Supabase python library, setting the flow_type to use Proof Key for Code Exchange (PKCE) and creating a sign in and a callback route to handle the user authentication.