]>
Commit | Line | Data |
---|---|---|
d92d070c C |
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' | |
6 | ||
7 | @Injectable() | |
8 | export class UserSignupService { | |
9 | constructor ( | |
10 | private authHttp: HttpClient, | |
11 | private restExtractor: RestExtractor, | |
12 | private userService: UserService | |
13 | ) { } | |
14 | ||
15 | signup (userCreate: UserRegister) { | |
16 | return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate) | |
17 | .pipe( | |
18 | tap(() => this.userService.setSignupInThisSession(true)), | |
19 | catchError(err => this.restExtractor.handleError(err)) | |
20 | ) | |
21 | } | |
22 | ||
23 | verifyEmail (userId: number, verificationString: string, isPendingEmail: boolean) { | |
24 | const url = `${UserService.BASE_USERS_URL}/${userId}/verify-email` | |
25 | const body = { | |
26 | verificationString, | |
27 | isPendingEmail | |
28 | } | |
29 | ||
30 | return this.authHttp.post(url, body) | |
31 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
32 | } | |
33 | ||
34 | askSendVerifyEmail (email: string) { | |
35 | const url = UserService.BASE_USERS_URL + '/ask-send-verify-email' | |
36 | ||
37 | return this.authHttp.post(url, { email }) | |
38 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
39 | } | |
40 | ||
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 | |
44 | ||
45 | return this.displayNameToUsername(newDisplayName) | |
46 | } | |
47 | ||
48 | private displayNameToUsername (displayName: string) { | |
49 | if (!displayName) return '' | |
50 | ||
51 | return displayName | |
52 | .toLowerCase() | |
53 | .replace(/\s/g, '_') | |
54 | .replace(/[^a-z0-9_.]/g, '') | |
55 | } | |
56 | } |