From 16a56184b8fac18e30e3cab5479738e4092bcf45 Mon Sep 17 00:00:00 2001 From: Lukas Metzger Date: Fri, 13 Apr 2018 17:29:20 +0200 Subject: [PATCH] Added handling of session expiry --- backend/src/middlewares/Authentication.php | 2 +- frontend/src/app/services/http.service.ts | 89 ++++++++++++++++------ 2 files changed, 66 insertions(+), 25 deletions(-) diff --git a/backend/src/middlewares/Authentication.php b/backend/src/middlewares/Authentication.php index c202963..0a97cf0 100644 --- a/backend/src/middlewares/Authentication.php +++ b/backend/src/middlewares/Authentication.php @@ -38,7 +38,7 @@ class Authentication return $next($req, $res); } else { $this->logger->warning('No valid authentication token found'); - return $res->withJson(['error' => 'No valid authentication token suplied'], 403); + return $res->withJson(['error' => 'No valid authentication token suplied', 'code' => 'invalid_session'], 403); } } } diff --git a/frontend/src/app/services/http.service.ts b/frontend/src/app/services/http.service.ts index 3321fc1..84fac45 100644 --- a/frontend/src/app/services/http.service.ts +++ b/frontend/src/app/services/http.service.ts @@ -1,15 +1,18 @@ +import { Router } from '@angular/router'; import { Injectable } from '@angular/core'; -import { AxiosInstance, AxiosResponse } from 'axios'; +import { AxiosInstance, AxiosResponse, AxiosError } from 'axios'; import axios from 'axios'; import { StateService } from './state.service'; +import { ModalService } from './modal.service'; +import { ModalOptionsDatatype } from '../datatypes/modal-options.datatype'; @Injectable() export class HttpService { http: AxiosInstance; - constructor(private gs: StateService) { + constructor(private gs: StateService, private router: Router, private modal: ModalService) { this.http = axios.create({ baseURL: 'api/v1/' }); @@ -36,37 +39,53 @@ export class HttpService { const reqUrl = queryStr.length > 0 ? this.makeUrl(url) + '?' + queryStr : this.makeUrl(url); - return (await this.http({ - url: reqUrl, - method: 'get', - headers: this.buildHeaders() - })).data; + try { + return (await this.http({ + url: reqUrl, + method: 'get', + headers: this.buildHeaders() + })).data; + } catch (e) { + this.handleException(e); + } } public async post(url: string | Array, data: Object = {}): Promise { - return (await this.http({ - url: this.makeUrl(url), - method: 'post', - data: data, - headers: this.buildHeaders() - })).data; + try { + return (await this.http({ + url: this.makeUrl(url), + method: 'post', + data: data, + headers: this.buildHeaders() + })).data; + } catch (e) { + this.handleException(e); + } } public async put(url: string | Array, data: Object = {}): Promise { - return (await this.http({ - url: this.makeUrl(url), - method: 'put', - data: data, - headers: this.buildHeaders() - })).data; + try { + return (await this.http({ + url: this.makeUrl(url), + method: 'put', + data: data, + headers: this.buildHeaders() + })).data; + } catch (e) { + this.handleException(e); + } } public async delete(url: string | Array): Promise { - return (await this.http({ - url: this.makeUrl(url), - method: 'delete', - headers: this.buildHeaders() - })).data; + try { + return (await this.http({ + url: this.makeUrl(url), + method: 'delete', + headers: this.buildHeaders() + })).data; + } catch (e) { + this.handleException(e); + } } private buildHeaders(): Object { @@ -84,4 +103,26 @@ export class HttpService { return url; } } + + private async handleException(e: AxiosError) { + if (e.response && e.response.status === 403 && + e.response.data.hasOwnProperty('code') && + e.response.data.code === 'invalid_session') { + + await this.modal.showMessage(new ModalOptionsDatatype({ + heading: 'Session expired!', + body: 'Your session has been expired please log in again!', + acceptText: 'OK', + acceptClass: 'warning', + dismisText: '' + })); + + this.gs.apiToken = ''; + this.gs.isLoggedIn = false; + + this.router.navigate(['/']); + } else { + throw e; + } + } }