File

src/app/services/multi-factor.service.ts

Index

Properties
Methods

Constructor

constructor(http: HttpClient, errorService: ErrorStore, sessionStore: SessionStore, app: AppState)
Parameters :
Name Type Optional
http HttpClient no
errorService ErrorStore no
sessionStore SessionStore no
app AppState no

Methods

getMethods
getMethods(useCache: )
Parameters :
Name Optional Default value
useCache no true
requestCode
requestCode(method: string, target: string)
Parameters :
Name Type Optional
method string no
target string no
Returns : Observable<boolean>
verifyCode
verifyCode(code: string, sentTo: string)
Parameters :
Name Type Optional
code string no
sentTo string no
Returns : Observable<boolean>

Properties

Private cache
cache: MultiFactorModel
Type : MultiFactorModel
Default value : null
Private headers
headers:
Default value : new HttpHeaders({ 'Cache-Control': 'no-cache', Pragma: 'no-cache', Expires: '-1' })
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { catchError } from 'rxjs/operators/catchError';
import { map } from 'rxjs/operators/map';
import { MULTI_FACTOR_API } from '../app.constants';
import { MultiFactorModel } from '../models';
import { SessionStore } from '../stores';
import { AppState } from '../stores/app-state.store';
import { ErrorStore } from '../stores/error.store';

@Injectable()
export class MultiFactorService {
  private cache: MultiFactorModel = null;
  private headers = new HttpHeaders({
    'Cache-Control': 'no-cache',
    Pragma: 'no-cache',
    Expires: '-1'
  });
  constructor(
    private http: HttpClient,
    private errorService: ErrorStore,
    private sessionStore: SessionStore,
    private app: AppState
  ) {}

  getMethods(useCache = true): Observable<MultiFactorModel> {
    if (useCache && this.cache !== null) {
      this.app.multiFactorModel = this.cache;
      return Observable.of(this.cache);
    }
    return this.sessionStore.session$.pipe(
      map(session => new MultiFactorModel(session)),
      map(model => {
        this.cache = model;
        this.app.multiFactorModel = this.cache;
        return model;
      })
    );
  }

  requestCode(method: string, target: string): Observable<boolean> {
    return this.http
      .get<boolean>(`${MULTI_FACTOR_API}/code?method=${method}&target=${target}`, {
        headers: this.headers
      })
      .pipe(
        catchError(error => {
          this.errorService.addError('Failed to request code', error.statusText);
          this.errorService.displayErrors();
          return Observable.throw(error);
        })
      );
  }

  verifyCode(code: string, sentTo: string): Observable<boolean> {
    return this.http
      .get<boolean>(`${MULTI_FACTOR_API}/verify?code=${code}&sentTo=${sentTo}`, {
        headers: this.headers
      })
      .pipe(
        catchError(error => {
          this.errorService.addError('Failed to verify code', error.statusText);
          this.errorService.displayErrors();
          return Observable.throw(error);
        })
      );
  }
}

results matching ""

    No results matching ""