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 } from '@shared/models'
8 export class UserSignupService {
10 private authHttp: HttpClient,
11 private restExtractor: RestExtractor,
12 private userService: UserService
15 signup (userCreate: UserRegister) {
16 return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
18 tap(() => this.userService.setSignupInThisSession(true)),
19 catchError(err => this.restExtractor.handleError(err))
23 verifyEmail (userId: number, verificationString: string, isPendingEmail: boolean) {
24 const url = `${UserService.BASE_USERS_URL}/${userId}/verify-email`
30 return this.authHttp.post(url, body)
31 .pipe(catchError(res => this.restExtractor.handleError(res)))
34 askSendVerifyEmail (email: string) {
35 const url = UserService.BASE_USERS_URL + '/ask-send-verify-email'
37 return this.authHttp.post(url, { email })
38 .pipe(catchError(err => this.restExtractor.handleError(err)))
41 getNewUsername (oldDisplayName: string, newDisplayName: string, currentUsername: string) {
42 // Don't update display name, the user seems to have changed it
43 if (this.displayNameToUsername(oldDisplayName) !== currentUsername) return currentUsername
45 return this.displayNameToUsername(newDisplayName)
48 private displayNameToUsername (displayName: string) {
49 if (!displayName) return ''
54 .replace(/[^a-z0-9_.]/g, '')