expirytime finally working
This commit is contained in:
parent
9478ad27c6
commit
fd25f1fcf5
@ -1,4 +1,5 @@
|
||||
// export const serviceBaseUrl = "https://api.hv.nober.de";
|
||||
// export const serviceBaseUrl = "http://172.16.10.38:5000";
|
||||
export const serviceBaseUrl = "http://localhost:8080";
|
||||
export const serviceBaseUrl = "http://localhost:8080"
|
||||
export const authserviceBaseUrl = "https://authservice.hottis.de"
|
||||
|
||||
|
@ -29,6 +29,7 @@
|
||||
<span>Nober Grundbesitz GbR Hausverwaltung</span>
|
||||
<span class="spacer"></span>
|
||||
<span class="gittagversion">GITTAGVERSION</span>
|
||||
<span class="gittagversion" *ngIf="authenticated">Läuft aus in {{expiryTime | async }} Sekunden</span>
|
||||
<a *ngIf="!authenticated" mat-button routerLink="/login">Login</a>
|
||||
<a *ngIf="authenticated" mat-button routerLink="/logout">Logout</a>
|
||||
</mat-toolbar>
|
||||
|
@ -33,6 +33,10 @@ export class NavigationComponent {
|
||||
})
|
||||
}
|
||||
|
||||
get expiryTime(): Observable<number> {
|
||||
return this.tokenService.expiryTime
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.authenticated = this.tokenService.checkAuthenticated()
|
||||
}
|
||||
|
@ -3,7 +3,9 @@ import { MessageService } from './message.service';
|
||||
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
||||
import { UserCreds } from './userCreds'
|
||||
import jwt_decode from 'jwt-decode'
|
||||
|
||||
import { Observable, interval, Subject, Subscription } from 'rxjs'
|
||||
import { map, takeUntil, takeWhile } from 'rxjs/operators'
|
||||
import { authserviceBaseUrl } from './config'
|
||||
|
||||
|
||||
interface TokenTuple {
|
||||
@ -19,6 +21,7 @@ export class TokenService {
|
||||
public static Id_AuthToken_Key : string = "id_authtoken";
|
||||
public static Id_RefreshToken_Key : string = "id_refreshtoken";
|
||||
|
||||
private _expiryTime = new Subject<number>()
|
||||
|
||||
constructor(private http: HttpClient, private messageService: MessageService) {
|
||||
}
|
||||
@ -39,13 +42,24 @@ export class TokenService {
|
||||
return result
|
||||
}
|
||||
|
||||
logout() {
|
||||
logout() : void {
|
||||
localStorage.removeItem(TokenService.Id_AuthToken_Key)
|
||||
localStorage.removeItem(TokenService.Id_RefreshToken_Key)
|
||||
this.messageService.add("Token removed from local storage")
|
||||
}
|
||||
|
||||
async login(login: string, password: string) {
|
||||
get expiryTime(): Observable<number> {
|
||||
return this._expiryTime
|
||||
}
|
||||
|
||||
setExpiryTime(token: string) :void {
|
||||
let exp = jwt_decode(token)["exp"]
|
||||
let iat = jwt_decode(token)["iat"]
|
||||
let start = exp - iat
|
||||
interval(1000).pipe(map(v => start - v)).pipe(takeWhile(v => v != 0)).subscribe(v => this._expiryTime.next(v))
|
||||
}
|
||||
|
||||
async login(login: string, password: string) : Promise<void> {
|
||||
this.messageService.add(`TokenService: trying to login and obtain token`)
|
||||
const userCreds : UserCreds = {
|
||||
"application": "hv2",
|
||||
@ -53,25 +67,29 @@ export class TokenService {
|
||||
"password": password
|
||||
}
|
||||
const tokenTuple: TokenTuple = await this.http.post<TokenTuple>(
|
||||
"https://authservice.hottis.de/refreshable",
|
||||
`${authserviceBaseUrl}/refreshable`,
|
||||
userCreds
|
||||
).toPromise()
|
||||
localStorage.setItem(TokenService.Id_AuthToken_Key, tokenTuple.authToken)
|
||||
localStorage.setItem(TokenService.Id_RefreshToken_Key, tokenTuple.refreshToken)
|
||||
this.messageService.add("Token saved")
|
||||
|
||||
this.setExpiryTime(tokenTuple.authToken)
|
||||
}
|
||||
|
||||
async refresh() {
|
||||
async refresh() : Promise<void> {
|
||||
try {
|
||||
this.messageService.add(`TokenService: trying to refresh tokens`)
|
||||
const refreshToken = localStorage.getItem(TokenService.Id_RefreshToken_Key)
|
||||
const tokenTuple: TokenTuple = await this.http.post<TokenTuple>(
|
||||
"https://authservice.hottis.de/refresh",
|
||||
`${authserviceBaseUrl}/refresh`,
|
||||
refreshToken
|
||||
).toPromise()
|
||||
localStorage.setItem(TokenService.Id_AuthToken_Key, tokenTuple.authToken)
|
||||
localStorage.setItem(TokenService.Id_RefreshToken_Key, tokenTuple.refreshToken)
|
||||
this.messageService.add("Token saved")
|
||||
|
||||
this.setExpiryTime(tokenTuple.authToken)
|
||||
} catch (err) {
|
||||
this.messageService.add(`error when trying to refresh: ${ JSON.stringify(err, undefined, 4)}`)
|
||||
localStorage.removeItem(TokenService.Id_AuthToken_Key)
|
||||
|
Loading…
x
Reference in New Issue
Block a user