1
|
import {Injectable} from '@angular/core';
|
2
|
import {Http, Response, Headers, RequestOptions} from '@angular/http';
|
3
|
import {Observable} from 'rxjs/Observable';
|
4
|
import 'rxjs/add/observable/of';
|
5
|
import 'rxjs/add/operator/do';
|
6
|
import 'rxjs/add/operator/share';
|
7
|
|
8
|
import { CacheService } from '../shared/cache.service';
|
9
|
import {PersonInfo} from '../utils/entities/personInfo';
|
10
|
import {OpenaireProperties} from '../utils/properties/openaireProperties';
|
11
|
import {User} from '../utils/entities/user';
|
12
|
|
13
|
@Injectable()
|
14
|
export class LoginService {
|
15
|
|
16
|
constructor(private http: Http, public _cache: CacheService) {}
|
17
|
|
18
|
authenticate (/*user: User*/username: string, password: string):any {
|
19
|
/*
|
20
|
let headers = new Headers({ 'Content-Type': 'application/json' });
|
21
|
let options = new RequestOptions({ headers: headers });
|
22
|
|
23
|
return this.http.post(this.heroesUrl, { name }, options)
|
24
|
.map(this.extractData)
|
25
|
.catch(this.handleError);
|
26
|
*/
|
27
|
let url = OpenaireProperties.getLoginAPIURL()+username+"&password="+password;
|
28
|
let key = url;
|
29
|
if (this._cache.has(key)) {
|
30
|
return Observable.of(this._cache.get(key));
|
31
|
}
|
32
|
return this.http.get(url)
|
33
|
.map(res => <any> res.json())
|
34
|
.map(res => this.parseUserInfo(res['data']))
|
35
|
.do(res => {
|
36
|
this._cache.set(key, res);
|
37
|
});
|
38
|
|
39
|
}
|
40
|
|
41
|
private parseUserInfo(data: any/*, user: User*/): User {
|
42
|
var user: User = new User();
|
43
|
|
44
|
user.username = data.username;
|
45
|
user.email = data.email;
|
46
|
user.id = data.id;
|
47
|
user.fullname = data.fullname;
|
48
|
|
49
|
localStorage.setItem("user", JSON.stringify(user));
|
50
|
|
51
|
return user;
|
52
|
}
|
53
|
}
|