This guide will help you set up Google OAuth authentication for StackIt.
Create a .env.local file in the root directory with the following variables:
# NextAuth Configuration
NEXTAUTH_URL=http://localhost:3000
NEXTAUTH_SECRET=your-nextauth-secret-key-here
# Google OAuth Configuration
GOOGLE_CLIENT_ID=your-google-client-id-here
GOOGLE_CLIENT_SECRET=your-google-client-secret-here
# Database Configuration
DATABASE_URL="postgresql://username:password@localhost:5432/stackit"- Go to Google Cloud Console
- Create a new project or select an existing one
- Enable the Google+ API
- Go to "APIs & Services" > "Credentials"
- Click "Create Credentials" > "OAuth 2.0 Client IDs"
- Choose "Web application"
- Add authorized redirect URIs:
http://localhost:3000/api/auth/callback/google(for development)https://yourdomain.com/api/auth/callback/google(for production)
- Copy the Client ID and Client Secret
Replace the placeholder values in .env.local with your actual Google OAuth credentials.
- Install PostgreSQL on your system
- Create a new database named
stackit
# Install dependencies
bun install
# Generate Prisma client
bunx prisma generate
# Run database migrations
bunx prisma db push
# (Optional) View your database
bunx prisma studioGenerate a secure random string for NEXTAUTH_SECRET:
# On macOS/Linux
openssl rand -base64 32
# On Windows PowerShell
[System.Web.Security.Membership]::GeneratePassword(32, 10)bun dev✅ Google OAuth Authentication
- Users can sign in with their Google account
- Automatic account creation on first sign-in
- Secure session management
✅ Protected Routes
/ask-a-questionrequires authentication- Unauthenticated users are redirected to sign-in page
- Guest users can still browse questions at
/browse
✅ User Interface
- Navigation bar with authentication status
- Sign in/out buttons
- User profile display
- Responsive design
✅ Database Integration
- Prisma schema with NextAuth.js tables
- User accounts and sessions storage
- PostgreSQL database support
- JWT-based session management
- Secure OAuth 2.0 flow
- Environment variable protection
- CSRF protection via NextAuth.js
- Automatic session validation
- Set up your Google OAuth credentials
- Configure your database connection
- Update the environment variables
- Run the database migrations
- Start the development server
The authentication system is now fully functional with Google OAuth integration!