UNPKG

@devlearning/jwt-auth

Version:

Jwt Angular Authentication manager with automatic Refresh Token management.

79 lines (78 loc) 3.56 kB
import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; import { JwtTokenBase } from './models/jwt-token-base'; import { JwtAuthConfig } from './models/jwt-auth-config'; import { MutexFastLockService } from '@devlearning/mutex-fast-lock'; import * as i0 from "@angular/core"; export declare class JwtAuthService<Token extends JwtTokenBase> { private readonly _config; private readonly _http; private readonly _mutexFastLock; private _isLoggedInSubject; private _isRefreshingTokenSubject; private _jwtTokenSubject; private _isLocalStorageSupported; private _refreshTokenSubject; private _storage; /** Emits whenever the login state changes. */ get isLoggedIn$(): Observable<boolean>; /** Emits whenever the current token changes (null when logged out). */ get jwtToken$(): Observable<Token>; /** Emits `true` while a token refresh is in progress. */ get refreshingToken$(): Observable<boolean>; /** Current login state (synchronous). */ get isLoggedIn(): boolean; /** Current token (synchronous). `null` when logged out. */ get jwtToken(): Token; constructor(_config: JwtAuthConfig, _http: HttpClient, _mutexFastLock: MutexFastLockService); /** * Restores the session from storage. If the access token is expired but the * refresh token is still valid, a refresh is attempted automatically. * Call this manually when `useManualInitialization` is `true`. */ init(): Observable<Token>; /** * Authenticates the user by posting `request` to `tokenUrl`. * On success the token is persisted to storage and reactive state is updated. * * @param request The login request body. Use the generic parameter `TRequest` * to get full type-safety for your specific API contract. */ token<TRequest = object>(request: TRequest): Observable<Token>; /** * Refreshes the access token using the stored refresh token. * Concurrent calls are serialized with a mutex so only one HTTP request is * made; other callers wait and receive the same result. * * The request body is built by `refreshTokenRequestFactory` (if configured) * or falls back to `{ username, refreshToken }`. */ refreshToken(): Observable<Token>; /** Clears the stored token and emits the logged-out state. */ logout(): void; /** Returns `true` if `url` is one of the authentication endpoints (token or refresh). */ isAuthenticationUrl(url: string): boolean; /** Returns `true` if the refresh token has passed its expiry timestamp. */ isRefreshTokenExpired(): boolean; /** Returns `true` if the access token has passed its expiry timestamp. */ isTokenExpired(): boolean; /** Overrides the token URL at runtime. */ setTokenUrl(url: string): void; /** Overrides the refresh URL at runtime. */ setRefreshUrl(url: string): void; private getIsRefreshingToken; private setRefreshingToken; /** Manually sets a token, persisting it to storage and updating reactive state. */ setToken(jwtToken: Token): void; private _setToken; private _cleanToken; private _checkStorageIsSupported; private _getLocalStorageSupported; private _saveJwtToken; private _getJwtToken; private _deleteJwtToken; private _checkTokenIsExpired; private _handleError; static ɵfac: i0.ɵɵFactoryDeclaration<JwtAuthService<any>, never>; static ɵprov: i0.ɵɵInjectableDeclaration<JwtAuthService<any>>; }