Implement OAuth2 / Social Login
Build a complete OAuth2 social login flow with token handling, refresh logic, and account linking.
The Prompt
Implement OAuth2 authentication for the following provider. Include: 1. Authorization URL generation with PKCE (state + code_verifier) 2. Callback handler — exchange code for tokens 3. Token storage (access token in memory, refresh token in httpOnly cookie) 4. Automatic token refresh before expiry 5. Account linking — what happens if the OAuth email matches an existing account? 6. Error handling for denied permission or expired state Provider: [GOOGLE / GITHUB / DISCORD — specify] Framework: [NEXTJS APP ROUTER / EXPRESS / FASTAPI] Database: [PRISMA/POSTGRES for storing OAuth accounts] Do NOT use a library like NextAuth — implement it manually so I understand the flow.
Example Output
Implemented Google OAuth2 with PKCE in Next.js Route Handlers: /api/auth/google generates the authorization URL with a cryptographically random state stored in a session cookie; /api/auth/callback validates state, exchanges the code, fetches the user profile, and upserts a linked account in the database.
FAQ
Which AI model is best for Implement OAuth2 / Social Login?
Claude Sonnet 4 — thorough about security details like PKCE, state validation, and token storage.
How do I use the Implement OAuth2 / Social Login prompt?
Copy the prompt, replace the [BRACKETED] placeholders with your specific information, and paste into your preferred AI assistant (ChatGPT, Claude, Gemini, etc.). Implemented Google OAuth2 with PKCE in Next.js Route Handlers: /api/auth/google generates the authorization URL with a cryptographically random state stored in a session cookie; /api/auth/callback validates state, exchanges the code, fetches the user profile, and upserts a linked account in the database.
Model Recommendation
Claude Sonnet 4 — thorough about security details like PKCE, state validation, and token storage.