Interface for Angular authentication providers - v3.0.0

This interface defines the contract that all auth providers must implement. It ensures consistent behavior across different OAuth providers while hiding provider-specific implementation details.

Breaking Changes from v2.x:

  • Removed: getUserProfile() - Use getUserInfo() instead
  • Removed: getUser() - Use getUserInfo() instead
  • Removed: getUserClaims() - Use getTokenInfo() instead
  • Removed: getToken() - Use getIdToken() instead
  • Removed: refresh() - Use refreshToken() instead
  • Removed: checkExpiredTokenError() - Use classifyError() instead

Version

3.0.0

interface IAngularAuthProvider {
    type: string;
    classifyError(error): StandardAuthError;
    getIdToken(): Promise<null | string>;
    getRequiredConfig(): string[];
    getTokenInfo(): Promise<null | StandardAuthToken>;
    getUserEmail(): Observable<string>;
    getUserInfo(): Observable<null | StandardUserInfo>;
    handleCallback(): Promise<void>;
    initialize(): Promise<void>;
    isAuthenticated(): Observable<boolean>;
    login(options?): Promise<void> | Observable<void>;
    logout(): Promise<void>;
    refreshToken(): Promise<StandardAuthToken>;
    validateConfig(config): boolean;
}

Implemented by

Properties

type: string

Provider type identifier (e.g., 'msal', 'auth0', 'okta')

Methods

  • Classify an error into a standard error type

    This method converts provider-specific errors into semantic error types that application code can handle consistently. Eliminates the need for consumers to check provider-specific error names like 'BrowserAuthError'.

    Parameters

    • error: unknown

      The error to classify (can be any type)

    Returns StandardAuthError

    StandardAuthError with categorized error type

    Example

    try {
    await this.authBase.login();
    } catch (err) {
    const authError = this.authBase.classifyError(err);

    switch (authError.type) {
    case AuthErrorType.TOKEN_EXPIRED:
    // Show "session expired" message
    break;
    case AuthErrorType.USER_CANCELLED:
    // User cancelled - don't show error
    break;
    default:
    // Show generic error
    alert(authError.userMessage);
    }
    }
  • Get the current ID token as a string

    This is the primary method applications should use to get the token for backend API calls. It abstracts away provider-specific token storage (Auth0's __raw vs MSAL's idToken).

    Returns Promise<null | string>

    Promise resolving to the ID token string, or null if not authenticated

    Example

    const token = await this.authBase.getIdToken();
    if (token) {
    // Use token for GraphQL or REST API calls
    setupGraphQLClient(token, apiUrl);
    }
  • Get complete token information

    Returns a standardized token object with ID token, access token, and expiration. Use this when you need more than just the token string.

    Returns Promise<null | StandardAuthToken>

    Promise resolving to StandardAuthToken or null if not authenticated

    Example

    const tokenInfo = await this.authBase.getTokenInfo();
    if (tokenInfo) {
    console.log(`Token expires at: ${new Date(tokenInfo.expiresAt)}`);
    }
  • Observable stream of authentication state

    Emits true when user is authenticated, false otherwise. Subscribe to this for reactive UI updates.

    Returns Observable<boolean>

    Example

    this.authBase.isAuthenticated().subscribe(isAuth => {
    this.showLoginButton = !isAuth;
    });
  • Initiate login flow

    Parameters

    • Optional options: Record<string, unknown>

      Optional provider-specific login options

    Returns Promise<void> | Observable<void>

    Observable for backward compatibility, can also return Promise

    Example

    await this.authBase.login({ appState: { target: '/dashboard' } });
    
  • Refresh the current authentication token

    Attempts to obtain a fresh authentication token using the provider's refresh mechanism. If silent refresh fails due to session expiry, the provider will handle re-authentication automatically (which may involve redirecting to the auth provider's login page).

    Returns a fresh token on success, or throws on complete failure.

    IMPORTANT: If the provider requires interactive re-authentication (redirect or popup), this method may never return. The app will reload after authentication completes and re-initialize with a fresh token.

    Returns Promise<StandardAuthToken>

    Promise resolving to StandardAuthToken or throws on failure

    Example

    const token = await this.authBase.refreshToken();
    return token.idToken; // Always succeeds or throws