src/app/services/multi-factor.service.ts
Properties |
Methods |
constructor(http: HttpClient, errorService: ErrorStore, sessionStore: SessionStore, app: AppState)
|
|||||||||||||||
Defined in src/app/services/multi-factor.service.ts:19
|
|||||||||||||||
Parameters :
|
getMethods | ||||||
getMethods(useCache: )
|
||||||
Defined in src/app/services/multi-factor.service.ts:27
|
||||||
Parameters :
Returns :
Observable<MultiFactorModel>
|
requestCode |
requestCode(method: string, target: string)
|
Defined in src/app/services/multi-factor.service.ts:42
|
Returns :
Observable<boolean>
|
verifyCode |
verifyCode(code: string, sentTo: string)
|
Defined in src/app/services/multi-factor.service.ts:56
|
Returns :
Observable<boolean>
|
Private cache |
cache:
|
Type : MultiFactorModel
|
Default value : null
|
Defined in src/app/services/multi-factor.service.ts:14
|
Private headers |
headers:
|
Default value : new HttpHeaders({
'Cache-Control': 'no-cache',
Pragma: 'no-cache',
Expires: '-1'
})
|
Defined in src/app/services/multi-factor.service.ts:15
|
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);
})
);
}
}