aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+signup/+verify-account/verify-account-email/verify-account-email.component.ts
blob: faf66339110ad1bd13a19d6b7e06651de6bc51e2 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { Component, OnInit } from '@angular/core'
import { ActivatedRoute } from '@angular/router'
import { SignupService } from '@app/+signup/shared/signup.service'
import { AuthService, Notifier, ServerService } from '@app/core'

@Component({
  selector: 'my-verify-account-email',
  templateUrl: './verify-account-email.component.html'
})

export class VerifyAccountEmailComponent implements OnInit {
  success = false
  failed = false
  isPendingEmail = false

  requiresApproval: boolean
  loaded = false

  private userId: number
  private registrationId: number
  private verificationString: string

  constructor (
    private signupService: SignupService,
    private server: ServerService,
    private authService: AuthService,
    private notifier: Notifier,
    private route: ActivatedRoute
  ) {
  }

  get instanceName () {
    return this.server.getHTMLConfig().instance.name
  }

  ngOnInit () {
    const queryParams = this.route.snapshot.queryParams

    this.server.getConfig().subscribe(config => {
      this.requiresApproval = config.signup.requiresApproval

      this.loaded = true
    })

    this.userId = queryParams['userId']
    this.registrationId = queryParams['registrationId']

    this.verificationString = queryParams['verificationString']

    this.isPendingEmail = queryParams['isPendingEmail'] === 'true'

    if (!this.verificationString) {
      this.notifier.error($localize`Unable to find verification string in URL query.`)
      return
    }

    if (!this.userId && !this.registrationId) {
      this.notifier.error($localize`Unable to find user id or registration id in URL query.`)
      return
    }

    this.verifyEmail()
  }

  isRegistrationRequest () {
    return !!this.registrationId
  }

  displaySignupSuccess () {
    if (!this.success) return false
    if (!this.isRegistrationRequest() && this.isPendingEmail) return false

    return true
  }

  verifyEmail () {
    if (this.isRegistrationRequest()) {
      return this.verifyRegistrationEmail()
    }

    return this.verifyUserEmail()
  }

  private verifyUserEmail () {
    const options = {
      userId: this.userId,
      verificationString: this.verificationString,
      isPendingEmail: this.isPendingEmail
    }

    this.signupService.verifyUserEmail(options)
      .subscribe({
        next: () => {
          if (this.authService.isLoggedIn()) {
            this.authService.refreshUserInformation()
          }

          this.success = true
        },

        error: err => {
          this.failed = true

          this.notifier.error(err.message)
        }
      })
  }

  private verifyRegistrationEmail () {
    const options = {
      registrationId: this.registrationId,
      verificationString: this.verificationString
    }

    this.signupService.verifyRegistrationEmail(options)
      .subscribe({
        next: () => {
          this.success = true
        },

        error: err => {
          this.failed = true

          this.notifier.error(err.message)
        }
      })
  }
}