aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+my-account/my-account-settings/my-account-two-factor/my-account-two-factor-button.component.ts
blob: 03b00e9335bf4bc799604ade6b7de5bc5c619b66 (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
import { Subject } from 'rxjs'
import { Component, Input, OnInit } from '@angular/core'
import { AuthService, ConfirmService, Notifier, User } from '@app/core'
import { TwoFactorService } from './two-factor.service'

@Component({
  selector: 'my-account-two-factor-button',
  templateUrl: './my-account-two-factor-button.component.html'
})
export class MyAccountTwoFactorButtonComponent implements OnInit {
  @Input() user: User = null
  @Input() userInformationLoaded: Subject<any>

  twoFactorEnabled = false

  constructor (
    private notifier: Notifier,
    private twoFactorService: TwoFactorService,
    private confirmService: ConfirmService,
    private auth: AuthService
  ) {
  }

  ngOnInit () {
    this.userInformationLoaded.subscribe(() => {
      this.twoFactorEnabled = this.user.twoFactorEnabled
    })
  }

  async disableTwoFactor () {
    const message = $localize`Are you sure you want to disable two factor authentication of your account?`

    const { confirmed, password } = await this.confirmService.confirmWithPassword(message, $localize`Disable two factor`)
    if (confirmed === false) return

    this.twoFactorService.disableTwoFactor({ userId: this.user.id, currentPassword: password })
      .subscribe({
        next: () => {
          this.twoFactorEnabled = false

          this.auth.refreshUserInformation()

          this.notifier.success($localize`Two factor authentication disabled`)
        },

        error: err => this.notifier.error(err.message)
      })
  }
}