1 import { catchError } from 'rxjs/operators'
2 import { HttpClient } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { RestExtractor, UserService } from '@app/core'
5 import { TwoFactorEnableResult } from '@shared/models'
8 export class TwoFactorService {
10 private authHttp: HttpClient,
11 private restExtractor: RestExtractor
14 // ---------------------------------------------------------------------------
16 requestTwoFactor (options: {
18 currentPassword: string
20 const { userId, currentPassword } = options
22 const url = UserService.BASE_USERS_URL + userId + '/two-factor/request'
24 return this.authHttp.post<TwoFactorEnableResult>(url, { currentPassword })
25 .pipe(catchError(err => this.restExtractor.handleError(err)))
28 confirmTwoFactorRequest (options: {
33 const { userId, requestToken, otpToken } = options
35 const url = UserService.BASE_USERS_URL + userId + '/two-factor/confirm-request'
37 return this.authHttp.post(url, { requestToken, otpToken })
38 .pipe(catchError(err => this.restExtractor.handleError(err)))
41 disableTwoFactor (options: {
43 currentPassword?: string
45 const { userId, currentPassword } = options
47 const url = UserService.BASE_USERS_URL + userId + '/two-factor/disable'
49 return this.authHttp.post(url, { currentPassword })
50 .pipe(catchError(err => this.restExtractor.handleError(err)))