<commit_before>// import { Router, ActivatedRoute } from '@angular/router';
import { Injectable } from '@angular/core';
import { HttpModule, Headers, Response, Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { CookieService } from '../../node_modules/angular2-cookie/services/cookies.service';

@Injectable()
export class AuthService {

    private currentLoggedUser = {
        access_token: '',
        expires_in: '',
        token_type: '',
        userName: '',
    };

    private cookieKey = 'authKey';
    private baseApiUrl: string = 'http://localhost:3200/api/';
    private baseUrl: string = 'http://localhost:3200/';
    private applicationJson: string = 'application/json';

    private jsonHeaders: Headers = new Headers({
        'Content-type': this.applicationJson,
        'Accept': this.applicationJson
    });

    private formHeaders: Headers = new Headers({
        'Content-type': 'application/x-www-form-urlencoded',
    });

    constructor(
        private http: Http,
        private cookieService: CookieService,
        // private route: ActivatedRoute,
        // private router: Router,
    ) { }

    getAuthorizationHeader(): Headers {
        let token = this.cookieService.get(this.cookieKey);

        return new Headers({
            'Content-type': this.applicationJson,
            'Authorization': `Bearer ${token}`
        });
    }

    isLoggedIn(): boolean {
        return !!this.cookieService.get(this.cookieKey);
    }

    //
    getLoggedUser(): any {
        return this.currentLoggedUser;
    }

    logout(): void {
        console.log('logout');
        this.cookieService.remove(this.cookieKey);
        this.currentLoggedUser.access_token = '';
        this.currentLoggedUser.expires_in = '';
        this.currentLoggedUser.token_type = '';
        this.currentLoggedUser.userName = '';
    }

    register(user: any): Observable<any>  {
        const path = `${this.baseApiUrl}account/register`;
        return this.http.post(path, user, { headers: this.jsonHeaders })
                .map((resp: Response) => {
                    // There are no response from register.
                    return Observable.empty;
                })
                .catch((error: Response) => {

                   let errorMessages: String[] = [];
                   let modelState = error.json().ModelState;
                   Object.getOwnPropertyNames(modelState).forEach((prop) => {
                        errorMessages.push(modelState[prop].toString());
                   });
                   return Observable.throw(errorMessages.toString());
                });
    }

    login(userName: string, password: string): Observable<Response> {
        let path = `${this.baseUrl}token`;
        let data = `grant_type=password&password=${password}&userName=${userName}`;

        return this.http.post(path, data, { headers: this.formHeaders })
            .map((resp: Response) => {
                let jsonData = resp.json();
                let expires: Date = new Date(jsonData['.expires']);
                this.currentLoggedUser = jsonData;

                this.cookieService.put(this.cookieKey, jsonData.access_token, { expires : expires } );
                // this.router.navigate(['/']);
                return resp;
            })<commit_msg>Auth service returns correct info abount current logged user.<commit_after>// import { Router, ActivatedRoute } from '@angular/router';
import { Injectable } from '@angular/core';
import { HttpModule, Headers, Response, Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { CookieService } from '../../node_modules/angular2-cookie/services/cookies.service';

@Injectable()
export class AuthService {

    private cookieKey = 'authKey';
    private baseApiUrl: string = 'http://localhost:3200/api/';
    private baseUrl: string = 'http://localhost:3200/';
    private applicationJson: string = 'application/json';

    private jsonHeaders: Headers = new Headers({
        'Content-type': this.applicationJson,
        'Accept': this.applicationJson
    });

    private formHeaders: Headers = new Headers({
        'Content-type': 'application/x-www-form-urlencoded',
    });

    constructor(
        private http: Http,
        private cookieService: CookieService,
        // private route: ActivatedRoute,
        // private router: Router,
    ) { }

    getAuthorizationHeader(): Headers {

        let token: string = JSON.parse(this.cookieService.get(this.cookieKey))['access_token'];
        return new Headers({
            'Content-type': this.applicationJson,
            'Authorization': `Bearer ${token}`
        });
    }

    isLoggedIn(): boolean {
        return !!this.cookieService.get(this.cookieKey);
    }

    getLoggedUser(): any {
        if (this.isLoggedIn()) {
            let value: string = this.cookieService.get(this.cookieKey);
            return JSON.parse(value);
        }

        return null;
    }

    logout(): void {
        this.cookieService.remove(this.cookieKey);
    }

    register(user: any): Observable<any> {
        const path = `${this.baseApiUrl}account/register`;
        return this.http.post(path, user, { headers: this.jsonHeaders })
            .map((resp: Response) => {
                // There are no response from register.
                return Observable.empty;
            })
            .catch((error: Response) => {

                let errorMessages: String[] = [];
                let modelState = error.json().ModelState;
                Object.getOwnPropertyNames(modelState).forEach((prop) => {
                    errorMessages.push(modelState[prop].toString());
                });
                return Observable.throw(errorMessages.toString());
            });
    }

    login(userName: string, password: string): Observable<Response> {
        let path = `${this.baseUrl}token`;
        let data = `grant_type=password&password=${password}&userName=${userName}`;

        return this.http.post(path, data, { headers: this.formHeaders })
            .map((resp: Response) => {
                let jsonData = resp.json();
                let expires: Date = new Date(jsonData['.expires']);
                let value: string = JSON.stringify(jsonData);

                this.cookieService.put(this.cookieKey, value, { expires: expires });
                // this.router.navigate(['/']);
                return resp;
            })
{'fraction_non_alphanumeric': 0.1329319129226494, 'fraction_numerical': 0.002779064381658175, 'mean_word_length': 2.2357642357642358, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4422404', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1697, 'n_words': 360}