1 import { catchError, tap } 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 { UserRegister, UserRegistrationRequest } from '@shared/models'
8 export class SignupService {
11 private authHttp: HttpClient,
12 private restExtractor: RestExtractor,
13 private userService: UserService
16 directSignup (userCreate: UserRegister) {
17 return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
19 tap(() => this.userService.setSignupInThisSession(true)),
20 catchError(err => this.restExtractor.handleError(err))
24 requestSignup (userCreate: UserRegistrationRequest) {
25 return this.authHttp.post(UserService.BASE_USERS_URL + 'registrations/request', userCreate)
26 .pipe(catchError(err => this.restExtractor.handleError(err)))
29 // ---------------------------------------------------------------------------
31 verifyUserEmail (options: {
33 verificationString: string
34 isPendingEmail: boolean
36 const { userId, verificationString, isPendingEmail } = options
38 const url = `${UserService.BASE_USERS_URL}${userId}/verify-email`
44 return this.authHttp.post(url, body)
45 .pipe(catchError(res => this.restExtractor.handleError(res)))
48 verifyRegistrationEmail (options: {
49 registrationId: number
50 verificationString: string
52 const { registrationId, verificationString } = options
54 const url = `${UserService.BASE_USERS_URL}registrations/${registrationId}/verify-email`
55 const body = { verificationString }
57 return this.authHttp.post(url, body)
58 .pipe(catchError(res => this.restExtractor.handleError(res)))
61 askSendVerifyEmail (email: string) {
62 const url = UserService.BASE_USERS_URL + 'ask-send-verify-email'
64 return this.authHttp.post(url, { email })
65 .pipe(catchError(err => this.restExtractor.handleError(err)))
68 // ---------------------------------------------------------------------------
70 getNewUsername (oldDisplayName: string, newDisplayName: string, currentUsername: string) {
71 // Don't update display name, the user seems to have changed it
72 if (this.displayNameToUsername(oldDisplayName) !== currentUsername) return currentUsername
74 return this.displayNameToUsername(newDisplayName)
77 private displayNameToUsername (displayName: string) {
78 if (!displayName) return ''
83 .replace(/[^a-z0-9_.]/g, '')