Skip to main content

OAuth Providers

Built-in providers: GitHub, Google, Discord, Microsoft, Apple.

Configure

from nexfetch_auth.oauth.providers.github import github
from nexfetch_auth.oauth.providers.google import google

auth = await nexfetch_auth(
secret="...",
oauth_providers=[
github(client_id="...", client_secret="..."),
google(client_id="...", client_secret="..."),
],
)

Initiate flow

POST /api/auth/sign-in/{provider}

Returns {"url": "https://provider/authorize?...", "state": "..."}. Redirect the user to url.

Callback

After the user authenticates, the provider redirects to /api/auth/callback/{provider} with code and state.

Custom provider

from typing import ClassVar
from nexfetch_auth.oauth.provider import OAuthProvider, OAuthUserInfo
import httpx

class MyProvider(OAuthProvider):
provider_id = "my-provider"
authorization_url = "https://provider.com/oauth/authorize"
token_url = "https://provider.com/oauth/token"
userinfo_url = "https://provider.com/api/me"
scopes: ClassVar[list[str]] = ["user", "email"]

async def get_user_info(self, access_token: str) -> OAuthUserInfo:
async with httpx.AsyncClient() as client:
response = await client.get(
self.userinfo_url,
headers={"Authorization": f"Bearer {access_token}"},
)
data = response.json()
return OAuthUserInfo(id=data["id"], email=data["email"], name=data["name"])