diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-08-05 18:04:08 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-08-05 18:04:08 +0200 |
commit | 629d8d6f70cf83b55011dff53bfe1c4a95ac3433 (patch) | |
tree | 9d5a145609d9c693ddacfbc42ae75ca3c841aef0 /client/src/app/shared/auth/auth.service.ts | |
parent | 99a64bfed25e45547df3045cf249bc895e6f220b (diff) | |
download | PeerTube-629d8d6f70cf83b55011dff53bfe1c4a95ac3433.tar.gz PeerTube-629d8d6f70cf83b55011dff53bfe1c4a95ac3433.tar.zst PeerTube-629d8d6f70cf83b55011dff53bfe1c4a95ac3433.zip |
Client: implement password change
Diffstat (limited to 'client/src/app/shared/auth/auth.service.ts')
-rw-r--r-- | client/src/app/shared/auth/auth.service.ts | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/client/src/app/shared/auth/auth.service.ts b/client/src/app/shared/auth/auth.service.ts index 6a5b19ffe..24d1a4fa2 100644 --- a/client/src/app/shared/auth/auth.service.ts +++ b/client/src/app/shared/auth/auth.service.ts | |||
@@ -10,6 +10,7 @@ import { User } from './user.model'; | |||
10 | export class AuthService { | 10 | export class AuthService { |
11 | private static BASE_CLIENT_URL = '/api/v1/clients/local'; | 11 | private static BASE_CLIENT_URL = '/api/v1/clients/local'; |
12 | private static BASE_TOKEN_URL = '/api/v1/users/token'; | 12 | private static BASE_TOKEN_URL = '/api/v1/users/token'; |
13 | private static BASE_USER_INFORMATIONS_URL = '/api/v1/users/me'; | ||
13 | 14 | ||
14 | loginChangedSource: Observable<AuthStatus>; | 15 | loginChangedSource: Observable<AuthStatus>; |
15 | 16 | ||
@@ -99,6 +100,7 @@ export class AuthService { | |||
99 | res.username = username; | 100 | res.username = username; |
100 | return res; | 101 | return res; |
101 | }) | 102 | }) |
103 | .flatMap(res => this.fetchUserInformations(res)) | ||
102 | .map(res => this.handleLogin(res)) | 104 | .map(res => this.handleLogin(res)) |
103 | .catch(this.handleError); | 105 | .catch(this.handleError); |
104 | } | 106 | } |
@@ -136,31 +138,50 @@ export class AuthService { | |||
136 | .catch(this.handleError); | 138 | .catch(this.handleError); |
137 | } | 139 | } |
138 | 140 | ||
139 | private setStatus(status: AuthStatus) { | 141 | private fetchUserInformations (obj: any) { |
140 | this.loginChanged.next(status); | 142 | // Do not call authHttp here to avoid circular dependencies headaches |
143 | |||
144 | const headers = new Headers(); | ||
145 | headers.set('Authorization', `Bearer ${obj.access_token}`); | ||
146 | |||
147 | return this.http.get(AuthService.BASE_USER_INFORMATIONS_URL, { headers }) | ||
148 | .map(res => res.json()) | ||
149 | .map(res => { | ||
150 | obj.id = res.id; | ||
151 | obj.role = res.role; | ||
152 | return obj; | ||
153 | } | ||
154 | ); | ||
155 | } | ||
156 | |||
157 | private handleError (error: Response) { | ||
158 | console.error(error); | ||
159 | return Observable.throw(error.json() || { error: 'Server error' }); | ||
141 | } | 160 | } |
142 | 161 | ||
143 | private handleLogin (obj: any) { | 162 | private handleLogin (obj: any) { |
163 | const id = obj.id; | ||
144 | const username = obj.username; | 164 | const username = obj.username; |
165 | const role = obj.role; | ||
145 | const hash_tokens = { | 166 | const hash_tokens = { |
146 | access_token: obj.access_token, | 167 | access_token: obj.access_token, |
147 | token_type: obj.token_type, | 168 | token_type: obj.token_type, |
148 | refresh_token: obj.refresh_token | 169 | refresh_token: obj.refresh_token |
149 | }; | 170 | }; |
150 | 171 | ||
151 | this.user = new User(username, hash_tokens); | 172 | this.user = new User(id, username, role, hash_tokens); |
152 | this.user.save(); | 173 | this.user.save(); |
153 | 174 | ||
154 | this.setStatus(AuthStatus.LoggedIn); | 175 | this.setStatus(AuthStatus.LoggedIn); |
155 | } | 176 | } |
156 | 177 | ||
157 | private handleError (error: Response) { | ||
158 | console.error(error); | ||
159 | return Observable.throw(error.json() || { error: 'Server error' }); | ||
160 | } | ||
161 | |||
162 | private handleRefreshToken (obj: any) { | 178 | private handleRefreshToken (obj: any) { |
163 | this.user.refreshTokens(obj.access_token, obj.refresh_token); | 179 | this.user.refreshTokens(obj.access_token, obj.refresh_token); |
164 | this.user.save(); | 180 | this.user.save(); |
165 | } | 181 | } |
182 | |||
183 | private setStatus(status: AuthStatus) { | ||
184 | this.loginChanged.next(status); | ||
185 | } | ||
186 | |||
166 | } | 187 | } |