refresh token support

This commit is contained in:
Wolfgang Hottgenroth 2021-09-03 19:31:46 +02:00
parent 65d10685a4
commit 2312f21d77
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F
2 changed files with 41 additions and 9 deletions

View File

@ -17,8 +17,11 @@ export class AuthHandlerInterceptor implements HttpInterceptor {
constructor(private tokenService: TokenService, private messageService: MessageService) {}
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
const token = localStorage.getItem(TokenService.Id_Token_Key)
const token = localStorage.getItem(TokenService.Id_AuthToken_Key)
if (request.url.includes(serviceBaseUrl) && token) {
this.messageService.add("start refresh of tokens")
this.tokenService.refresh()
this.messageService.add("api request intercepted")
const clone = request.clone({
setHeaders: { Authorization: `Bearer ${token}`}

View File

@ -4,12 +4,20 @@ import { HttpClient, HttpHeaders } from '@angular/common/http';
import { UserCreds } from './userCreds'
import jwt_decode from 'jwt-decode'
interface TokenTuple {
authToken: string
refreshToken: string
}
@Injectable({
providedIn: 'root'
})
export class TokenService {
public static Id_Token_Key : string = "id_token";
public static Id_AuthToken_Key : string = "id_authtoken";
public static Id_RefreshToken_Key : string = "id_refreshtoken";
constructor(private http: HttpClient, private messageService: MessageService) {
}
@ -17,7 +25,7 @@ export class TokenService {
checkAuthenticated(): boolean {
let result: boolean = false
const token = localStorage.getItem(TokenService.Id_Token_Key)
const token = localStorage.getItem(TokenService.Id_AuthToken_Key)
if (token) {
let expiration = jwt_decode(token)["exp"]
if ((expiration * 1000) > Date.now()) {
@ -31,7 +39,8 @@ export class TokenService {
}
logout() {
localStorage.removeItem(TokenService.Id_Token_Key)
localStorage.removeItem(TokenService.Id_AuthToken_Key)
localStorage.removeItem(TokenService.Id_RefreshToken_Key)
this.messageService.add("Token removed from local storage")
}
@ -42,13 +51,33 @@ export class TokenService {
"login": login,
"password": password
}
const token = await this.http.post(
"https://authservice.hottis.de/token",
userCreds,
{responseType:'text'}
const tokenTuple: TokenTuple = await this.http.post<TokenTuple>(
"https://authservice.hottis.de/refreshable",
userCreds
).toPromise()
localStorage.setItem(TokenService.Id_Token_Key, token)
localStorage.setItem(TokenService.Id_AuthToken_Key, tokenTuple.authToken)
localStorage.setItem(TokenService.Id_RefreshToken_Key, tokenTuple.refreshToken)
this.messageService.add("Token saved")
}
async refresh() {
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",
refreshToken
).toPromise()
localStorage.setItem(TokenService.Id_AuthToken_Key, tokenTuple.authToken)
localStorage.setItem(TokenService.Id_RefreshToken_Key, tokenTuple.refreshToken)
this.messageService.add("Token saved")
} catch (err) {
this.messageService.add(`error when trying to refresh: ${ JSON.stringify(err, undefined, 4)}`)
localStorage.removeItem(TokenService.Id_AuthToken_Key)
localStorage.removeItem(TokenService.Id_RefreshToken_Key)
this.messageService.add("Token removed from local storage")
}
}
}