1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\r
3 # Licensed under the Apache License, Version 2.0 (the "License"); #
\r
4 # you may not use this file except in compliance with the License. #
\r
5 # You may obtain a copy of the License at #
\r
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\r
9 # Unless required by applicable law or agreed to in writing, software #
\r
10 # distributed under the License is distributed on an "AS IS" BASIS, #
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
\r
12 # See the License for the specific language governing permissions and #
\r
13 # limitations under the License. #
\r
14 ##############################################################################*/
\r
17 import { Injectable } from '@angular/core';
\r
18 import { CookieService } from 'ngx-cookie-service';
\r
19 import { HttpClient, HttpHeaders } from '@angular/common/http';
\r
20 import { Observable } from 'rxjs';
\r
21 import { AppGlobals } from '../../app.global';
\r
22 import { map } from 'rxjs/operators';
\r
23 import { FeathersService } from './feathers.service';
\r
25 const httpOptions = {
\r
26 headers: new HttpHeaders({ 'Content-Type': 'application/json' })
\r
32 export class AuthService {
\r
34 constructor(private cookie: CookieService, private http: HttpClient, private feathers: FeathersService) { }
\r
36 //logs user into the app and store the auth token in cookie
\r
37 login(userLogin): Observable<Object> {
\r
38 let body = userLogin;
\r
39 body.strategy = "local";
\r
40 return new Observable(observer => {
\r
41 this.feathers.authenticate(body)
\r
43 this.storeUser(res);
\r
47 observer.error(err);
\r
50 // return this.http.post(AppGlobals.baseAPIUrl + 'authentication', body, httpOptions)
\r
51 // .pipe(map(authResult => {
\r
52 // if (authResult && authResult['accessToken']) {
\r
53 // this.storeUser(authResult);
\r
55 // return authResult;
\r
59 register(user): Observable<Object> {
\r
60 return this.http.post(AppGlobals.baseAPIUrl + 'users', user, httpOptions);
\r
63 //logs user out of app
\r
65 this.feathers.logout();
\r
66 window.localStorage.clear();
\r
67 this.cookie.delete('access_token');
\r
68 this.cookie.delete('currentUser');
\r
74 if (user.accessToken) {
\r
75 window.localStorage.setItem('access_token', user['accessToken'])
\r
76 window.localStorage.setItem('user_rules', JSON.stringify(user['user']['rules']));
\r
78 //The rules are too large to store as a cookie
\r
79 delete user['user']['rules'];
\r
81 this.cookie.set('access_token', JSON.stringify(user['accessToken']));
\r
82 this.cookie.set('currentUser', JSON.stringify(user['user']));
\r