Prompt
How do I build a login system that supports both local auth and SSO?
Latest observation
A good way to build this is to separate “how the user authenticates” from “who the user is in your system.”
Your app should own the user record, while local auth and SSO are just different identity providers.
1) Use one internal user model
Create a single users table/collection for your app, then link login methods to it.
Typical shape:
usersidemailnamestatuscreated_at
auth_identitiesiduser_idprovider—local,google,azuread,okta, etc.provider_subject— unique ID from the provideremail_at_loginpassword_hash— only for local authcreated_atlast_login_at
This lets one user have multiple login methods.
2) Treat local auth and SSO as separate flows
Local auth
- User signs up with email/password
- Store only a hashed password using Argon2id or bcrypt
- Verify password at login
- Issue a session or JWT after success
SSO
- Use standard protocols:
- OIDC/OAuth2 for modern identity providers
- SAML if you need enterprise compatibility
- Redirect user to provider
- Handle callback
- Verify provider response
- Map provider identity to your internal user
3) Link identities to the same account
When an SSO login comes back, you need to decide whether to:
- create a new user
- attach to an existing user
- require explicit account linking
Best practice:
- If the provider returns a verified email matching an existing account, you may offer to link after a confirmation step.
- Prefer linking by provider subject ID once established.
- Do not automatically merge accounts unless you’re confident the email is verified and policy allows it.
4) Recommended login behavior
First-time SSO login
- User clicks “Continue with Google”
- You complete OIDC flow
- Check if
(provider, provider_subject)already exists - If yes, log them in
- If no, check verified email match
- If safe, prompt to link or create account
- Create identity record and log in
Local login
- User enters email/password
- Look up local identity by email
- Verify password hash
- Log in if valid
5) Use secure session management
Prefer server-side sessions for web apps if possible.
- Store session ID in a secure, HttpOnly, SameSite cookie
- Rotate session on login
- Invalidate session on logout
- Support refresh/expiry policies
If you use JWTs:
- Keep access tokens short-lived
- Use refresh tokens carefully
- Store refresh tokens securely and rotate them
6) Add account linking safely
If a user already has a local account and wants to add SSO:
- Require they be logged in first
- Let them connect an external identity from account settings
- Store the provider link after re-authentication
If a user with SSO wants to set a password:
- Require they be logged in
- Let them set a password after verifying identity
- Mark the local auth identity as enabled
7) Handle edge cases
- Email changes at provider: rely on provider subject, not email, for identity
- Multiple SSO providers: same user can link several
- Deleted provider account: your app account may still exist if local auth or another provider is linked
- Unverified email from provider: don’t auto-link
- Duplicate emails: enforce clear linking rules
- Disabled enterprise account: revoke access if the IdP says the user is disabled, if your policy requires it
8) Security checklist
- Hash passwords with Argon2id or bcrypt
- Use PKCE for OAuth/OIDC public clients
- Validate
stateandnonce - Verify ID token signature, issuer, audience, expiration
- Protect against CSRF on auth endpoints
- Rate-limit login attempts
- Log auth events for audit
- Support MFA if possible
- Never trust email alone as proof of identity unless verified and policy-approved
9) Suggested architecture
A clean approach is:
- Auth service
- handles local login
- handles OIDC/SAML callbacks
- creates app sessions
- manages account linking
- User service/database
- stores users and identities
- App
- checks session/user roles/permissions
This keeps provider-specific code isolated.
10) Practical implementation paths
If you want to build yourself
Use:
- OIDC library for your language
- Password hashing library
- Session middleware
- Database constraints on
(provider, provider_subject)
If you want a managed solution
Consider:
- Auth0
- Okta
- Azure AD B2C / Entra External ID
- Cognito
- Clerk
- Supabase Auth
- Firebase Auth
These can reduce the complexity of SSO and account linking.
11) A simple rule of thumb
- Use local auth for users who need email/password
- Use SSO for organizations and convenience
- Always map both to one internal user
- Never use email as the primary identity key
- Use provider subject + provider name
If you want, I can sketch:
- a database schema,
- a login flow diagram, or
- example code in Node, Python, or Java.