File

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

Index

Properties
Methods

Constructor

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

Methods

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 headers
Default value : new HttpHeaders()
Private mfaModel
Type : MultiFactorModel
Default value : null
Public mfaModel$
Default value : this.publisher.asObservable()
Private publisher
Default value : new BehaviorSubject<MultiFactorModel>(this.mfaModel)
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';
import { catchError } from 'rxjs/operators/catchError';
import { MULTI_FACTOR_API } from '../../app.constants';
import { MultiFactorModel } from '../../models/multifactor.model';
import { SessionStore } from '../stores';
import { AppStateStore } from '../stores/app-state.store';
import { ErrorStore } from '../stores/error.store';

@Injectable()
export class MultiFactorService {
  private headers = new HttpHeaders();
  private mfaModel: MultiFactorModel = null;
  private publisher = new BehaviorSubject<MultiFactorModel>(this.mfaModel);
  public mfaModel$ = this.publisher.asObservable();

  constructor(
    private http: HttpClient,
    private errorService: ErrorStore,
    private sessionStore: SessionStore,
    private app: AppStateStore
  ) {
    this.headers.append('Cache-control', 'no-cache');
    this.headers.append('Pragma', 'no-cache');
    this.headers.append('Expires', '0');
  }

  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.showError(
            'Failed to request code',
            error.statusText
          );
          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.showError(
            'Failed to verify code',
            error.statusText
          );
          return Observable.throw(error);
        })
      );
  }
}

results matching ""

    No results matching ""