added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / shared / services / auth.service.ts
diff --git a/otf-frontend/client/src/app/shared/services/auth.service.ts b/otf-frontend/client/src/app/shared/services/auth.service.ts
new file mode 100644 (file)
index 0000000..7b5fe3f
--- /dev/null
@@ -0,0 +1,85 @@
+/*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
+#                                                                              #\r
+#   Licensed under the Apache License, Version 2.0 (the "License");            #\r
+#   you may not use this file except in compliance with the License.           #\r
+#   You may obtain a copy of the License at                                    #\r
+#                                                                              #\r
+#       http://www.apache.org/licenses/LICENSE-2.0                             #\r
+#                                                                              #\r
+#   Unless required by applicable law or agreed to in writing, software        #\r
+#   distributed under the License is distributed on an "AS IS" BASIS,          #\r
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
+#   See the License for the specific language governing permissions and        #\r
+#   limitations under the License.                                             #\r
+##############################################################################*/\r
+\r
+\r
+import { Injectable } from '@angular/core';\r
+import { CookieService } from 'ngx-cookie-service';\r
+import { HttpClient, HttpHeaders } from '@angular/common/http';\r
+import { Observable } from 'rxjs';\r
+import { AppGlobals } from '../../app.global';\r
+import { map } from 'rxjs/operators';\r
+import { FeathersService } from './feathers.service';\r
+\r
+const httpOptions = {\r
+  headers: new HttpHeaders({ 'Content-Type': 'application/json' })\r
+};\r
+\r
+@Injectable({\r
+  providedIn: 'root'\r
+})\r
+export class AuthService {\r
+\r
+  constructor(private cookie: CookieService, private http: HttpClient, private feathers: FeathersService) { }\r
+\r
+  //logs user into the app and store the auth token in cookie\r
+  login(userLogin): Observable<Object> {\r
+    let body = userLogin;\r
+    body.strategy = "local";\r
+    return new Observable(observer => {\r
+      this.feathers.authenticate(body)\r
+        .subscribe(res => {\r
+          this.storeUser(res);\r
+          observer.next(res);\r
+        },\r
+        err => {\r
+          observer.error(err);\r
+        });\r
+    });\r
+    // return this.http.post(AppGlobals.baseAPIUrl + 'authentication', body, httpOptions)\r
+    //   .pipe(map(authResult => {\r
+    //     if (authResult && authResult['accessToken']) {\r
+    //       this.storeUser(authResult);\r
+    //     }\r
+    //     return authResult;\r
+    //   }));\r
+  }\r
+\r
+  register(user): Observable<Object> {\r
+    return this.http.post(AppGlobals.baseAPIUrl + 'users', user, httpOptions);\r
+  }\r
+\r
+  //logs user out of app\r
+  logout() {\r
+    this.feathers.logout();\r
+    window.localStorage.clear();\r
+    this.cookie.delete('access_token');\r
+    this.cookie.delete('currentUser');\r
+  }\r
+\r
+  //store a user\r
+  storeUser(user) {\r
+\r
+    if (user.accessToken) {\r
+      window.localStorage.setItem('access_token', user['accessToken'])\r
+      window.localStorage.setItem('user_rules', JSON.stringify(user['user']['rules']));\r
+\r
+      //The rules are too large to store as a cookie\r
+      delete user['user']['rules'];\r
+\r
+      this.cookie.set('access_token', JSON.stringify(user['accessToken']));\r
+      this.cookie.set('currentUser', JSON.stringify(user['user']));\r
+    }\r
+  }\r
+}\r