aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+my-account/my-account-settings/my-account-two-factor/two-factor.service.ts
blob: c0e5ac49271073d2e054b3e192b96d38ce04e30f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { catchError } from 'rxjs/operators'
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { RestExtractor, UserService } from '@app/core'
import { TwoFactorEnableResult } from '@shared/models'

@Injectable()
export class TwoFactorService {
  constructor (
    private authHttp: HttpClient,
    private restExtractor: RestExtractor
  ) { }

  // ---------------------------------------------------------------------------

  requestTwoFactor (options: {
    userId: number
    currentPassword: string
  }) {
    const { userId, currentPassword } = options

    const url = UserService.BASE_USERS_URL + userId + '/two-factor/request'

    return this.authHttp.post<TwoFactorEnableResult>(url, { currentPassword })
    .pipe(catchError(err => this.restExtractor.handleError(err)))
  }

  confirmTwoFactorRequest (options: {
    userId: number
    requestToken: string
    otpToken: string
  }) {
    const { userId, requestToken, otpToken } = options

    const url = UserService.BASE_USERS_URL + userId + '/two-factor/confirm-request'

    return this.authHttp.post(url, { requestToken, otpToken })
    .pipe(catchError(err => this.restExtractor.handleError(err)))
  }

  disableTwoFactor (options: {
    userId: number
    currentPassword: string
  }) {
    const { userId, currentPassword } = options

    const url = UserService.BASE_USERS_URL + userId + '/two-factor/disable'

    return this.authHttp.post(url, { currentPassword })
      .pipe(catchError(err => this.restExtractor.handleError(err)))
  }
}