added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / shared / services / auth.service.ts
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\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
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\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
15 \r
16 \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
24 \r
25 const httpOptions = {\r
26   headers: new HttpHeaders({ 'Content-Type': 'application/json' })\r
27 };\r
28 \r
29 @Injectable({\r
30   providedIn: 'root'\r
31 })\r
32 export class AuthService {\r
33 \r
34   constructor(private cookie: CookieService, private http: HttpClient, private feathers: FeathersService) { }\r
35 \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
42         .subscribe(res => {\r
43           this.storeUser(res);\r
44           observer.next(res);\r
45         },\r
46         err => {\r
47           observer.error(err);\r
48         });\r
49     });\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
54     //     }\r
55     //     return authResult;\r
56     //   }));\r
57   }\r
58 \r
59   register(user): Observable<Object> {\r
60     return this.http.post(AppGlobals.baseAPIUrl + 'users', user, httpOptions);\r
61   }\r
62 \r
63   //logs user out of app\r
64   logout() {\r
65     this.feathers.logout();\r
66     window.localStorage.clear();\r
67     this.cookie.delete('access_token');\r
68     this.cookie.delete('currentUser');\r
69   }\r
70 \r
71   //store a user\r
72   storeUser(user) {\r
73 \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
77 \r
78       //The rules are too large to store as a cookie\r
79       delete user['user']['rules'];\r
80 \r
81       this.cookie.set('access_token', JSON.stringify(user['accessToken']));\r
82       this.cookie.set('currentUser', JSON.stringify(user['user']));\r
83     }\r
84   }\r
85 }\r