Prompt
How to add OAuth login to app
Latest observation
Oct 6, 2026
OpenAI APIWeb search: off
Adding OAuth login usually means letting users sign in with an external identity provider like Google, GitHub, Microsoft, or Facebook.
Here’s the typical approach:
1) Pick an OAuth/OpenID provider
Common choices:
- GitHub
- Microsoft
- Apple
- Auth0 / Okta / Firebase Auth (hosted identity platforms)
If you’re doing “login,” you usually want OpenID Connect (OIDC) on top of OAuth 2.0, because OAuth alone is for authorization, while OIDC gives you identity/login.
2) Register your app with the provider
You’ll get:
- Client ID
- Client Secret (for backend apps)
- Redirect URI / Callback URL
Example redirect URI:
https://yourapp.com/auth/callback- or
http://localhost:3000/auth/callbackfor local dev
3) Use the Authorization Code flow
This is the standard secure flow for web/mobile apps:
- User clicks “Sign in with Google”
- Your app redirects them to provider’s authorization URL
- User logs in and consents
- Provider redirects back to your callback URL with a
code - Your backend exchanges the
codefor tokens - You use the user info / ID token to create a session in your app
4) Store users in your database
Typically save:
- provider name
- provider user ID
- name
- avatar
- local app user ID
This lets you link external accounts to your own user system.
5) Create a session or JWT
After successful login, your app should issue:
- a server session cookie, or
- a JWT access token
For most web apps, secure HTTP-only cookies are a good choice.
Example flow
Frontend button
<a href="/auth/google">Sign in with Google</a>
Backend route
Redirect user to provider:
app.get('/auth/google', (req, res) => {
const url = buildGoogleAuthUrl();
res.redirect(url);
});
Callback route
Handle provider response:
app.get('/auth/callback', async (req, res) => {
const { code } = req.query;
const tokens = await exchangeCodeForTokens(code);
const userInfo = await getUserInfo(tokens.access_token);
const user = await findOrCreateUser(userInfo);
createSession(res, user);
res.redirect('/dashboard');
});
Security tips
- Always use HTTPS
- Use
stateparameter to prevent CSRF - Use PKCE for public clients like mobile apps/SPAs
- Don’t expose client secret in frontend code
- Verify ID tokens properly
- Restrict redirect URIs exactly
Best practices by app type
Web app
- Backend handles OAuth code exchange
- Use server session cookies
SPA
- Use Authorization Code + PKCE
- Avoid storing long-lived tokens in localStorage if possible
Mobile app
- Use system browser / app auth flow
- Use PKCE
- Deep link/callback handling
Easiest implementation options
If you want to avoid building OAuth manually, use:
- NextAuth.js for Next.js apps
- Passport.js for Node.js
- Auth0
- Firebase Authentication
- Supabase Auth
- Clerk
If you want, I can show you:
- how to do this in Node/Express,
- React + backend,
- Next.js, or
- a specific provider like Google login.