Untitled

dsds
mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.5 kB
2
Indexable
Never
apiURL: string = environment.API_URL;

  token:string;
  public loggedUser:string;
  public isloggedIn: boolean ;
  public roles:string[];
  private helper = new JwtHelperService();


  constructor(private router: Router,
              private http : HttpClient) { }


  login(user : User)
  {
    return this.http.post(this.apiURL+'/auth/login', user , {observe:'response'});
  }

  saveToken(jwt:string){
    localStorage.setItem('jwt',jwt);
    this.token = jwt;
    this.isloggedIn = true;
    console.log(this.isloggedIn);
    this.decodeJWT();
  }

  decodeJWT()
  {   if (this.token == undefined)
    return;
    const decodedToken = this.helper.decodeToken(this.token);
    this.roles = decodedToken.roles;
    this.isloggedIn = true;
    this.loggedUser = decodedToken.sub;
    console.log(this.loggedUser);
  }



  loadToken() {
    this.token = <string>localStorage.getItem('jwt');
    this.decodeJWT();
  }

  getToken():string {
    return this.token;
  }

  logout() {
    this.loggedUser = "";
    this.roles = [];
    this.token= "";
    this.isloggedIn = false;
    localStorage.removeItem('jwt');
    this.router.navigate(['/login']);
  }



  isAdmin():Boolean{
    if (!this.roles)
      return false;
    return this.roles.indexOf('ROLE_ADMIN') >=0;
  }



  isTokenExpired(): Boolean
  {
    return  this.helper.isTokenExpired(this.token);
  }


  setLoggedUserFromLocalStorage(login : string) {
    this.loggedUser = login;
    this.isloggedIn = true;
    this.getUserRoles(login);
  }