]>
Commit | Line | Data |
---|---|---|
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' | |
6 | ||
7 | @Injectable() | |
8 | export class SignupService { | |
9 | ||
10 | constructor ( | |
11 | private authHttp: HttpClient, | |
12 | private restExtractor: RestExtractor, | |
13 | private userService: UserService | |
14 | ) { } | |
15 | ||
16 | directSignup (userCreate: UserRegister) { | |
17 | return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate) | |
18 | .pipe( | |
19 | tap(() => this.userService.setSignupInThisSession(true)), | |
20 | catchError(err => this.restExtractor.handleError(err)) | |
21 | ) | |
22 | } | |
23 | ||
24 | requestSignup (userCreate: UserRegistrationRequest) { | |
25 | return this.authHttp.post(UserService.BASE_USERS_URL + 'registrations/request', userCreate) | |
26 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
27 | } | |
28 | ||
29 | // --------------------------------------------------------------------------- | |
30 | ||
31 | verifyUserEmail (options: { | |
32 | userId: number | |
33 | verificationString: string | |
34 | isPendingEmail: boolean | |
35 | }) { | |
36 | const { userId, verificationString, isPendingEmail } = options | |
37 | ||
38 | const url = `${UserService.BASE_USERS_URL}${userId}/verify-email` | |
39 | const body = { | |
40 | verificationString, | |
41 | isPendingEmail | |
42 | } | |
43 | ||
44 | return this.authHttp.post(url, body) | |
45 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
46 | } | |
47 | ||
48 | verifyRegistrationEmail (options: { | |
49 | registrationId: number | |
50 | verificationString: string | |
51 | }) { | |
52 | const { registrationId, verificationString } = options | |
53 | ||
54 | const url = `${UserService.BASE_USERS_URL}registrations/${registrationId}/verify-email` | |
55 | const body = { verificationString } | |
56 | ||
57 | return this.authHttp.post(url, body) | |
58 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
59 | } | |
60 | ||
61 | askSendVerifyEmail (email: string) { | |
62 | const url = UserService.BASE_USERS_URL + 'ask-send-verify-email' | |
63 | ||
64 | return this.authHttp.post(url, { email }) | |
65 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
66 | } | |
67 | ||
68 | // --------------------------------------------------------------------------- | |
69 | ||
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 | |
73 | ||
74 | return this.displayNameToUsername(newDisplayName) | |
75 | } | |
76 | ||
77 | private displayNameToUsername (displayName: string) { | |
78 | if (!displayName) return '' | |
79 | ||
80 | return displayName | |
81 | .toLowerCase() | |
82 | .replace(/\s/g, '_') | |
83 | .replace(/[^a-z0-9_.]/g, '') | |
84 | } | |
85 | } |