]>
Commit | Line | Data |
---|---|---|
df98563e C |
1 | import { Injectable } from '@angular/core' |
2 | import { Http } from '@angular/http' | |
3 | import 'rxjs/add/operator/catch' | |
4 | import 'rxjs/add/operator/map' | |
629d8d6f | 5 | |
df98563e C |
6 | import { AuthService } from '../../core' |
7 | import { AuthHttp } from '../auth' | |
8 | import { RestExtractor } from '../rest' | |
629d8d6f C |
9 | |
10 | @Injectable() | |
e2a2d6c8 | 11 | export class UserService { |
df98563e | 12 | static BASE_USERS_URL = API_URL + '/api/v1/users/' |
629d8d6f | 13 | |
df98563e | 14 | constructor ( |
a184c71b | 15 | private http: Http, |
de59c48f C |
16 | private authHttp: AuthHttp, |
17 | private authService: AuthService, | |
18 | private restExtractor: RestExtractor | |
19 | ) {} | |
629d8d6f | 20 | |
df98563e C |
21 | checkTokenValidity () { |
22 | const url = UserService.BASE_USERS_URL + 'me' | |
e2a2d6c8 C |
23 | |
24 | // AuthHttp will redirect us to the login page if the oken is not valid anymore | |
df98563e | 25 | this.authHttp.get(url).subscribe() |
e2a2d6c8 C |
26 | } |
27 | ||
df98563e C |
28 | changePassword (newPassword: string) { |
29 | const url = UserService.BASE_USERS_URL + this.authService.getUser().id | |
629d8d6f C |
30 | const body = { |
31 | password: newPassword | |
df98563e | 32 | } |
629d8d6f | 33 | |
de59c48f C |
34 | return this.authHttp.put(url, body) |
35 | .map(this.restExtractor.extractDataBool) | |
df98563e | 36 | .catch((res) => this.restExtractor.handleError(res)) |
629d8d6f | 37 | } |
af5e743b | 38 | |
df98563e C |
39 | updateDetails (details: { displayNSFW: boolean }) { |
40 | const url = UserService.BASE_USERS_URL + this.authService.getUser().id | |
af5e743b C |
41 | |
42 | return this.authHttp.put(url, details) | |
43 | .map(this.restExtractor.extractDataBool) | |
df98563e | 44 | .catch((res) => this.restExtractor.handleError(res)) |
af5e743b | 45 | } |
a184c71b | 46 | |
df98563e | 47 | signup (username: string, password: string, email: string) { |
a184c71b C |
48 | const body = { |
49 | username, | |
50 | email, | |
51 | password | |
df98563e | 52 | } |
a184c71b C |
53 | |
54 | return this.http.post(UserService.BASE_USERS_URL + 'register', body) | |
55 | .map(this.restExtractor.extractDataBool) | |
df98563e | 56 | .catch(this.restExtractor.handleError) |
a184c71b | 57 | } |
629d8d6f | 58 | } |