Providers
OAuth through your backend
Social logins such as GitHub, whose client secret must stay on a server and whose tokens your API does not accept.
Some providers cannot be used directly from an app:
- The token exchange needs a client secret. GitHub requires
client_secreton its token endpoint, even with PKCE. A secret shipped in an app is public. - Their tokens are not yours. A GitHub access token lets you call GitHub, not your API. It carries no OpenID Connect ID token, and by default it never expires.
The answer is to let your backend run the OAuth flow and hand the app your own session tokens. BackendOAuthProvider (in authyra_flutter) is the app side of that contract.
GitHub, end to end
BackendOAuthProvider(
id: 'github',
config: BackendOAuthConfig(
startUrl: Uri.parse('https://api.example.com/auth/github/start'),
exchangeEndpoint: Uri.parse('https://api.example.com/auth/exchange'),
refreshEndpoint: Uri.parse('https://api.example.com/auth/refresh'),
signOutEndpoint: Uri.parse('https://api.example.com/auth/sign-out'),
redirectUri: Uri.parse('com.example.app:/auth-callback'),
),
)
await authyra.signIn('github');
What happens:
- The app opens
startUrl?redirect_uri=...&state=...in a secure browser session. - Your backend checks
redirect_uriagainst an allowlist, remembersstate, and redirects to GitHub's consent page. - GitHub calls your backend back. The backend exchanges the code with its
client_secret, finds or creates the user, and creates a short-lived, single-use code of its own. - The backend redirects to
redirect_uri?code=<one-time code>&state=<same state>. Tokens never travel in a URL. - The app checks
state, then posts the code toexchangeEndpoint. - Your backend answers with the user and your session tokens.
From then on, the session is an ordinary session of your API: refreshed through refreshEndpoint, revoked through signOutEndpoint.
The contract your backend implements
| Endpoint | Request | Response |
|---|---|---|
startUrl | GET ?redirect_uri=...&state=... | A redirect to the provider. |
| Provider callback | (between your backend and the provider) | A redirect to redirect_uri?code=...&state=..., or ?error=...&state=.... |
exchangeEndpoint | POST {"code": "..."} | {"user": {"id": "...", "email": "..."}, "access_token": "...", "refresh_token": "...", "expires_in": 900} |
refreshEndpoint (optional) | POST {"refresh_token": "..."} | A token response like the one above without user, or 401 when the token is rejected. |
signOutEndpoint (optional) | POST {"refresh_token": "..."} | A 2xx with an empty or JSON-object body. A failure is logged; the local sign-out happens anyway. |
headers adds headers to every call (an API key, an app version), and userFromJson maps the user object to an AuthUser.
Other providers
The same pattern fits GitLab, Discord, Slack, Bitbucket, LinkedIn, or several of them behind one backend route per provider. Your backend can also link the identities to one account in your database.
GitHub's device flow needs no client secret, but it is designed for CLIs and TVs, and it still yields a GitHub token rather than a session of your API.