Prompt
How do I authenticate users in a mobile app?
Latest observation
A common, secure way to authenticate users in a mobile app is to not handle passwords directly in the app if you can avoid it, and instead use a standard auth flow such as OAuth 2.0 / OpenID Connect through a trusted identity provider.
Typical approach
1. Use a backend or auth provider
Examples:
- Auth0
- Firebase Authentication
- AWS Cognito
- Supabase Auth
- Your own backend with OAuth/OpenID Connect
2. Sign in via a secure flow
For mobile apps, the recommended pattern is:
- Open the provider’s system browser / web auth session
- User logs in there
- The provider redirects back to your app using a custom URL scheme or universal link
- Your app receives an authorization code
- Exchange that code for tokens
This is usually the Authorization Code Flow with PKCE, which is the standard for mobile apps.
3. Store tokens securely
After login:
- Store the access token / refresh token in secure storage
- iOS: Keychain
- Android: Keystore / EncryptedSharedPreferences
- Cross-platform: use secure storage plugins
- Avoid storing tokens in plain text or shared preferences
4. Use tokens for API calls
Your app sends the access token in requests:
Authorization: Bearer <access_token>
Your backend verifies the token before allowing access.
If you build your own auth
If you’re not using a third-party provider, you generally need:
- A login endpoint
- Password hashing on the server with a strong algorithm like bcrypt, Argon2, or scrypt
- Secure session/token issuance
- Token refresh and revocation
- Rate limiting, brute-force protection, and MFA if possible
Best practices
- Always use HTTPS
- Prefer short-lived access tokens
- Use refresh tokens carefully and securely
- Support multi-factor authentication
- Never hardcode secrets in the mobile app
- Don’t use embedded client secrets in mobile apps
- Validate tokens server-side
- Consider biometric unlock only as a convenience layer, not the primary auth method
Recommended pattern in one sentence
For most mobile apps: use OAuth 2.0 / OpenID Connect with PKCE, authenticate via the browser, store tokens in secure storage, and call your API with bearer tokens.
If you want, I can also show:
- a Flutter example,
- a React Native example, or
- a native iOS/Android flow.