moved to single project

This commit is contained in:
2021-08-02 16:52:31 +02:00
commit 4f8b3ce8f3
89 changed files with 16070 additions and 0 deletions

View 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")
}
}