Creates a new Authentication API client instance
Initializes the authentication client with an optional configuration. If no configuration is provided, it uses the ConfigurationManager to get the current configuration.
Optionalconfig: MosaiaConfigOptional configuration object
Configuration interface for the Mosaia SDK
This interface defines all configuration options available when initializing the Mosaia SDK client. It includes authentication settings, API endpoints, and optional context parameters.
OptionalapiKey?: stringAPI key for authentication (optional if using OAuth)
Optionalversion?: stringAPI version to use (defaults to '1')
OptionalapiURL?: stringBase URL for API requests (defaults to https://api.mosaia.ai)
OptionalclientId?: stringClient ID for client credentials flows (Optional)
OptionalclientSecret?: stringClient secret for client credentials flow (optional)
Optionalverbose?: booleanEnable verbose HTTP request/response logging (defaults to false)
Optionalsession?: SessionCredentialsSession credentials for OAuth and token-based authentication (optional)
Sign in using email and password authentication
Authenticates a user with their email and password credentials. Returns a configured Mosaia client instance with the obtained access token.
The user's email address
The user's password
Promise that resolves to a configured Mosaia client instance
Sign in using client credentials authentication
Authenticates an application using client ID and client secret. This flow is typically used for server-to-server authentication where no user interaction is required.
The OAuth client ID
The OAuth client secret
Promise that resolves to a configured Mosaia client instance
const auth = new MosaiaAuth();
try {
const mosaiaConfig = await auth.signInWithClient('client-id', 'client-secret');
mosaia.config = mosaiaConfig;
console.log('Successfully authenticated with client credentials');
} catch (error) {
console.error('Client authentication failed:', error.message);
}
Refresh an access token using a refresh token
Obtains a new access token using an existing refresh token. This method can be used to extend a user's session without requiring them to re-enter their credentials.
Optionaltoken: stringOptional refresh token. If not provided, attempts to use the refresh token from the current configuration
Promise that resolves to an updated MosaiaConfig
const auth = new MosaiaAuth();
try {
// Use refresh token from config
const mosaia = await auth.refreshToken();
// Or provide a specific refresh token
const mosaiaConfig = await auth.refreshToken('specific-refresh-token');
mosaia.config = mosaiaConfig;
} catch (error) {
console.error('Token refresh failed:', error.message);
}
Refreshes an OAuth access token using a refresh token
This method exchanges a refresh token for a new access token when the current access token expires. This allows for long-term authentication without requiring user re-authentication.
The refresh token received from the initial token exchange
Promise that resolves to a new OAuth token response
// When access token expires, use refresh token to get new tokens
try {
const mosaiaConfig = await oauth.refreshToken(refreshToken);
mosaia.config = mosaiaConfig;
console.log('New access token:', mosaiaConfig.apiKey);
console.log('New refresh token:', mosaiaConfig.refreshToken);
} catch (error) {
// Refresh token expired, user needs to re-authenticate
console.error('Token refresh failed:', error);
}
Sign out and invalidate the current session
Invalidates the current access token and clears the configuration. This method should be called when a user logs out or when you want to ensure the current session is terminated.
OptionalapiKey: stringOptional API key to sign out. If not provided, uses the API key from the current configuration
Promise that resolves when sign out is complete
Authentication API client for the Mosaia SDK
This class provides comprehensive authentication functionality for the Mosaia SDK, supporting multiple authentication flows:
The class integrates with ConfigurationManager for centralized configuration management and uses APIClient for making authenticated HTTP requests.
Remarks
All authentication methods return a MosaiaConfig object that can be used to configure the main SDK client. The configuration includes access tokens, refresh tokens, and session information.
Example
Basic usage with password authentication:
Example
Client credentials authentication:
Example
Token refresh and sign out: