UNPKG

ngx-auth

Version:

Angular 20+ Authentication module

79 lines (72 loc) 3.01 kB
import * as _angular_core from '@angular/core'; import { InjectionToken, Type } from '@angular/core'; import { HttpInterceptorFn, HttpErrorResponse, HttpRequest } from '@angular/common/http'; import { Observable } from 'rxjs'; import { CanActivateFn } from '@angular/router'; declare const NgxRefreshInProgress: _angular_core.WritableSignal<boolean>; declare const ngxAuthInterceptor: HttpInterceptorFn; /** * Essential service for authentication */ declare abstract class NgxAuthService { /** * Get access token * Should return access token in Observable from e.g. * localStorage */ abstract getAccessToken(): Promise<string | null> | Observable<string | null>; /** * Add token to headers, dependent on server * set-up, by default adds a bearer token. * Called by interceptor. * To change behavior, override this method. */ abstract getHeaders?(token: string): Record<string, string | string[]>; /** * Check, if user already authorized. * Should return Observable with true or false values */ abstract isAuthenticated(): Promise<boolean> | Observable<boolean>; /** * Function, checks response of failed request to determine, * whether token be refreshed or not. * * Essentially checks status */ abstract refreshShouldHappen(response: HttpErrorResponse, request?: HttpRequest<any>): Promise<boolean> | Observable<boolean> | boolean; /** * Function, that should perform refresh token * Should be successfully completed so interceptor * can execute pending requests or retry original one */ abstract refreshToken(): Promise<any> | Observable<any>; /** * Saves last interrupted url inside of the service for further reusage, * e.g. restoring interrupted page after logging in */ abstract setInterruptedUrl?(url: string): void; /** * Checks if request must be skipped by interceptor. * Useful for requests such as request token which doesn't require token in headers */ abstract skipRequest(request: HttpRequest<any>): boolean; } declare const AUTH_SERVICE: InjectionToken<NgxAuthService>; declare const PUBLIC_REDIRECT_URI: InjectionToken<string>; /** * Guard, checks access token availability and allows or disallows access to page, * and redirects out */ declare const ngxProtectedGuard: CanActivateFn; declare function provideNgxAuthProviders(options: { authService: Type<NgxAuthService>; protectedRedirectUri: string; publicRedirectUri: string; }): _angular_core.EnvironmentProviders; declare const PROTECTED_REDIRECT_URI: InjectionToken<string>; /** * Guard, checks access token availability and allows or disallows access to page, * and redirects out */ declare const ngxPublicGuard: CanActivateFn; export { AUTH_SERVICE, NgxAuthService, NgxRefreshInProgress, PROTECTED_REDIRECT_URI, PUBLIC_REDIRECT_URI, ngxAuthInterceptor, ngxProtectedGuard, ngxPublicGuard, provideNgxAuthProviders };