mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-03 17:59:37 +02:00
Implement user requests autorizations in the client side
This commit is contained in:
parent
0c1cbbfe29
commit
1553e15d82
10 changed files with 122 additions and 33 deletions
|
@ -1,20 +1,23 @@
|
|||
import { Injectable } from 'angular2/core';
|
||||
import { Http, Response, Headers, URLSearchParams } from 'angular2/http';
|
||||
import { Http, Response, Headers, URLSearchParams, RequestOptions } from 'angular2/http';
|
||||
import { Observable, Subject } from 'rxjs/Rx';
|
||||
|
||||
import { AuthStatus } from '../models/authStatus';
|
||||
import { User } from '../models/user';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
loginChanged$ = this._loginChanged.asObservable();
|
||||
|
||||
private _loginChanged = new Subject<AuthStatus>();
|
||||
loginChanged$;
|
||||
|
||||
private _loginChanged;
|
||||
private _baseLoginUrl = '/api/v1/users/token';
|
||||
private _clientId = '56f055587305d40b21904240';
|
||||
private _clientSecret = 'megustalabanana';
|
||||
|
||||
constructor (private http: Http) {}
|
||||
constructor (private http: Http) {
|
||||
this._loginChanged = new Subject<AuthStatus>();
|
||||
this.loginChanged$ = this._loginChanged.asObservable();
|
||||
}
|
||||
|
||||
login(username: string, password: string) {
|
||||
let body = new URLSearchParams();
|
||||
|
@ -42,12 +45,46 @@ export class AuthService {
|
|||
// TODO make HTTP request
|
||||
}
|
||||
|
||||
getRequestHeader(): Headers {
|
||||
return new Headers({ 'Authorization': `${this.getTokenType()} ${this.getToken()}` });
|
||||
}
|
||||
|
||||
getAuthRequestOptions(): RequestOptions {
|
||||
return new RequestOptions({ headers: this.getRequestHeader() });
|
||||
}
|
||||
|
||||
getToken(): string {
|
||||
return localStorage.getItem('access_token');
|
||||
}
|
||||
|
||||
getTokenType(): string {
|
||||
return localStorage.getItem('token_type');
|
||||
}
|
||||
|
||||
getUser(): User {
|
||||
if (this.isLoggedIn() === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const user = User.load();
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
isLoggedIn(): boolean {
|
||||
if (this.getToken()) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
setStatus(status: AuthStatus) {
|
||||
this._loginChanged.next(status);
|
||||
}
|
||||
|
||||
private handleError (error: Response) {
|
||||
console.error(error);
|
||||
return Observable.throw(error.json().error || 'Server error');
|
||||
return Observable.throw(error.json() || { error: 'Server error' });
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue