1 import { Injectable } from '@angular/core'
2 import { Http } from '@angular/http'
3 import 'rxjs/add/operator/catch'
4 import 'rxjs/add/operator/map'
6 import { AuthService } from '../../core'
7 import { AuthHttp } from '../auth'
8 import { RestExtractor } from '../rest'
11 export class UserService {
12 static BASE_USERS_URL = API_URL + '/api/v1/users/'
16 private authHttp: AuthHttp,
17 private authService: AuthService,
18 private restExtractor: RestExtractor
21 checkTokenValidity () {
22 const url = UserService.BASE_USERS_URL + 'me'
24 // AuthHttp will redirect us to the login page if the oken is not valid anymore
25 this.authHttp.get(url).subscribe()
28 changePassword (newPassword: string) {
29 const url = UserService.BASE_USERS_URL + this.authService.getUser().id
34 return this.authHttp.put(url, body)
35 .map(this.restExtractor.extractDataBool)
36 .catch((res) => this.restExtractor.handleError(res))
39 updateDetails (details: { displayNSFW: boolean }) {
40 const url = UserService.BASE_USERS_URL + this.authService.getUser().id
42 return this.authHttp.put(url, details)
43 .map(this.restExtractor.extractDataBool)
44 .catch((res) => this.restExtractor.handleError(res))
47 signup (username: string, password: string, email: string) {
54 return this.http.post(UserService.BASE_USERS_URL + 'register', body)
55 .map(this.restExtractor.extractDataBool)
56 .catch(this.restExtractor.handleError)