File

projects/netgrif-components-core/src/lib/authentication/services/authentication/authentication.service.ts

Index

Properties
Methods
Accessors

Constructor

constructor(_auth: AuthenticationMethodService, _config: ConfigurationService, _sessionService: SessionService, _userTransformer: UserTransformer)
Parameters :
Name Type Optional
_auth AuthenticationMethodService No
_config ConfigurationService No
_sessionService SessionService No
_userTransformer UserTransformer No

Methods

login
login(credentials: Credentials)
Parameters :
Name Type Optional
credentials Credentials No
Returns : Observable<User>
logout
logout()
Returns : Observable<object>
ngOnDestroy
ngOnDestroy()
Returns : void

Properties

Protected _authenticated$
Type : BehaviorSubject<boolean>
Protected Static Readonly IDENTIFICATION_ATTRIBUTE
Type : string
Default value : 'id'
Protected subSession
Type : Subscription

Accessors

isAuthenticated
getisAuthenticated()
authenticated$
getauthenticated$()
import {Credentials} from '../../models/credentials';
import {Injectable, OnDestroy} from '@angular/core';
import {AuthenticationMethodService} from '../authentication-method.service';
import {BehaviorSubject, Observable, of, Subscription} from 'rxjs';
import {ConfigurationService} from '../../../configuration/configuration.service';
import {catchError, map, tap} from 'rxjs/operators';
import {User} from '../../../user/models/user';
import {UserTransformer} from '../../models/user.transformer';
import {SessionService} from '../../session/services/session.service';
import {UserResource} from '../../../resources/interface/user-resource';

@Injectable({
    providedIn: 'root'
})
export class AuthenticationService implements OnDestroy {

    protected static readonly IDENTIFICATION_ATTRIBUTE = 'id';

    protected _authenticated$: BehaviorSubject<boolean>;
    protected subSession: Subscription;

    constructor(protected _auth: AuthenticationMethodService,
                protected _config: ConfigurationService,
                protected _sessionService: SessionService,
                protected _userTransformer: UserTransformer) {
        this._authenticated$ = new BehaviorSubject<boolean>(false);
        this.subSession = this._sessionService.session$.subscribe(token => {
            this._authenticated$.next(!!token && token.length !== 0 && this._sessionService.verified);
        });
    }

    login(credentials: Credentials): Observable<User> {
        return this._auth.login(credentials).pipe(
            tap((user: UserResource) => {
                this._authenticated$.next(!!user[AuthenticationService.IDENTIFICATION_ATTRIBUTE]);
            }),
            map((user: UserResource) => this._userTransformer.transform(user)),
            catchError(error => {
                console.error(error);
                return of(null);
            })
        );
    }

    logout(): Observable<object> {
        return this._auth.logout().pipe(
            tap(() => {
                this._authenticated$.next(false);
                this._sessionService.clear();
            }),
            catchError(error => {
                console.error(error);
                return of(error);
            })
        );
    }

    get isAuthenticated(): boolean {
        return this._authenticated$.getValue();
    }

    get authenticated$(): Observable<boolean> {
        return this._authenticated$.asObservable();
    }

    ngOnDestroy(): void {
        this.subSession.unsubscribe();
        this._authenticated$.complete();
    }
}

result-matching ""

    No results matching ""