Restore API token if expired
This commit is contained in:
@@ -3,6 +3,7 @@ import { Injectable } from '@angular/core';
|
||||
import moment, { Moment } from 'moment';
|
||||
import { environment } from 'src/environments/environment';
|
||||
import { User } from '../models/user.model';
|
||||
import { from, Observable } from 'rxjs';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@@ -43,10 +44,6 @@ export class AuthService {
|
||||
this.user.id = data.id;
|
||||
this.apiToken = data.token;
|
||||
this.expirationTime = moment(data.expirationTime);
|
||||
const ms = this.expirationTime.subtract(29, 'minutes').diff(moment.utc(), 'milliseconds');
|
||||
setTimeout(() => {
|
||||
this.getAPIToken();
|
||||
}, ms);
|
||||
resolve(data);
|
||||
},
|
||||
error: (e: HttpErrorResponse) => {
|
||||
@@ -56,4 +53,8 @@ export class AuthService {
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
getAPITokenObservable(): Observable<void> {
|
||||
return from(this.getAPIToken());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ export class LoginPageComponent implements OnInit {
|
||||
} else {
|
||||
this.ngZone$.run(() => {
|
||||
this.notifications$.add({
|
||||
text: "DiunaBI server not responsed.",
|
||||
text: "DiunaBI server not responded.",
|
||||
btn: "OK",
|
||||
duration: 15000
|
||||
});
|
||||
|
||||
@@ -6,21 +6,45 @@ import {
|
||||
HttpInterceptor
|
||||
} from '@angular/common/http';
|
||||
import { AuthService } from '../auth/auth.service';
|
||||
import { Observable } from 'rxjs';
|
||||
import { EMPTY, Observable } from 'rxjs';
|
||||
import moment from 'moment';
|
||||
import { catchError, mergeMap } from 'rxjs/operators';
|
||||
import { NotificationsService } from '../services/notifications.service';
|
||||
|
||||
@Injectable()
|
||||
export class AuthInterceptor implements HttpInterceptor {
|
||||
constructor(
|
||||
private auth$: AuthService
|
||||
private auth$: AuthService,
|
||||
private notifications$: NotificationsService
|
||||
) { }
|
||||
|
||||
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
|
||||
if (!request.url.includes('/auth/apiToken')) {
|
||||
return next.handle(request.clone({
|
||||
headers: request.headers.set('Authorization', `Bearer ${this.auth$.apiToken}`),
|
||||
}));
|
||||
if (this.auth$.expirationTime.isBefore(moment.utc())) {
|
||||
return this.auth$.getAPITokenObservable().pipe(
|
||||
mergeMap(() => {
|
||||
return next.handle(request.clone({
|
||||
headers: request.headers.set('Authorization', `Bearer ${this.auth$.apiToken}`),
|
||||
}));
|
||||
}),
|
||||
catchError(() => {
|
||||
this.notifications$.add({
|
||||
text: "User session is expired and unable to restore. Please restart the app.",
|
||||
btn: "Restart",
|
||||
action: () => { window.location.reload(); },
|
||||
duration: 5000,
|
||||
});
|
||||
return EMPTY;
|
||||
})
|
||||
);
|
||||
} else {
|
||||
return next.handle(request.clone({
|
||||
headers: request.headers.set('Authorization', `Bearer ${this.auth$.apiToken}`),
|
||||
}));
|
||||
}
|
||||
} else {
|
||||
return next.handle(request);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,9 @@ export class NotificationsService {
|
||||
}, message.duration ? message.duration : 5000);
|
||||
// close parent
|
||||
if (message.parentId) {
|
||||
this.remove(message.parentId);
|
||||
setTimeout(() => {
|
||||
this.remove(message.parentId);
|
||||
}, 500);
|
||||
}
|
||||
return message.id;
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ export const environment = {
|
||||
appEnvironment: "local",
|
||||
production: false,
|
||||
api: {
|
||||
//url: "http://localhost:5400/api"
|
||||
url: "https://diunabi.bim-it.pl/api"
|
||||
url: "http://localhost:5400/api"
|
||||
// url: "https://diunabi.bim-it.pl/api"
|
||||
},
|
||||
google: {
|
||||
clientId: "107631825312-bkfe438ehr9k9ecb2h76g802tj6advma.apps.googleusercontent.com"
|
||||
|
||||
@@ -47,7 +47,7 @@ namespace WebAPI.Controllers
|
||||
private dynamic JWTGenerator(User user)
|
||||
{
|
||||
var key = Encoding.ASCII.GetBytes(configuration.GetValue<string>("Secret"));
|
||||
var expirationTime = DateTime.UtcNow.AddMinutes(30);
|
||||
var expirationTime = DateTime.UtcNow.AddMinutes(5);
|
||||
var tokenDescriptor = new SecurityTokenDescriptor
|
||||
{
|
||||
Subject = new ClaimsIdentity(new[]
|
||||
|
||||
Reference in New Issue
Block a user