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