src/lib/authentication.service.ts
Properties |
|
Methods |
|
constructor()
|
|
Defined in src/lib/authentication.service.ts:37
|
| Public isAuthenticated |
isAuthenticated()
|
|
Defined in src/lib/authentication.service.ts:49
|
|
Returns :
Promise<boolean>
|
| Public Async signOut |
signOut()
|
|
Defined in src/lib/authentication.service.ts:43
|
|
Returns :
Promise<void>
|
| Public Readonly events$ |
Default value : new ReplaySubject<AuthenticationEvent>()
|
|
Defined in src/lib/authentication.service.ts:35
|
| Public isAuthenticated$ |
Default value : new BehaviorSubject<boolean | null>(null)
|
|
Defined in src/lib/authentication.service.ts:33
|
import {
inject,
Injectable,
} from '@angular/core';
import {
BehaviorSubject,
Observable,
ReplaySubject,
} from 'rxjs';
import { RXAP_INITIAL_AUTHENTICATION_STATE } from './tokens';
export interface IAuthenticationService {
isAuthenticated$: Observable<boolean | null>;
events$: Observable<AuthenticationEvent>;
signOut(): Promise<void>;
isAuthenticated(): Promise<boolean>;
}
export enum AuthenticationEventType {
OnAuthSuccess = 'on-auth-success',
OnAuthError = 'on-auth-error',
OnLogout = 'on-logout',
}
export interface AuthenticationEvent extends Record<string, any> {
type: AuthenticationEventType;
}
@Injectable({ providedIn: 'root' })
export class RxapAuthenticationService implements IAuthenticationService {
public isAuthenticated$ = new BehaviorSubject<boolean | null>(null);
public readonly events$ = new ReplaySubject<AuthenticationEvent>();
private _authenticated = inject(RXAP_INITIAL_AUTHENTICATION_STATE);
constructor() {
this.isAuthenticated().then(isAuthenticated => this.isAuthenticated$.next(isAuthenticated));
}
public async signOut(): Promise<void> {
this._authenticated = false;
this.isAuthenticated$.next(this._authenticated);
this.events$.next({ type: AuthenticationEventType.OnLogout });
}
public isAuthenticated(): Promise<boolean> {
return Promise.resolve(this._authenticated);
}
}