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 moment, { Moment } from 'moment';
|
||||||
import { environment } from 'src/environments/environment';
|
import { environment } from 'src/environments/environment';
|
||||||
import { User } from '../models/user.model';
|
import { User } from '../models/user.model';
|
||||||
|
import { from, Observable } from 'rxjs';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
@@ -43,10 +44,6 @@ export class AuthService {
|
|||||||
this.user.id = data.id;
|
this.user.id = data.id;
|
||||||
this.apiToken = data.token;
|
this.apiToken = data.token;
|
||||||
this.expirationTime = moment(data.expirationTime);
|
this.expirationTime = moment(data.expirationTime);
|
||||||
const ms = this.expirationTime.subtract(29, 'minutes').diff(moment.utc(), 'milliseconds');
|
|
||||||
setTimeout(() => {
|
|
||||||
this.getAPIToken();
|
|
||||||
}, ms);
|
|
||||||
resolve(data);
|
resolve(data);
|
||||||
},
|
},
|
||||||
error: (e: HttpErrorResponse) => {
|
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 {
|
} else {
|
||||||
this.ngZone$.run(() => {
|
this.ngZone$.run(() => {
|
||||||
this.notifications$.add({
|
this.notifications$.add({
|
||||||
text: "DiunaBI server not responsed.",
|
text: "DiunaBI server not responded.",
|
||||||
btn: "OK",
|
btn: "OK",
|
||||||
duration: 15000
|
duration: 15000
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,21 +6,45 @@ import {
|
|||||||
HttpInterceptor
|
HttpInterceptor
|
||||||
} from '@angular/common/http';
|
} from '@angular/common/http';
|
||||||
import { AuthService } from '../auth/auth.service';
|
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()
|
@Injectable()
|
||||||
export class AuthInterceptor implements HttpInterceptor {
|
export class AuthInterceptor implements HttpInterceptor {
|
||||||
constructor(
|
constructor(
|
||||||
private auth$: AuthService
|
private auth$: AuthService,
|
||||||
|
private notifications$: NotificationsService
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
|
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
|
||||||
if (!request.url.includes('/auth/apiToken')) {
|
if (!request.url.includes('/auth/apiToken')) {
|
||||||
return next.handle(request.clone({
|
if (this.auth$.expirationTime.isBefore(moment.utc())) {
|
||||||
headers: request.headers.set('Authorization', `Bearer ${this.auth$.apiToken}`),
|
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 {
|
} else {
|
||||||
return next.handle(request);
|
return next.handle(request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,9 @@ export class NotificationsService {
|
|||||||
}, message.duration ? message.duration : 5000);
|
}, message.duration ? message.duration : 5000);
|
||||||
// close parent
|
// close parent
|
||||||
if (message.parentId) {
|
if (message.parentId) {
|
||||||
this.remove(message.parentId);
|
setTimeout(() => {
|
||||||
|
this.remove(message.parentId);
|
||||||
|
}, 500);
|
||||||
}
|
}
|
||||||
return message.id;
|
return message.id;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ export const environment = {
|
|||||||
appEnvironment: "local",
|
appEnvironment: "local",
|
||||||
production: false,
|
production: false,
|
||||||
api: {
|
api: {
|
||||||
//url: "http://localhost:5400/api"
|
url: "http://localhost:5400/api"
|
||||||
url: "https://diunabi.bim-it.pl/api"
|
// url: "https://diunabi.bim-it.pl/api"
|
||||||
},
|
},
|
||||||
google: {
|
google: {
|
||||||
clientId: "107631825312-bkfe438ehr9k9ecb2h76g802tj6advma.apps.googleusercontent.com"
|
clientId: "107631825312-bkfe438ehr9k9ecb2h76g802tj6advma.apps.googleusercontent.com"
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ namespace WebAPI.Controllers
|
|||||||
private dynamic JWTGenerator(User user)
|
private dynamic JWTGenerator(User user)
|
||||||
{
|
{
|
||||||
var key = Encoding.ASCII.GetBytes(configuration.GetValue<string>("Secret"));
|
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
|
var tokenDescriptor = new SecurityTokenDescriptor
|
||||||
{
|
{
|
||||||
Subject = new ClaimsIdentity(new[]
|
Subject = new ClaimsIdentity(new[]
|
||||||
|
|||||||
Reference in New Issue
Block a user