NextAuth Configuration
The authentication system is configured insrc/server/auth.ts and uses the Prisma adapter to store user and account data.
Authentication Provider
Uxie currently supports Google OAuth for user authentication:Session Strategy
Uxie uses JWT sessions for stateless authentication:JWT sessions are stored in HTTP-only cookies and don’t require database lookups on every request, making them ideal for serverless deployments.
Google OAuth Setup
1. Create Google OAuth Credentials
- Go to Google Cloud Console
- Create a new project or select an existing one
- Navigate to APIs & Services > Credentials
- Click Create Credentials > OAuth client ID
- Configure the OAuth consent screen if prompted:
- User type: External
- App name: Your app name (e.g., “Uxie”)
- User support email: Your email
- Authorized domains: Your domain
2. Configure OAuth Client
- Application type: Web application
- Name: “Uxie Web Client” (or your preferred name)
-
Authorized JavaScript origins:
- Development:
http://localhost:3000 - Production:
https://yourdomain.com
- Development:
-
Authorized redirect URIs:
- Development:
http://localhost:3000/api/auth/callback/google - Production:
https://yourdomain.com/api/auth/callback/google
- Development:
- Click Create
- Copy your Client ID and Client Secret
-
Add them to your
.envfile:
Session Management
Session Callbacks
Uxie extends the default session object to include additional user data:JWT Callback
The JWT callback fetches user data from the database and includes it in the token:The user’s subscription plan (
FREE, FREE_PLUS, or PRO) is included in the session for easy access throughout the application.User Roles and Plans
Subscription Plans
Uxie supports three subscription tiers defined in the Prisma schema:Plan Features
Plan Features
Each plan has different limits and features:
- FREE: Basic document access with limited uploads
- FREE_PLUS: Increased document limits
- PRO: Maximum document limits and advanced features
src/lib/constants.ts and enforced in the upload middleware (see src/server/uploadthing.ts:26).Document Collaboration Roles
For document sharing, Uxie uses a separate role system:A user can have different roles for different documents. The
OWNER role is automatically assigned to the document creator.Custom Sign-In Page
Uxie uses a custom sign-in page instead of the default NextAuth page:src/pages/login.tsx or src/app/login/page.tsx depending on your Next.js routing strategy.
Usage in Your Application
Server-Side Session
Get the session in API routes orgetServerSideProps:
Client-Side Session
Use theuseSession hook from NextAuth:
Sign In / Sign Out
Protected Routes
API Route Protection
Protect API routes by checking for a valid session:Middleware Protection (App Router)
For Next.js App Router, create amiddleware.ts file:
Environment Variables
Make sure these environment variables are set:Secret for encrypting tokens and session data. Generate with:
Canonical URL of your site:
- Development:
http://localhost:3000 - Production:
https://yourdomain.com
OAuth 2.0 Client ID from Google Cloud Console
OAuth 2.0 Client Secret from Google Cloud Console
Troubleshooting
Redirect URI mismatch error
Redirect URI mismatch error
If you see “redirect_uri_mismatch” error:
-
Check your Google OAuth redirect URIs exactly match:
http://localhost:3000/api/auth/callback/google(development)https://yourdomain.com/api/auth/callback/google(production)
- Ensure no trailing slashes
- Wait a few minutes after updating Google Console settings
Session not persisting
Session not persisting
If sessions aren’t persisting:
- Verify
NEXTAUTH_SECRETis set and consistent - Check cookies are enabled in browser
- Ensure
NEXTAUTH_URLmatches your actual domain - Clear cookies and try again
Database connection issues
Database connection issues
If authentication fails with database errors:
- Verify
DATABASE_URLis correct - Run Prisma migrations:
npx prisma migrate dev - Check
AccountandUsertables exist - Verify Prisma Client is generated:
npx prisma generate
Adding More Providers
To add additional authentication providers (GitHub, Discord, etc.):- Install the provider package if needed
- Add provider configuration:
- Add environment variables
- Update Prisma schema if provider requires additional fields
- Configure OAuth app in provider’s developer console
