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
9 * # http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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';
32 export class AuthService{
34 private user: User = {
43 private http:HttpClient,
44 private jwtHelper: JwtHelperService,
45 private router: Router
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);
56 login(user: User): Observable<AuthResponse> {
57 return this.http.post<AuthResponse>(`http://${environment.api_baseURL}:31003/api/auth/signin`, user)
59 tap((res: AuthResponse) => {
60 localStorage.setItem('token', res.token);
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;
74 checkLogin(): Observable<boolean>{
75 return this.http.post<boolean>(`http://${environment.api_baseURL}:31003/api/auth/validate-token`, null);
79 if(localStorage.getItem('token')){
80 return localStorage.getItem('token');
86 localStorage.removeItem('token');
87 this.authPass = false;
88 this.router.navigate(['/login']);