2016-08-05 18:04:08 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
2017-01-13 12:16:00 +01:00
|
|
|
import 'rxjs/add/operator/catch';
|
|
|
|
import 'rxjs/add/operator/map';
|
2016-08-05 18:04:08 +02:00
|
|
|
|
2017-03-04 11:45:47 +01:00
|
|
|
import { AuthService } from '../../core';
|
|
|
|
import { AuthHttp } from '../auth';
|
|
|
|
import { RestExtractor } from '../rest';
|
2016-08-05 18:04:08 +02:00
|
|
|
|
|
|
|
@Injectable()
|
2017-03-04 11:45:47 +01:00
|
|
|
export class UserService {
|
2017-03-08 21:35:43 +01:00
|
|
|
static BASE_USERS_URL = '/api/v1/users/';
|
2016-08-05 18:04:08 +02:00
|
|
|
|
2016-08-23 16:54:21 +02:00
|
|
|
constructor(
|
|
|
|
private authHttp: AuthHttp,
|
|
|
|
private authService: AuthService,
|
|
|
|
private restExtractor: RestExtractor
|
|
|
|
) {}
|
2016-08-05 18:04:08 +02:00
|
|
|
|
2017-03-04 11:45:47 +01:00
|
|
|
checkTokenValidity() {
|
|
|
|
const url = UserService.BASE_USERS_URL + 'me';
|
|
|
|
|
|
|
|
// AuthHttp will redirect us to the login page if the oken is not valid anymore
|
|
|
|
this.authHttp.get(url).subscribe(() => { ; });
|
|
|
|
}
|
|
|
|
|
2016-08-05 18:04:08 +02:00
|
|
|
changePassword(newPassword: string) {
|
2017-03-04 11:45:47 +01:00
|
|
|
const url = UserService.BASE_USERS_URL + this.authService.getUser().id;
|
2016-08-05 18:04:08 +02:00
|
|
|
const body = {
|
|
|
|
password: newPassword
|
|
|
|
};
|
|
|
|
|
2016-08-23 16:54:21 +02:00
|
|
|
return this.authHttp.put(url, body)
|
|
|
|
.map(this.restExtractor.extractDataBool)
|
|
|
|
.catch((res) => this.restExtractor.handleError(res));
|
2016-08-05 18:04:08 +02:00
|
|
|
}
|
2017-04-06 21:21:03 +02:00
|
|
|
|
|
|
|
updateDetails(details: { displayNSFW: boolean }) {
|
|
|
|
const url = UserService.BASE_USERS_URL + this.authService.getUser().id;
|
|
|
|
|
|
|
|
return this.authHttp.put(url, details)
|
|
|
|
.map(this.restExtractor.extractDataBool)
|
|
|
|
.catch((res) => this.restExtractor.handleError(res));
|
|
|
|
}
|
2016-08-05 18:04:08 +02:00
|
|
|
}
|