Authentication Done Right: A Complete Guide for Vibe Coders
Session tokens vs JWTs, OAuth vs magic links — here's how to implement auth properly when AI is writing your code.
Authentication is where most vibe-coded apps are most vulnerable. AI can scaffold a login flow in minutes, but the defaults are almost never production-ready. Here's what you need to know.
The Problem
When you tell AI to "add authentication," it typically generates:
This works for demos. For production, it's dangerous. The token isn't validated properly, it persists forever, there's no protection against theft, and localStorage is accessible to any script running on your page.
Option 1: Use an Auth Library (Recommended)
The safest approach is to not build auth yourself. Use a battle-tested library:
Next.js / React
The Prompt
"Add authentication using NextAuth.js with Google and GitHub OAuth providers and email magic links. Store sessions in the database using Prisma adapter. Protect all /dashboard and /api routes that require authentication. Add a sign-in page at /login and handle the full sign-in, sign-out, and session refresh flow."
Option 2: OAuth (Social Login)
If you want "Sign in with Google" or "Sign in with GitHub," you need OAuth. The key rules:
The Prompt
"Implement Google OAuth sign-in using the Authorization Code flow with PKCE. On successful login, create or update a user record in the database with their Google ID, name, email, and profile picture. Create a secure HTTP-only session cookie. Never expose OAuth tokens to the client."
Option 3: Magic Links (Email-Based)
Magic links avoid passwords entirely. The user enters their email, receives a link, and clicking it logs them in. This is surprisingly secure because:
The Prompt
"Implement email magic link authentication. When a user enters their email: generate a cryptographically random token, store it in the database with the user's email and a 15-minute expiry, send an email with a link containing the token. When the link is clicked: validate the token exists and isn't expired, create a session, delete the token. Make tokens single-use."
Session Management Rules
Regardless of which auth method you use, follow these session rules:
Cookie Configuration
The Prompt
"Configure session cookies with HttpOnly, Secure, and SameSite=Lax flags. Set session duration to 24 hours. Implement session rotation on privilege changes (login, password change). Add a 'remember me' option that extends the session to 30 days. Invalidate all sessions when the user changes their password."
Authorization (The Other Half)
Authentication tells you who someone is. Authorization tells you what they can do. AI often gets authentication right but forgets authorization.
Common Mistakes
The Prompt
"Audit every API route for proper authorization. For routes that modify resources (PUT, PATCH, DELETE): verify the authenticated user owns the resource or is an admin. For routes that read resources: ensure they only return data the authenticated user is allowed to see. For admin routes: check the user's role in the database, not just in the session."
Password Security (If You Must)
If you're implementing email/password auth:
The Prompt
"Implement secure password handling: hash passwords with bcrypt cost factor 12 on registration, compare hashes on login, implement account lockout after 5 failed attempts with 15-minute cooldown, add password reset with a cryptographically random token that expires in 1 hour, and ensure login error messages say 'Invalid email or password' without revealing which was wrong."
Test Your Auth
After implementing, test these scenarios:
These tests take 5 minutes and catch the most common auth bugs in vibe-coded apps.
More security resources
Browse all our security guides, checklists, and copy-paste prompts in the Guides hub.
All Guides →Have a vibe-coded app?
List it on VibbleLaunch for free and get discovered by thousands of makers and users.
List Your App