Skip to main content

@idpass/data-collect-core / AuthManager

Class: AuthManager

Defined in: components/AuthManager.ts:90

Manages authentication across various providers using pluggable adapters.

The AuthManager orchestrates authentication flows, supporting multiple authentication configurations (e.g., Auth0, Keycloak, or a default username/password login). It securely stores tokens and manages user session states.

Key features:

  • Pluggable Adapters: Integrates with different authentication providers via adapter pattern.
  • Multi-Provider Support: Allows configuring and using multiple authentication mechanisms simultaneously.
  • Token Management: Handles secure storage, retrieval, and removal of authentication tokens.
  • Session Management: Provides methods to check authentication status and manage logout.

Architecture:

  • Uses the Strategy pattern where each authentication provider (Auth0, Keycloak) is an adapter.
  • Adapters are dynamically loaded based on the AuthConfig provided during initialization.
  • Leverages AuthStorageAdapter for persistent storage of authentication tokens.

Examples

Basic usage with a default login:

const authManager = new AuthManager(
[{ type: 'default', url: 'http://localhost:3000' }], // Configure default login
'http://localhost:3000',
new IndexedDbAuthStorageAdapter('my-tenant')
);
await authManager.initialize();

// Attempt login
await authManager.login({ username: 'user@example.com', password: 'password123' }, 'default');
if (await authManager.isAuthenticated()) {
console.log('User is authenticated!');
}

Login with an external provider (e.g., Auth0):

const authManager = new AuthManager(
[{ type: 'auth0', clientId: '...', domain: '...' }], // Configure Auth0
'http://localhost:3000', // Sync server URL, not directly used by Auth0 adapter
new IndexedDbAuthStorageAdapter('my-tenant')
);
await authManager.initialize();

// Initiate Auth0 login flow (redirects to Auth0, then back to callback URL)
await authManager.login(null, 'auth0');

// In the callback handler:
await authManager.handleCallback('auth0');

Constructors

Constructor

new AuthManager(configs, syncServerUrl, authStorage?): AuthManager

Defined in: components/AuthManager.ts:91

Parameters

configs

AuthConfig[]

syncServerUrl

string

authStorage?

AuthStorageAdapter

Returns

AuthManager

Methods

initialize()

initialize(): Promise<void>

Defined in: components/AuthManager.ts:106

Initializes the AuthManager by instantiating and configuring authentication adapters. Based on the provided AuthConfig array, it loads the corresponding authentication adapters (e.g., Auth0, Keycloak) and prepares them for use.

Returns

Promise<void>

A Promise that resolves when all configured adapters are initialized.


isAuthenticated()

isAuthenticated(): Promise<boolean>

Defined in: components/AuthManager.ts:142

Checks if the user is currently authenticated with any of the configured providers or via the default login mechanism.

Returns

Promise<boolean>

A Promise that resolves to true if authenticated, false otherwise.

Throws

If AuthStorageAdapter is not set when checking default token.


login()

login(credentials, type?): Promise<void>

Defined in: components/AuthManager.ts:170

Handles user login, either through a specific authentication adapter or using the default username/password login mechanism to the sync server.

Parameters

credentials

The credentials for login (username/password or token).

PasswordCredentials | TokenCredentials | null

type?

string

Optional. The type of authentication provider to use (e.g., 'auth0', 'keycloak', 'default'). If not provided, and credentials are PasswordCredentials, it defaults to the 'default' login.

Returns

Promise<void>

A Promise that resolves when the login operation is complete.

Throws

If AuthStorageAdapter is not set or if login fails.


logout()

logout(): Promise<void>

Defined in: components/AuthManager.ts:245

Logs out the user from all configured authentication adapters and clears all stored tokens.

Returns

Promise<void>

A Promise that resolves when the logout operation is complete.


validateToken()

validateToken(type, token): Promise<boolean>

Defined in: components/AuthManager.ts:260

Validates an authentication token for a specific provider.

Parameters

type

string

The type of authentication provider (e.g., 'auth0', 'keycloak').

token

string

The token string to validate.

Returns

Promise<boolean>

A Promise that resolves to true if the token is valid, false otherwise.


getCurrentUser()

getCurrentUser(): { id: string; username?: string; } | null

Defined in: components/AuthManager.ts:264

Returns

{ id: string; username?: string; } | null


handleCallback()

handleCallback(type): Promise<void>

Defined in: components/AuthManager.ts:275

Handles the authentication callback for a specific provider. This is typically used in browser environments after a redirect from an OAuth provider.

Parameters

type

string

The type of authentication provider (e.g., 'auth0', 'keycloak').

Returns

Promise<void>

A Promise that resolves when the callback is handled.