PeerTube/client/src/app/shared/users/user.service.ts

59 lines
1.7 KiB
TypeScript
Raw Normal View History

2016-08-05 18:04:08 +02:00
import { Injectable } from '@angular/core';
2017-04-10 20:29:33 +02:00
import { Http } from '@angular/http';
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
import { AuthService } from '../../core';
import { AuthHttp } from '../auth';
import { RestExtractor } from '../rest';
2016-08-05 18:04:08 +02:00
@Injectable()
export class UserService {
2017-06-11 15:19:43 +02:00
static BASE_USERS_URL = API_URL + '/api/v1/users/';
2016-08-05 18:04:08 +02:00
constructor(
2017-04-10 20:29:33 +02:00
private http: Http,
private authHttp: AuthHttp,
private authService: AuthService,
private restExtractor: RestExtractor
) {}
2016-08-05 18:04:08 +02: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) {
const url = UserService.BASE_USERS_URL + this.authService.getUser().id;
2016-08-05 18:04:08 +02:00
const body = {
password: newPassword
};
return this.authHttp.put(url, body)
.map(this.restExtractor.extractDataBool)
.catch((res) => this.restExtractor.handleError(res));
2016-08-05 18:04:08 +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));
}
2017-04-10 20:29:33 +02:00
signup(username: string, password: string, email: string) {
const body = {
username,
email,
password
};
return this.http.post(UserService.BASE_USERS_URL + 'register', body)
.map(this.restExtractor.extractDataBool)
.catch(this.restExtractor.handleError);
}
2016-08-05 18:04:08 +02:00
}