Fix mod ui build issues
[dcaegen2/platform.git] / mod2 / ui / src / app / services / auth.service.ts
1 /* 
2  *  # ============LICENSE_START=======================================================
3  *  # Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
4  *  # ================================================================================
5  *  # Licensed under the Apache License, Version 2.0 (the "License");
6  *  # you may not use this file except in compliance with the License.
7  *  # You may obtain a copy of the License at
8  *  #
9  *  #      http://www.apache.org/licenses/LICENSE-2.0
10  *  #
11  *  # Unless required by applicable law or agreed to in writing, software
12  *  # distributed under the License is distributed on an "AS IS" BASIS,
13  *  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  # See the License for the specific language governing permissions and
15  *  # limitations under the License.
16  *  # ============LICENSE_END=========================================================
17  */
18
19 import { Injectable } from '@angular/core';
20 import { HttpClient, HttpHandler, HttpHeaders } from '@angular/common/http';
21 import { Observable } from 'rxjs';
22 import { tap } from 'rxjs/internal/operators';
23 import { User } from '../models/User';
24 import { JwtHelperService } from '@auth0/angular-jwt';
25 import { AuthResponse } from '../models/AuthResponse';
26 import { Router } from '@angular/router';
27 import { environment } from '../../environments/environment';
28
29 @Injectable({
30   providedIn: 'root'
31 })
32 export class AuthService{
33
34  private user: User = {
35    username: '',
36    roles:[]
37  };
38  authPass=false;
39  isAdmin=false;
40  reLoginMsg = false;
41  
42   constructor(
43     private http:HttpClient,
44     private jwtHelper: JwtHelperService,
45     private router: Router
46     ) { 
47       
48     }
49
50   register(user: User): Observable<AuthResponse> {
51     return this.http.post<AuthResponse>(`http://${environment.api_baseURL}:31003/api/auth/signup`, user);
52     // return this.http.post<AuthResponse>(`http://localhost:8082/api/auth/signup`, user);
53
54   }
55
56   login(user: User): Observable<AuthResponse> {
57       return this.http.post<AuthResponse>(`http://${environment.api_baseURL}:31003/api/auth/signin`, user)
58       .pipe(
59        tap((res: AuthResponse) => {
60         localStorage.setItem('token', res.token);
61         this.setUser();
62         this.authPass = true;
63        })
64       );
65   }
66
67   setUser() {
68     this.user.username = this.jwtHelper.decodeToken(localStorage.getItem('token')).sub;
69     this.user.roles = this.jwtHelper.decodeToken(localStorage.getItem('token')).roles;
70     this.user.fullName = this.jwtHelper.decodeToken(localStorage.getItem('token')).fullName;
71     
72   }
73
74   checkLogin(): Observable<boolean>{
75     return this.http.post<boolean>(`http://${environment.api_baseURL}:31003/api/auth/validate-token`, null);
76   }
77
78   getJwt() {
79     if(localStorage.getItem('token')){
80       return localStorage.getItem('token');
81     }
82     return "";
83   }
84
85   logout() {
86     localStorage.removeItem('token');
87     this.authPass = false;
88     this.router.navigate(['/login']);
89   }
90
91   getUser(): User {
92     return this.user;
93   }
94
95 }