File

src/app/services/auth.service.ts

Index

Properties
Methods
Accessors

Constructor

constructor(http: HttpClient, router: Router, storage: StorageService, sessionStore: SessionStore, errorStore: ErrorStore, helper: JwtHelperService, featureFlagService: FeatureFlagService)
Parameters :
Name Type Optional
http HttpClient no
router Router no
storage StorageService no
sessionStore SessionStore no
errorStore ErrorStore no
helper JwtHelperService no
featureFlagService FeatureFlagService no

Methods

Private fallbackToken
fallbackToken()
Returns : string
getToken
getToken()
Returns : string
login
login(credentials: Credentials)
Parameters :
Name Type Optional
credentials Credentials no
Returns : Observable<boolean>
logout
logout()
Returns : void

Properties

Private token
token: string
Type : string

Accessors

authenticated
getauthenticated()
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { JwtHelperService } from '@auth0/angular-jwt';
import { Observable } from 'rxjs/Observable';
import { AUTH_API, JWT_KEY, RT247_FLAG } from '../app.constants';
import { AuthResponse } from '../models/auth-response.model';
import { Credentials } from '../models/credentials.model';
import { IJwt } from '../models/i-jwt.model';
import { ErrorStore } from '../stores/error.store';
import { SessionStore } from '../stores/session.store';
import { StorageService } from './storage.service';
import { map, catchError } from 'rxjs/operators';
import { FeatureFlagService } from './feature-flag.service';

@Injectable()
export class AuthService {
  private token: string;

  constructor(
    private http: HttpClient,
    private router: Router,
    private storage: StorageService,
    private sessionStore: SessionStore,
    private errorStore: ErrorStore,
    private helper: JwtHelperService,
    private featureFlagService: FeatureFlagService
  ) {}

  get authenticated(): boolean {
    try {
      const rawToken = this.getToken();
      const isExpired = this.helper.isTokenExpired(rawToken);
      return !isExpired;
    } catch (error) {
      return false;
    }
  }

  login(credentials: Credentials): Observable<boolean> {
    return this.http.post<AuthResponse>(`${AUTH_API}/login`, credentials).pipe(
      map((res) => {
        const authenticated = res.token && res.token.length > 0;
        if (authenticated) {
          this.storage.set(JWT_KEY, res.token);
          const jwt = this.helper.decodeToken(res.token) as IJwt;

          //RealTime247 Feature Flag
          this.storage.set(RT247_FLAG, "false");
          this.featureFlagService.getFeatureFlag(jwt.domain).subscribe((data: any) => {
            this.storage.set(RT247_FLAG, data.IsOn);
          });

          this.sessionStore.createSession(jwt);
        }
        return authenticated;
      }),
      catchError((err) => {
        return Observable.of(false);
      })
    );
  }

  logout(): void {
    this.sessionStore.clearSession();
    this.http.get(`${AUTH_API}/signout`).subscribe(() => {});
    this.router.navigate(['/login']);
  }

  getToken(): string {
    return this.token ? this.token : this.fallbackToken();
  }

  private fallbackToken(): string {
    return this.storage.get(JWT_KEY);
  }
}

results matching ""

    No results matching ""