File
Methods
requestCode
|
requestCode(method: string, target: string)
|
|
Returns : Observable<boolean>
|
verifyCode
|
verifyCode(code: string, sentTo: string)
|
|
Returns : Observable<boolean>
|
Private
headers
|
Default value : new HttpHeaders()
|
|
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);
})
);
}
}