add user account email verificiation (#977)
[oweals/peertube.git] / client / src / app / shared / users / user.service.ts
1 import { catchError, map } from 'rxjs/operators'
2 import { HttpClient } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { UserCreate, UserUpdateMe, UserVideoQuota } from '../../../../../shared'
5 import { environment } from '../../../environments/environment'
6 import { RestExtractor } from '../rest'
7 import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
8
9 @Injectable()
10 export class UserService {
11   static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/'
12
13   constructor (
14     private authHttp: HttpClient,
15     private restExtractor: RestExtractor
16   ) {
17   }
18
19   changePassword (newPassword: string) {
20     const url = UserService.BASE_USERS_URL + 'me'
21     const body: UserUpdateMe = {
22       password: newPassword
23     }
24
25     return this.authHttp.put(url, body)
26                .pipe(
27                  map(this.restExtractor.extractDataBool),
28                  catchError(err => this.restExtractor.handleError(err))
29                )
30   }
31
32   updateMyProfile (profile: UserUpdateMe) {
33     const url = UserService.BASE_USERS_URL + 'me'
34
35     return this.authHttp.put(url, profile)
36                .pipe(
37                  map(this.restExtractor.extractDataBool),
38                  catchError(err => this.restExtractor.handleError(err))
39                )
40   }
41
42   deleteMe () {
43     const url = UserService.BASE_USERS_URL + 'me'
44
45     return this.authHttp.delete(url)
46                .pipe(
47                  map(this.restExtractor.extractDataBool),
48                  catchError(err => this.restExtractor.handleError(err))
49                )
50   }
51
52   changeAvatar (avatarForm: FormData) {
53     const url = UserService.BASE_USERS_URL + 'me/avatar/pick'
54
55     return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
56                .pipe(catchError(err => this.restExtractor.handleError(err)))
57   }
58
59   signup (userCreate: UserCreate) {
60     return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
61                .pipe(
62                  map(this.restExtractor.extractDataBool),
63                  catchError(err => this.restExtractor.handleError(err))
64                )
65   }
66
67   getMyVideoQuotaUsed () {
68     const url = UserService.BASE_USERS_URL + '/me/video-quota-used'
69
70     return this.authHttp.get<UserVideoQuota>(url)
71                .pipe(catchError(err => this.restExtractor.handleError(err)))
72   }
73
74   askResetPassword (email: string) {
75     const url = UserService.BASE_USERS_URL + '/ask-reset-password'
76
77     return this.authHttp.post(url, { email })
78                .pipe(
79                  map(this.restExtractor.extractDataBool),
80                  catchError(err => this.restExtractor.handleError(err))
81                )
82   }
83
84   resetPassword (userId: number, verificationString: string, password: string) {
85     const url = `${UserService.BASE_USERS_URL}/${userId}/reset-password`
86     const body = {
87       verificationString,
88       password
89     }
90
91     return this.authHttp.post(url, body)
92                .pipe(
93                  map(this.restExtractor.extractDataBool),
94                  catchError(res => this.restExtractor.handleError(res))
95                )
96   }
97
98   verifyEmail (userId: number, verificationString: string) {
99     const url = `${UserService.BASE_USERS_URL}/${userId}/verify-email`
100     const body = {
101       verificationString
102     }
103
104     return this.authHttp.post(url, body)
105                .pipe(
106                  map(this.restExtractor.extractDataBool),
107                  catchError(res => this.restExtractor.handleError(res))
108                )
109   }
110
111   askSendVerifyEmail (email: string) {
112     const url = UserService.BASE_USERS_URL + '/ask-send-verify-email'
113
114     return this.authHttp.post(url, { email })
115                .pipe(
116                  map(this.restExtractor.extractDataBool),
117                  catchError(err => this.restExtractor.handleError(err))
118                )
119   }
120 }