diff --git a/Frontend/src/app/auth/auth.service.ts b/Frontend/src/app/auth/auth.service.ts index 893057d..e670cc9 100644 --- a/Frontend/src/app/auth/auth.service.ts +++ b/Frontend/src/app/auth/auth.service.ts @@ -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 { + return from(this.getAPIToken()); + } } diff --git a/Frontend/src/app/components/login-page/login-page.component.ts b/Frontend/src/app/components/login-page/login-page.component.ts index d52b4d7..71a623a 100644 --- a/Frontend/src/app/components/login-page/login-page.component.ts +++ b/Frontend/src/app/components/login-page/login-page.component.ts @@ -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 }); diff --git a/Frontend/src/app/interceptors/auth.interceptor.ts b/Frontend/src/app/interceptors/auth.interceptor.ts index 10e724e..9ca40ce 100644 --- a/Frontend/src/app/interceptors/auth.interceptor.ts +++ b/Frontend/src/app/interceptors/auth.interceptor.ts @@ -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, next: HttpHandler): Observable> { 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); } } } + diff --git a/Frontend/src/app/services/notifications.service.ts b/Frontend/src/app/services/notifications.service.ts index 0c1a566..d0c8abf 100644 --- a/Frontend/src/app/services/notifications.service.ts +++ b/Frontend/src/app/services/notifications.service.ts @@ -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; } diff --git a/Frontend/src/environments/environment.ts b/Frontend/src/environments/environment.ts index a1bc68a..e132aa2 100644 --- a/Frontend/src/environments/environment.ts +++ b/Frontend/src/environments/environment.ts @@ -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" diff --git a/WebAPI/Controllers/AuthController.cs b/WebAPI/Controllers/AuthController.cs index 9266456..4e2a4e4 100644 --- a/WebAPI/Controllers/AuthController.cs +++ b/WebAPI/Controllers/AuthController.cs @@ -47,7 +47,7 @@ namespace WebAPI.Controllers private dynamic JWTGenerator(User user) { var key = Encoding.ASCII.GetBytes(configuration.GetValue("Secret")); - var expirationTime = DateTime.UtcNow.AddMinutes(30); + var expirationTime = DateTime.UtcNow.AddMinutes(5); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new[]