2016-03-22 15:51:54 +01:00
|
|
|
import { Injectable } from 'angular2/core';
|
|
|
|
import { Http, Response, Headers, URLSearchParams } from 'angular2/http';
|
|
|
|
import { Observable, Subject } from 'rxjs/Rx';
|
|
|
|
|
|
|
|
import { AuthStatus } from '../models/authStatus';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class AuthService {
|
2016-04-08 20:58:07 +02:00
|
|
|
loginChanged$ = this._loginChanged.asObservable();
|
|
|
|
|
2016-03-22 15:51:54 +01:00
|
|
|
private _loginChanged = new Subject<AuthStatus>();
|
|
|
|
|
|
|
|
private _baseLoginUrl = '/api/v1/users/token';
|
|
|
|
private _clientId = '56f055587305d40b21904240';
|
|
|
|
private _clientSecret = 'megustalabanana';
|
|
|
|
|
|
|
|
constructor (private http: Http) {}
|
|
|
|
|
|
|
|
login(username: string, password: string) {
|
|
|
|
let body = new URLSearchParams();
|
|
|
|
body.set('client_id', this._clientId);
|
|
|
|
body.set('client_secret', this._clientSecret);
|
|
|
|
body.set('response_type', 'code');
|
|
|
|
body.set('grant_type', 'password');
|
|
|
|
body.set('scope', 'upload');
|
|
|
|
body.set('username', username);
|
|
|
|
body.set('password', password);
|
|
|
|
|
|
|
|
let headers = new Headers();
|
|
|
|
headers.append('Content-Type', 'application/x-www-form-urlencoded');
|
|
|
|
|
|
|
|
let options = {
|
|
|
|
headers: headers
|
2016-04-08 20:58:07 +02:00
|
|
|
};
|
2016-03-22 15:51:54 +01:00
|
|
|
|
|
|
|
return this.http.post(this._baseLoginUrl, body.toString(), options)
|
|
|
|
.map(res => res.json())
|
|
|
|
.catch(this.handleError);
|
|
|
|
}
|
|
|
|
|
|
|
|
logout() {
|
|
|
|
// TODO make HTTP request
|
|
|
|
}
|
|
|
|
|
|
|
|
setStatus(status: AuthStatus) {
|
|
|
|
this._loginChanged.next(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
private handleError (error: Response) {
|
|
|
|
console.error(error);
|
|
|
|
return Observable.throw(error.json().error || 'Server error');
|
|
|
|
}
|
|
|
|
}
|