Prompt
How do I centralize login across web, mobile, and APIs?
Latest observation
To centralize login across web, mobile, and APIs, use a single Identity Provider (IdP) and standard auth protocols, usually OpenID Connect (OIDC) on top of OAuth 2.0.
Recommended architecture
1) Put authentication in one place
Use a centralized IdP such as:
- Auth0
- Okta
- Azure AD / Entra ID
- Keycloak
- Cognito
- Ping Identity
This system handles:
- User login
- MFA
- Password resets
- Social login / SSO
- Session management
- Token issuance
2) Use OIDC for user login
OIDC gives you:
- ID token: who the user is
- Access token: what APIs they can call
- Refresh token: get new tokens without re-login
3) Use one login flow per client type
- Web app: Authorization Code Flow + PKCE
- Mobile app: Authorization Code Flow + PKCE using the system browser
- API clients / machine-to-machine: Client Credentials Flow
- Server-to-server / background jobs: Client Credentials or token exchange
How each platform should work
Web
- User clicks “Sign in”
- Redirect to IdP
- User authenticates
- IdP redirects back with an auth code
- Your backend exchanges code for tokens
- Backend creates its own app session cookie, or stores tokens securely
Best practice: use an HttpOnly, Secure, SameSite cookie for the browser session rather than storing tokens in localStorage.
Mobile
- App opens system browser to IdP
- User signs in
- IdP returns via deep link / app link
- App exchanges auth code for tokens
- Store refresh token in secure storage:
- iOS Keychain
- Android Keystore
Do not use embedded webviews for login unless you have a strong reason.
APIs
Protect APIs with bearer access tokens:
- Validate JWTs locally, or
- Introspect opaque tokens with the IdP
APIs should check:
- token signature
- issuer
- audience
- expiration
- scopes/roles/claims
Centralization patterns
Pattern A: Direct-to-IdP for every client
Each client talks directly to the IdP.
Pros
- Clean architecture
- Standard
- Less custom code
Cons
- Each app must implement OIDC correctly
Pattern B: Backend-for-Frontend (BFF)
Use a small backend for each UI channel:
- Web BFF
- Mobile BFF
The BFF handles tokens, and the client only gets a session cookie or app-specific session.
Pros
- Better security for browser apps
- Tokens stay server-side
- Easier API access control
Cons
- More backend complexity
For many organizations, BFF + centralized IdP is the best setup.
Key design decisions
Use a single source of truth for identity
Store users in one place, or federate to one place:
- Internal directory
- Social IdPs
- Enterprise SSO via SAML/OIDC
Separate authentication from authorization
- Authentication = who the user is
- Authorization = what they can do
Use:
- scopes for API permissions
- roles/claims for app-level permissions
- groups if needed
Standardize token validation
All APIs should validate tokens consistently:
- same issuer
- same signing keys / JWKS endpoint
- same claim model
- same audience conventions
Use SSO across apps
If web, mobile, and APIs all trust the same IdP, users sign in once and can access multiple apps without re-entering credentials.
Security best practices
- Use Authorization Code + PKCE for web/mobile
- Never put secrets in mobile apps or SPAs
- Use short-lived access tokens
- Use refresh token rotation
- Store refresh tokens securely
- Prefer HttpOnly cookies for browser sessions
- Use MFA for sensitive users/actions
- Support logout/revocation at the IdP
- Protect APIs with scopes and audience checks
- Use HTTPS everywhere
Example flow
A user logs into the mobile app:
- Mobile app redirects to IdP
- User logs in
- IdP issues tokens
- Mobile app calls API with access token
- API validates token and returns data
- Web app later uses the same IdP, so the user gets SSO without a separate password
If you want the simplest robust setup
- IdP: Keycloak/Auth0/Okta/Entra
- Protocol: OIDC + OAuth2
- Web: Authorization Code + PKCE, session cookie via BFF
- Mobile: Authorization Code + PKCE, system browser
- APIs: JWT validation with scopes/audience
- M2M: Client Credentials
If you want, I can also provide:
- a reference architecture diagram,
- a step-by-step implementation plan, or
- examples for a specific stack like React + Node + iOS + Android.