]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+signup/shared/signup.service.ts
Update angular
[github/Chocobozzz/PeerTube.git] / client / src / app / +signup / shared / signup.service.ts
CommitLineData
d92d070c
C
1import { catchError, tap } from 'rxjs/operators'
2import { HttpClient } from '@angular/common/http'
3import { Injectable } from '@angular/core'
4import { RestExtractor, UserService } from '@app/core'
9589907c 5import { UserRegister, UserRegistrationRequest } from '@shared/models'
d92d070c
C
6
7@Injectable()
9589907c
C
8export class SignupService {
9
d92d070c
C
10 constructor (
11 private authHttp: HttpClient,
12 private restExtractor: RestExtractor,
13 private userService: UserService
14 ) { }
15
9589907c 16 directSignup (userCreate: UserRegister) {
d92d070c
C
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
9589907c
C
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`
d92d070c
C
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
9589907c
C
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
d92d070c 61 askSendVerifyEmail (email: string) {
9589907c 62 const url = UserService.BASE_USERS_URL + 'ask-send-verify-email'
d92d070c
C
63
64 return this.authHttp.post(url, { email })
65 .pipe(catchError(err => this.restExtractor.handleError(err)))
66 }
67
9589907c
C
68 // ---------------------------------------------------------------------------
69
d92d070c
C
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}