OAuth Authentication Setup
Complete guide to the Google OAuth implementation in this application
Authentication Architecture
How OAuth is implemented in this app
Technology Stack
- Auth.js (NextAuth v5) - Modern authentication library for Next.js
- Google OAuth 2.0 - Secure authentication provider
- Prisma Adapter - Database session management
- PostgreSQL (Neon) - User and session storage
Authentication Flow
- User clicks "Sign in with Google"
- Redirected to Google OAuth consent screen
- User authorizes the application
- Google returns authorization code
- Auth.js exchanges code for access token
- User profile is fetched and stored in database
- Session is created and user is authenticated
Configuration Details
Environment variables and setup requirements
Required Environment Variables
AUTH_SECRET=your-secret-key
GOOGLE_CLIENT_ID=your-client-id
GOOGLE_CLIENT_SECRET=your-client-secret
DATABASE_URL=your-database-connection
All credentials are securely stored in environment variables and never committed to version control.
Google Cloud Console Setup
- Create project in Google Cloud Console
- Enable Google OAuth API
- Configure OAuth consent screen
- Create OAuth 2.0 credentials (Web application)
- Add authorized redirect URIs:
http://localhost:3000/api/auth/callback/googlehttps://your-domain.com/api/auth/callback/google
Database Schema
Auth.js tables managed by Prisma
User Table
Stores user profile information (id, name, email, image)
Account Table
OAuth provider details and tokens (Google OAuth data)
Session Table
Active user sessions with expiration timestamps
VerificationToken Table
Email verification tokens (if email auth is added)
Implementation Files
Key files in the authentication system
auth.tsAuth.js configuration and exportsmiddleware.tsRoute protection and redirectsapp/api/auth/[...nextauth]/route.tsAPI handlerapp/auth/signin/page.tsxSign-in pageapp/components/user-menu.tsxUser dropdown menuprisma/schema.prismaDatabase models