Base class for Angular authentication providers - v3.0.0

Provides common functionality and enforces the provider interface. All concrete providers (MSAL, Auth0, Okta) should extend this class.

Key Improvements in v3.0:

  • Proper abstraction - no more leaky provider-specific logic
  • Standardized types - no more any types
  • Clean token access - no more __raw || idToken patterns
  • Semantic error handling - no more provider-specific error checks

For Provider Implementers:

Extend this class and implement the abstract methods:

  • extractIdTokenInternal() - Extract token from provider storage
  • extractTokenInfoInternal() - Extract full token info
  • extractUserInfoInternal() - Map claims to StandardUserInfo
  • refreshTokenInternal() - Implement refresh mechanism
  • classifyErrorInternal() - Map errors to AuthErrorType

Version

3.0.0

Hierarchy (view full)

Implements

Constructors

Properties

_initialPath: null | string = null
_initialSearch: null | string = null
isAuthenticated$: BehaviorSubject<boolean> = ...
type: string

Provider type identifier Must be implemented by concrete providers

userEmail$: BehaviorSubject<string> = ...
userInfo$: BehaviorSubject<null | StandardUserInfo> = ...

Accessors

Methods

  • Extract ID token from provider-specific storage

    This is where providers hide their implementation details.

    • Auth0: Extracts from claims.__raw
    • MSAL: Extracts from response.idToken
    • Okta: Extracts from authState.idToken

    Returns Promise<null | string>

    Promise resolving to token string or null if not authenticated

  • Get profile picture URL from auth provider

    Returns the user's profile picture URL if available from the auth provider. This abstracts away provider-specific logic:

    • Microsoft/MSAL: Fetches from Graph API
    • Auth0/Okta: Returns from user claims

    Returns Promise<null | string>

    Promise resolving to image URL or null if not available

    Example

    const pictureUrl = await this.authBase.getProfilePictureUrl();
    if (pictureUrl) {
    this.userAvatar = pictureUrl;
    }
  • Get profile picture URL from auth provider

    Retrieves the user's profile picture using provider-specific mechanisms. Some providers include the URL in user claims, others require API calls to fetch the image.

    Returns Promise<null | string>

    Promise resolving to image URL or null if not available

  • Handle session expiry when silent refresh fails

    Called internally when silent token refresh fails with TOKEN_EXPIRED or INTERACTION_REQUIRED errors. Providers that support refresh tokens can implement this as a no-op. Providers that require interactive re-authentication should initiate the appropriate flow (redirect, popup, etc.).

    Note: If this method redirects the page, it may never return. The app will reload after authentication completes and re-initialize with a fresh token.

    Returns Promise<void>

    Promise that resolves if re-auth completed, or never returns if redirected

  • Refresh 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 StandardAuthToken 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
  • Refresh token using provider-specific mechanism

    Implements the provider's token refresh logic using whatever mechanism is appropriate (silent refresh with refresh tokens, iframe-based token acquisition, etc.).

    Should return success with token if refresh succeeds, or failure with appropriate error type (TOKEN_EXPIRED, INTERACTION_REQUIRED, etc.) if refresh fails.

    Returns Promise<TokenRefreshResult>

    Promise resolving to TokenRefreshResult indicating success/failure