aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+signup/shared/signup.service.ts
blob: f647298be47cead9766f4ca442e725735e0102e3 (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
import { catchError, tap } from 'rxjs/operators'
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { RestExtractor, UserService } from '@app/core'
import { UserRegister, UserRegistrationRequest } from '@shared/models'

@Injectable()
export class SignupService {

  constructor (
    private authHttp: HttpClient,
    private restExtractor: RestExtractor,
    private userService: UserService
  ) { }

  directSignup (userCreate: UserRegister) {
    return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
               .pipe(
                 tap(() => this.userService.setSignupInThisSession(true)),
                 catchError(err => this.restExtractor.handleError(err))
               )
  }

  requestSignup (userCreate: UserRegistrationRequest) {
    return this.authHttp.post(UserService.BASE_USERS_URL + 'registrations/request', userCreate)
               .pipe(catchError(err => this.restExtractor.handleError(err)))
  }

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

  verifyUserEmail (options: {
    userId: number
    verificationString: string
    isPendingEmail: boolean
  }) {
    const { userId, verificationString, isPendingEmail } = options

    const url = `${UserService.BASE_USERS_URL}${userId}/verify-email`
    const body = {
      verificationString,
      isPendingEmail
    }

    return this.authHttp.post(url, body)
               .pipe(catchError(res => this.restExtractor.handleError(res)))
  }

  verifyRegistrationEmail (options: {
    registrationId: number
    verificationString: string
  }) {
    const { registrationId, verificationString } = options

    const url = `${UserService.BASE_USERS_URL}registrations/${registrationId}/verify-email`
    const body = { verificationString }

    return this.authHttp.post(url, body)
               .pipe(catchError(res => this.restExtractor.handleError(res)))
  }

  askSendVerifyEmail (email: string) {
    const url = UserService.BASE_USERS_URL + 'ask-send-verify-email'

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

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

  getNewUsername (oldDisplayName: string, newDisplayName: string, currentUsername: string) {
    // Don't update display name, the user seems to have changed it
    if (this.displayNameToUsername(oldDisplayName) !== currentUsername) return currentUsername

    return this.displayNameToUsername(newDisplayName)
  }

  private displayNameToUsername (displayName: string) {
    if (!displayName) return ''

    return displayName
      .toLowerCase()
      .replace(/\s/g, '_')
      .replace(/[^a-z0-9_.]/g, '')
  }
}