Overview
The platform uses Google OAuth 2.0 for authentication with extended permissions to access Google Calendar Events. This enables integration with Google Calendar for scheduling virtual classes.OAuth Configuration
The Google provider is configured insrc/auth.ts:16 with custom authorization parameters:
OAuth Scopes
The following OAuth 2.0 scopes are requested during authentication:Required for OpenID Connect authentication
Access to user’s email address
Access to user’s basic profile information (name, picture)
Full access to Google Calendar events - allows creating, reading, updating, and deleting calendar events
Authorization Parameters
Request offline access to receive a refresh token. This allows the application to access Google APIs when the user is not present.
Force consent screen to appear (commented out in production). When enabled, users see the consent screen every time.
Environment Setup
Required Variables
Your Google OAuth 2.0 Client ID obtained from Google Cloud Console
Your Google OAuth 2.0 Client Secret obtained from Google Cloud Console
Obtaining Credentials
- Go to Google Cloud Console
- Create a new project or select an existing one
- Enable the Google Calendar API
- Navigate to APIs & Services > Credentials
- Click Create Credentials > OAuth 2.0 Client ID
- Configure the OAuth consent screen
- Add authorized redirect URIs:
http://localhost:3000/api/auth/callback/google(development)https://yourdomain.com/api/auth/callback/google(production)
- Copy the Client ID and Client Secret to your
.envfile
Sign In Flow
Initiate Sign In
Callback Handling
When Google redirects back to the application, thesignIn callback is triggered (src/auth.ts:35):
Callback Parameters
User information from Google OAuth
OAuth account information
Full OAuth profile from Google
Access Token Handling
Thejwt callback processes the OAuth tokens and stores them in the JWT (src/auth.ts:55):
Token Structure
User’s database ID
Issued at timestamp (Unix timestamp)
Expiration timestamp (Unix timestamp) - 30 days from issuance
Google OAuth access token for API calls
Google OAuth refresh token (admin users only)
Refresh Token Management
Refresh tokens are only stored for admin users and persisted to the database:Extract Refresh Token
Get the refresh token from the account object (only available with
access_type: "offline")Using the Access Token
Accessing Google Calendar API
Error Handling
The sign-in callback includes error handling (src/auth.ts:50):
false from the callback will:
- Prevent the user from signing in
- Redirect to the error page (configured as
/) - Not create a session
Security Considerations
Access Token Storage
Access Token Storage
Access tokens are stored in the encrypted JWT and never exposed to the client-side code. They’re only available in server components and API routes.
Refresh Token Limitation
Refresh Token Limitation
Refresh tokens are only stored for admin users to minimize security exposure. Regular users must re-authenticate after token expiration.
Offline Access
Offline Access
The
access_type: "offline" parameter ensures the application can access Google APIs even when the user is not actively using the application.Scope Minimization
Scope Minimization
Only request the minimum scopes necessary. The current implementation requests Calendar Events access for class scheduling functionality.
Testing
Check OAuth Configuration
Verify Session After Sign In
Common Issues
Refresh token not provided
Refresh token not provided
Solution: Ensure
access_type: "offline" is set and the user consents to offline access. Google only provides refresh tokens on the first authorization or when forcing consent with prompt: "consent".Invalid redirect URI
Invalid redirect URI
Solution: Verify the redirect URI in Google Cloud Console matches exactly with your callback URL, including protocol (http/https) and port.
Calendar API access denied
Calendar API access denied
Solution: Ensure the Google Calendar API is enabled in your Google Cloud Console project and the correct scope is requested.
Next Steps
Session Management
Learn about JWT tokens and session handling
Authentication Overview
Back to authentication overview