This guide provides a comprehensive overview of how authentication is implemented and managed within the Agora platform. Agora uses a centralized authentication system based on JSON Web Tokens (JWT) stored in secure, HttpOnly cookies.
Agora utilizes a stateless authentication strategy using JWT (JSON Web Tokens).
- Cookie Name:
auth_token - Storage: HttpOnly, Secure (in production), SameSite: Lax.
- Expiration: 7 days.
- Signing: Tokens are signed using a server-side
JWT_SECRET.
When a user authenticates via any of the supported flows, a JWT is generated containing the user's identification (email and/or subject ID) and set as a cookie. Subsequent requests to the frontend API routes or the backend (via proxy) use this cookie to validate the user's session.
The email flow is currently implemented as a passwordless login system.
Step-by-step:
- Initiation: The user enters their email address on the
/authpage. - Request: The frontend sends a
POSTrequest to/api/auth/emailwith the email in the request body. - Validation: The API validates the email format.
- Token Generation: A JWT is signed containing the email address.
- Cookie Injection: The JWT is set as an
HttpOnlycookie namedauth_token. - Completion: The API returns a success response, and the frontend redirects the user to the
/homepage.
Note
This flow currently serves as a foundation for a future "Magic Link" implementation. In the current development state, it provides immediate access upon entering a valid email.
Agora supports authentication via Google OAuth 2.0.
Step-by-step:
- Initiation: The user clicks the "Sign in with Google" button.
- Redirect to Provider: The application redirects the user to Google's OAuth consent screen (via
/api/auth/google). - User Authorization: The user grants permission to Agora.
- Callback: Google redirects the user back to Agora at
/api/auth/google?code=.... - Code Exchange: The server-side API route exchanges the
codefor anid_tokenusing the Google OAuth API. - User Identification: The
id_tokenis decoded to extract the user's email and unique subject ID (sub). - Session Creation: A JWT is signed for the Agora session and set as the
auth_tokencookie. - Redirection: The user is redirected to the
/homepage.
The Apple OAuth flow follows the "Sign in with Apple" protocol.
Step-by-step:
- Initiation: The user clicks the "Sign in with Apple" button.
- Redirect to Provider: The application redirects the user to Apple's authorization server.
- User Authorization: The user authorizes the request (potentially using FaceID/TouchID).
- Callback (GET/POST): Apple redirects back to
/api/auth/apple.- If
response_modeisquery, it's aGETrequest. - If
response_modeisform_post(used when requesting scopes like email/name), it's aPOSTrequest.
- If
- Code Exchange: Agora exchanges the authorization
codefor anid_tokenfrom Apple. - User Identification: The
id_tokenis decoded. If the email is missing (common in subsequent logins), the subject ID (sub) is used to identify the user. - Session Creation: The session JWT is generated and set as the
auth_tokencookie. - Redirection: The user is redirected to the
/homepage.
Session validation is performed on the server-side by checking the presence and validity of the auth_token cookie.
- Frontend (Next.js): The utility function
getAuthFromRequestinapps/web/lib/auth.tsextracts and verifies the JWT from the request cookies. - Token Payload:
{ "email": "user@example.com", "sub": "unique-provider-id", "iat": 1234567890, "exp": 1234567890 }
To test authentication locally, ensure you have the following environment variables configured in your apps/web/.env.local (or equivalent):
JWT_SECRET=your_jwt_secret_here
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
GOOGLE_REDIRECT_URI=http://localhost:3000/api/auth/google
APPLE_CLIENT_ID=your_apple_client_id
APPLE_CLIENT_SECRET=your_apple_client_secret
APPLE_REDIRECT_URI=http://localhost:3000/api/auth/apple
- Start the development server:
pnpm dev. - Navigate to
http://localhost:3000/auth. - Enter any valid email (e.g.,
test@example.com). - You should be redirected to
/homeand see theauth_tokencookie set in your browser's dev tools.
For local development without real OAuth credentials:
- You can manually set an
auth_tokencookie with a valid JWT signed by your localJWT_SECRET. - Alternatively, use the Email flow which provides a similar session token.
| Error Message | Cause | Resolution |
|---|---|---|
Invalid email format |
The provided email does not match the standard email regex. | Enter a valid email address. |
Missing code |
The OAuth callback was triggered without an authorization code. | Ensure the OAuth flow is initiated from the official buttons. |
Google/Apple OAuth failed |
The provider rejected the code exchange request. | Check your Client ID, Secret, and Redirect URI configuration. |
Invalid ID token |
The token returned by the provider could not be decoded or verified. | Ensure the provider's public keys are reachable or the token hasn't expired. |
Internal server error |
An unexpected error occurred during JWT signing or cookie setting. | Check server logs for stack traces and environment variable status. |