[SDC-29] catalog 1707 rebase commit.
[sdc.git] / catalog-ui / src / app / ng2 / services / authentication.service.ts
1 import { Injectable } from '@angular/core';
2 import { sdc2Config } from './../../../main';
3 import {IAppConfigurtaion, ICookie} from "../../models/app-config";
4 import {Response, Headers, RequestOptions, Http} from '@angular/http';
5 import {Cookie2Service} from "./cookie.service";
6 import { Observable } from 'rxjs/Observable';
7
8 @Injectable()
9 export class AuthenticationService {
10
11     private cookieService:Cookie2Service;
12     private http:Http;
13
14     constructor(cookieService:Cookie2Service, http: Http) {
15         this.cookieService = cookieService;
16         this.http = http;
17     }
18
19     private getAuthHeaders():any {
20         let cookie:ICookie = sdc2Config.cookie;
21         let authHeaders:any = {};
22         authHeaders[cookie.userFirstName] = this.cookieService.getFirstName();
23         authHeaders[cookie.userLastName] = this.cookieService.getLastName();
24         authHeaders[cookie.userEmail] = this.cookieService.getEmail();
25         authHeaders[cookie.userIdSuffix] = this.cookieService.getUserId();
26         return authHeaders;
27     }
28
29     public authenticate(): Observable<JSON> {
30         let options = new RequestOptions({
31             headers: new Headers(this.getAuthHeaders())
32         });
33
34         let authUrl = sdc2Config.api.root + sdc2Config.api.GET_user_authorize;
35         return this.http
36             .get(authUrl, options)
37             .map((res: Response) => res.json());
38     }
39
40 }