moved to single project
This commit is contained in:
54
ui/hv2-ui/src/app/token.service.ts
Normal file
54
ui/hv2-ui/src/app/token.service.ts
Normal file
@ -0,0 +1,54 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { MessageService } from './message.service';
|
||||
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
||||
import { UserCreds } from './userCreds'
|
||||
import jwt_decode from 'jwt-decode'
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class TokenService {
|
||||
|
||||
public static Id_Token_Key : string = "id_token";
|
||||
|
||||
constructor(private http: HttpClient, private messageService: MessageService) {
|
||||
}
|
||||
|
||||
checkAuthenticated(): boolean {
|
||||
let result: boolean = false
|
||||
|
||||
const token = localStorage.getItem(TokenService.Id_Token_Key)
|
||||
if (token) {
|
||||
let expiration = jwt_decode(token)["exp"]
|
||||
if ((expiration * 1000) > Date.now()) {
|
||||
result = true
|
||||
} else {
|
||||
this.logout()
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
logout() {
|
||||
localStorage.removeItem(TokenService.Id_Token_Key)
|
||||
this.messageService.add("Token removed from local storage")
|
||||
}
|
||||
|
||||
async login(login: string, password: string) {
|
||||
this.messageService.add(`TokenService: trying to login and obtain token`);
|
||||
const userCreds : UserCreds = {
|
||||
"application": "hv2",
|
||||
"login": login,
|
||||
"password": password
|
||||
}
|
||||
const token = await this.http.post(
|
||||
"https://authservice.hottis.de/token",
|
||||
userCreds,
|
||||
{responseType:'text'}
|
||||
).toPromise()
|
||||
localStorage.setItem(TokenService.Id_Token_Key, token)
|
||||
this.messageService.add("Token saved")
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user