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