How to Add Authentication to Your Windsurf App
Windsurf-generated apps often need user authentication. Rather than using AI-generated custom auth code, this guide shows you how to integrate Auth.js (NextAuth), a battle-tested authentication library that handles all the security complexity for you.
Find security issues automatically before attackers do.
Follow These Steps
Install Auth.js
Add the authentication library to your project.
npm install next-auth@betaConfigure Auth.js with providers
Set up authentication providers in your auth configuration.
// auth.ts
import NextAuth from 'next-auth'
import Google from 'next-auth/providers/google'
import GitHub from 'next-auth/providers/github'
import Credentials from 'next-auth/providers/credentials'
export const { handlers, auth, signIn, signOut } = NextAuth({
providers: [
Google({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!
}),
GitHub({
clientId: process.env.GITHUB_CLIENT_ID!,
clientSecret: process.env.GITHUB_CLIENT_SECRET!
})
],
pages: {
signIn: '/login'
}
})Set up the API route handler
Create the route that handles auth callbacks.
// app/api/auth/[...nextauth]/route.ts
import { handlers } from '@/auth'
export const { GET, POST } = handlersAdd middleware for route protection
Protect routes automatically using Next.js middleware.
// middleware.ts
export { auth as middleware } from '@/auth'
export const config = {
matcher: ['/dashboard/:path*', '/api/:path*', '/settings/:path*']
}Set environment variables
Configure the required environment variables.
# .env.local
AUTH_SECRET=generate-a-random-32-character-string
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
GITHUB_CLIENT_ID=your-github-client-id
GITHUB_CLIENT_SECRET=your-github-client-secretGenerate AUTH_SECRET with: npx auth secret
Access session in Server Components
Use the auth() function to get the current user session.
import { auth } from '@/auth'
export default async function DashboardPage() {
const session = await auth()
if (!session) return <p>Not authenticated</p>
return <p>Welcome, {session.user?.name}</p>
}Test the authentication flow
Verify login, logout, and protected route redirection work correctly.
What You'll Achieve
Your Windsurf app has secure authentication with OAuth providers, middleware-based route protection, and proper session management. No custom auth code to maintain.
Common Mistakes to Avoid
Mistake
Building custom auth instead of using a library
Fix
Custom authentication has countless security pitfalls. Use Auth.js, Clerk, or Lucia instead of rolling your own.
Mistake
Not generating a proper AUTH_SECRET
Fix
Use npx auth secret to generate a cryptographically secure random string. Weak secrets compromise all sessions.
Frequently Asked Questions
Should I use Auth.js or a managed service like Clerk?
Auth.js is free and self-hosted but requires more configuration. Clerk and similar services provide a hosted solution with UI components. Choose Auth.js for full control, Clerk for convenience.
How do I add email/password auth with Auth.js?
Use the Credentials provider with proper password hashing (bcrypt). However, OAuth providers are recommended as they handle password security for you.
Ready to Secure Your App?
VAS automatically scans your deployed app for the security issues covered in this guide. Get actionable results in minutes.
Start Security Scan