Authentication (Clerk & SSO)
Relevant source files
The following files were used as context for generating this wiki page:
The Inbox Zero application handles user authentication and session management using a framework referred to as "Better Auth". This system integrates standard OAuth 2.0 providers like Google and Microsoft for primary user login and supports a separate Single Sign-On (SSO) flow for organizational users. The authentication architecture is responsible for user sign-in, session creation, and linking multiple identity providers to a single user account.
The core of the system is built around the /api/auth/* endpoints, which handle all authentication-related requests. The frontend interacts with these endpoints to manage the user's session state.
Sources: ARCHITECTURE.md, apps/web/app/(landing)/login/page.tsx, apps/web/app/api/sso/signin/route.test.ts
Core Authentication Framework
The application's authentication is centralized through a configuration object, likely named betterAuthConfig located in apps/web/utils/auth.ts. This configuration manages providers, database interactions, and API handlers.
Sources: apps/web/app/api/sso/signin/route.test.ts:7-13
API Endpoints
A catch-all API route at apps/web/app/api/auth/[...all]/route.ts serves all standard authentication actions (e.g., signin, signout, callback). This route delegates logic to the handlers defined in the core betterAuthConfig.
Sources: ARCHITECTURE.md
Session Management
User sessions are managed via a session cookie, identified as __Secure-better-auth.session_token. The presence of this cookie is used in the Next.js configuration to implement redirects for authenticated users, guiding them away from public pages like the landing page to the main application dashboard.
Sources: next.config.ts:39-47
OAuth Providers (Google & Microsoft)
The application supports user authentication via Google and Microsoft accounts. The setup for these providers is detailed in the project's README.md and requires generating OAuth client credentials from the respective cloud consoles.
Sources: README.md
Configuration
To enable OAuth, developers must configure several environment variables with the credentials obtained from Google Cloud and Microsoft Azure Portal.
| Variable | Description | Source |
|---|---|---|
AUTH_SECRET | A secret key for signing session tokens. | README.md |
GOOGLE_CLIENT_ID | The Google OAuth 2.0 Client ID. | README.md |
GOOGLE_CLIENT_SECRET | The Google OAuth 2.0 Client Secret. | README.md |
MICROSOFT_CLIENT_ID | The Microsoft Application (client) ID. | README.md |
MICROSOFT_CLIENT_SECRET | The Microsoft client secret value. | README.md |
Sources: README.md
The required API scopes for Google include userinfo.profile, userinfo.email, gmail.modify, gmail.settings.basic, and contacts. For Microsoft, permissions like Mail.ReadWrite, Mail.Send, and User.Read are necessary.
Sources: README.md
Standard OAuth Login Flow
The following diagram illustrates the sequence of events during a typical login with an OAuth provider like Google.
This diagram represents a standard OAuth 2.0 flow inferred from the application's use of OAuth providers.
Sources: README.md, apps/web/app/(landing)/login/page.tsx
Single Sign-On (SSO)
The application provides an SSO flow for users belonging to a specific organization. This allows them to authenticate through their organization's Identity Provider (IdP).
SSO Initiation
The SSO process is initiated via a GET request to the /api/sso/signin endpoint.
| Property | Value |
|---|---|
| Endpoint | GET /api/sso/signin |
| Description | Initiates an SSO sign-in flow for a given organization. |
| Query Parameters | email (string, required), organizationSlug (string, required) |
| Success Response | 200 OK with JSON { "redirectUrl": "...", "providerId": "..." } |
| Error Response | 400 Bad Request if parameters are missing or org not found. |
Sources: apps/web/app/api/sso/signin/route.test.ts
The endpoint logic performs the following steps:
- Validates the presence of
emailandorganizationSlugquery parameters. - Queries the database to find an
ssoProviderassociated with theorganizationSlug. - If a provider is found, it calls the
betterAuthConfig.api.signInSSOfunction with theproviderId. - This function generates a redirect URL to the IdP.
- The endpoint returns the
redirectUrlandproviderIdin a JSON response.
Sources: apps/web/app/api/sso/signin/route.test.ts:74-98
SSO Sign-in Flow Diagram
This diagram shows the data flow for initiating an SSO login.
This flow is derived from the logic tested in apps/web/app/api/sso/signin/route.test.ts.
SSO Configuration Parsing
The system includes a utility for parsing SSO provider configurations from XML metadata, located at apps/web/utils/sso/extract-sso-provider-config-from-xml.ts. This suggests that the application can be configured with IdP metadata, likely for SAML-based SSO.
Sources: (File path mentioned in prompt)
Login UI & Error Handling
The primary login interface is rendered by the AuthenticationPage component.
Sources: apps/web/app/(landing)/login/page.tsx
Session Checking
On page load, the component calls the auth() utility function to check for an active session. If a user is already authenticated, they are redirected to the welcome page (/welcome) or a specified next URL.
Sources: apps/web/app/(landing)/login/page.tsx:23-29
Error Display
The login page can display authentication-related errors based on query parameters. For example, if the error parameter is OAuthAccountNotLinked, it informs the user that the email is already associated with another account and provides an option to merge accounts.