Feature: Multi-Account Management
Relevant source files
The following files were used as context for generating this wiki page:The Multi-Account Management feature allows a single Analog user to connect multiple external service accounts, such as Google, Microsoft, and Zoom. This system is fundamental to the application's ability to aggregate data from various sources, providing a unified calendar experience. The feature is built upon the better-auth library, which handles the complexities of OAuth flows and account linking.
Users can manage their connected accounts through the settings dialog, where they can add new accounts or disconnect existing ones. The backend architecture supports linking these external provider accounts to a primary Analog user record in the database, storing necessary tokens to maintain API access.
Architecture Overview
The multi-account system is composed of three main layers: the frontend UI, the backend API (tRPC), and the database. The better-auth library serves as the core middleware, orchestrating authentication and account linking.
Core Components
| Component | Description | Source File(s) |
|---|---|---|
better-auth | The authentication library configured to handle social providers and account linking. It manages OAuth flows, token storage, and session management. | packages/auth/src/server.ts |
| Database Schema | PostgreSQL tables (user, account) defined with Drizzle ORM to store user profiles and their linked external accounts. | packages/db/src/schema/auth.ts |
| Settings UI | React components within the settings dialog that allow users to view, add, and remove their connected accounts. | apps/web/src/components/settings-dialog/tabs/accounts.tsx |
| tRPC API | API endpoints used by the frontend to fetch the list of connected accounts and trigger actions like unlinking. | apps/web/src/components/settings-dialog/tabs/accounts.tsx |
Data Model
The database schema is designed to support a one-to-many relationship between a user and their connected accounts. A single user record can be associated with multiple account records, each representing a connection to an external provider.
The following Entity-Relationship diagram illustrates the relationship between the user and account tables.
Authentication and Account Linking
The system uses better-auth to manage all authentication and account linking logic. The configuration enables linking multiple trusted social providers to a single user account, even if the email addresses differ.
Sources: packages/auth/src/server.ts:40-47
Provider Configuration
Analog is configured to support Google, Microsoft, and Zoom as OAuth providers. For development, specific setup instructions are provided to create OAuth credentials for each service.
Sources: CONTRIBUTING.md:33-63, README.md:40-70
The scopes requested from each provider are defined to access calendar and user profile information.
| Provider | Requested OAuth Scopes |
|---|---|
email, profile, openid, https://www.googleapis.com/auth/calendar | |
| Microsoft | https://graph.microsoft.com/User.Read, https://graph.microsoft.com/Calendars.Read, https://graph.microsoft.com/Calendars.Read.Shared, https://graph.microsoft.com/Calendars.ReadBasic, https://graph.microsoft.com/Calendars.ReadWrite, https://graph.microsoft.com/Calendars.ReadWrite.Shared, offline_access |
| Zoom | user:read, calendar:read, calendar:write, meeting:read, meeting:write, offline_access |
Sources: packages/auth/src/server.ts:19-38
better-auth Configuration
The core configuration for better-auth is located in packages/auth/src/server.ts. Key settings for multi-account management include:
// packages/auth/src/server.ts:40-47
export const auth = betterAuth({
// ...
account: {
accountLinking: {
enabled: true,
allowDifferentEmails: true,
trustedProviders: ["google", "microsoft", "zoom"],
},
},
// ...
});
Sources: packages/auth/src/server.ts:40-47
This configuration allows a logged-in user to link additional accounts from the trustedProviders list.
Database Schema
The persistence layer uses Drizzle ORM with a PostgreSQL database. The user and account tables are central to this feature.
user Table
Stores the primary user profile for the Analog application. It includes a reference to a default account for new operations.
| Column | Type | Description |
|---|---|---|
id | text | Primary key for the user. |
name | text | User's name. |
email | text | User's unique email address. |
defaultAccountId | text | Foreign key to the account table, specifying the default account to use. Can be null. |
defaultCalendarId | text | The ID of the default calendar for creating new events. |
Sources: packages/db/src/schema/auth.ts:9-24
account Table
Stores information for each external account linked to a user.
| Column | Type | Description |
|---|---|---|
id | text | Primary key for the account record. |
accountId | text | The unique identifier for the account from the provider (e.g., Google user ID). |
providerId | text | The provider identifier, e.g., "google", "microsoft", or "zoom". |
email | text | The email associated with the external account. |
userId | text | Foreign key linking to the user table. |
accessToken | text | The OAuth access token for API calls. |
refreshToken | text | The OAuth refresh token to get new access tokens. |
accessTokenExpiresAt | timestamp | Expiration timestamp for the access token. |
Sources: packages/db/src/schema/auth.ts:60-84
Frontend Implementation
The user-facing part of this feature is located in the "Accounts" tab of the settings dialog. It is built with React and uses TanStack Query with tRPC for data fetching and mutations.
Sources: apps/web/src/components/settings-dialog/tabs/accounts.tsx
Displaying Connected Accounts
The AccountsList component fetches and displays the list of connected accounts for the current user.
- The component uses the
useQueryhook withtrpc.accounts.list.queryOptions()to fetch data. - The API returns a list of accounts, which are then rendered by the
AccountListItemcomponent. - Each item displays the account's email and an icon corresponding to the provider (
google,microsoft,zoom).
This diagram shows the data flow for listing accounts.
Adding a New Account
The AddAccountButton component initiates the flow for linking a new external account.
- When a user clicks "Add Account", the
linkAccountfunction is called. - This function calls
authClient.linkSocial({ provider: "google", ... }). better-authhandles the redirect to the Google OAuth consent screen.- After user approval, Google redirects back to the application.
- The
better-authbackend handler processes the callback, creates a newaccountrecord in the database linked to the current user, and stores the tokens.
// apps/web/src/components/settings-dialog/tabs/accounts.tsx:252-257
function AddAccountButton() {
const linkAccount = async () => {
await authClient.linkSocial({
provider: "google",
callbackURL: "/calendar",
});
};
//...
}
The following sequence diagram illustrates the process of adding an account.
Disconnecting an Account
The AccountListItem component provides the functionality to disconnect an account.
- The user clicks the "Disconnect" button.
- A mutation is triggered, which calls
authClient.unlinkAccountwith theproviderIdandproviderAccountId. - This sends a request to the backend, where a
better-authhook (handleUnlinkAccount) is executed to delete the correspondingaccountrecord from the database. - On success, relevant TanStack Query caches (
accounts,calendars,events) are invalidated to refresh the UI.
The following sequence diagram illustrates the process of disconnecting an account.
If a user signs in but has not granted the required scopes (e.g., for Google Calendar), they are redirected to the login page with an alert prompting them to sign in again to grant the necessary permissions.
Sources: apps/web/src/app/(auth)/login/page.tsx:23-24
Summary
The Multi-Account Management feature is a cornerstone of Analog, enabling users to consolidate their digital lives into a single interface. By leveraging better-auth for robust authentication and a well-defined database schema, the system provides a seamless experience for adding and managing connections to external services like Google, Microsoft, and Zoom. The frontend, built with React and tRPC, offers an intuitive UI for users to control which accounts are connected, ensuring data is always synchronized and accessible.