Authentication Flow
Relevant source files
The following files were used as context for generating this wiki page:
The authentication system in Analog is built upon the better-auth library, providing a comprehensive solution for user management, session handling, and OAuth-based social sign-in. It is configured to support multiple providers, including Google, Microsoft, and Zoom, and features account linking to allow users to connect different social accounts to a single primary user profile.
The flow is tightly integrated with the Next.js application framework, using API routes to handle OAuth callbacks and server components to manage session state. User and account data is persisted in a PostgreSQL database using the Drizzle ORM, with a schema designed to support the relationships between users, their sessions, and their linked provider accounts.
Core Configuration
The central configuration for the authentication system resides in packages/auth/src/server.ts. It initializes better-auth with a Drizzle adapter, defines social providers, and configures account and user behavior.
Sources: packages/auth/src/server.ts
betterAuth Initialization
The auth object is exported from server.ts and encapsulates the entire authentication logic.
// packages/auth/src/server.ts:36-93
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
}),
account: {
accountLinking: {
enabled: true,
allowDifferentEmails: true,
trustedProviders: ["google", "microsoft", "zoom"],
},
},
user: {
// ...
},
hooks: {
after: handleUnlinkAccount,
},
databaseHooks: {
account: {
create: {
after: createProviderHandler,
},
},
},
socialProviders: {
// ...
},
plugins: [
apiKey({
enableMetadata: true,
}),
mcp({
loginPage: "/login",
}),
],
});
Key configuration options are summarized below:
| Option | Description | Source |
|---|---|---|
database | Configures the drizzleAdapter to connect better-auth with the PostgreSQL database. | packages/auth/src/server.ts:37-39 |
account.accountLinking | Enables linking multiple provider accounts to one user. It allows linking accounts with different emails and specifies google, microsoft, and zoom as trusted providers. | packages/auth/src/server.ts:41-46 |
hooks & databaseHooks | Registers functions to be executed at specific lifecycle events, such as after an account is created (createProviderHandler) or unlinked (handleUnlinkAccount). | packages/auth/src/server.ts:60-68 |
socialProviders | Defines the configuration for each OAuth provider, including client IDs, secrets, and required scopes. | packages/auth/src/server.ts:69-90 |
plugins | Enables additional functionality. apiKey adds API key support, and mcp (Multi-page/Component Plugin) configures the login page route to /login. | packages/auth/src/server.ts:91-97 |
Social Providers and Scopes
Analog is configured to support Google, Microsoft, and Zoom for OAuth-based authentication. Each provider has a specific set of scopes requested during the consent process to access user data and calendar functionalities.
Sources: packages/auth/src/server.ts:11-34, 69-90
| Provider | Scopes | Configuration Details |
|---|---|---|
email, profile, openid, https://www.googleapis.com/auth/calendar | accessType: "offline", prompt: "consent" to ensure a refresh token is issued. | |
| Microsoft | User.Read, Calendars.Read, Calendars.ReadWrite, offline_access, and others. | Requests a broad set of calendar permissions. |
| Zoom | user:read, calendar:read, calendar:write, meeting:read, meeting:write, offline_access | Requests permissions for user info, calendar, and meeting management. |
Environment Setup
To enable the OAuth providers, specific environment variables must be set. These include client IDs and secrets obtained from the respective developer consoles.
Sources: README.md
| Variable | Description |
|---|---|
BETTER_AUTH_SECRET | A secure secret for better-auth session encryption. |
GOOGLE_CLIENT_ID | Client ID for the Google OAuth application. |
GOOGLE_CLIENT_SECRET | Client secret for the Google OAuth application. |
MICROSOFT_CLIENT_ID | Client ID for the Microsoft Entra ID application. |
MICROSOFT_CLIENT_SECRET | Client secret for the Microsoft Entra ID application. |
ZOOM_CLIENT_ID | Client ID for the Zoom OAuth application. |
ZOOM_CLIENT_SECRET | Client secret for the Zoom OAuth application. |
Database Schema
The authentication data model is defined in packages/db/src/schema/auth.ts. It includes tables for users, accounts (linked providers), and sessions.
The following diagram illustrates the relationships between the core authentication tables.
Sources: packages/db/src/schema/auth.ts
user Table
Stores the primary profile for each user in the system.
| Column | Type | Description |
|---|---|---|
id | text | Primary key, unique user identifier. |
name | text | User's full name. |
email | text | User's unique email address. |
defaultAccountId | text | Foreign key to the account table, indicating the user's primary linked account. |
Sources: packages/db/src/schema/auth.ts:11-23
account Table
Stores information about each social provider account linked to a user. A single user can have multiple accounts (e.g., one for Google, one for Microsoft).
| Column | Type | Description |
|---|---|---|
id | text | Primary key. |
accountId | text | The unique ID for the user from the provider (e.g., Google's sub). |
providerId | text | The provider name (google, microsoft, zoom). |
userId | text | Foreign key linking to the user table. |
accessToken | text | The OAuth access token. |
refreshToken | text | The OAuth refresh token. |
Sources: packages/db/src/schema/auth.ts:58-83
session Table
Stores active user sessions.
| Column | Type | Description |
|---|---|---|
id | text | Primary key. |
expiresAt | timestamp | The expiration date of the session. |
token | text | The unique session token. |
userId | text | Foreign key linking to the user table. |
Sources: packages/db/src/schema/auth.ts:36-47
Authentication Process
The authentication process begins when an unauthenticated user attempts to sign in. The flow is managed by better-auth and integrated into the Next.js application.
The sequence diagram below shows the high-level login flow.
Session Checking
On both public and protected pages, the application first checks for an active session on the server.
- On the marketing page (
/), if a session exists, the user is immediately redirected to/calendar. - On the login page (
/login), if a session exists, the user is also redirected to/calendar.
This check is performed using auth.api.getSession({ headers: await headers() }) in a Next.js Server Component.
Sources: apps/web/src/app/(marketing)/page.tsx:7-11, apps/web/src/app/(auth)/login/page.tsx:16-19
Handling Missing Scopes
The login page can display an alert if the user is redirected with an error query parameter. For example, if a user revokes required permissions, they might be redirected to /login?error=required_scopes_missing. The page detects this and prompts the user to sign in again to grant the necessary permissions.