Added handling of session expiry
This commit is contained in:
parent
e6ca551641
commit
16a56184b8
|
@ -38,7 +38,7 @@ class Authentication
|
||||||
return $next($req, $res);
|
return $next($req, $res);
|
||||||
} else {
|
} else {
|
||||||
$this->logger->warning('No valid authentication token found');
|
$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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,18 @@
|
||||||
|
import { Router } from '@angular/router';
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
|
|
||||||
import { AxiosInstance, AxiosResponse } from 'axios';
|
import { AxiosInstance, AxiosResponse, AxiosError } from 'axios';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { StateService } from './state.service';
|
import { StateService } from './state.service';
|
||||||
|
import { ModalService } from './modal.service';
|
||||||
|
import { ModalOptionsDatatype } from '../datatypes/modal-options.datatype';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class HttpService {
|
export class HttpService {
|
||||||
|
|
||||||
http: AxiosInstance;
|
http: AxiosInstance;
|
||||||
|
|
||||||
constructor(private gs: StateService) {
|
constructor(private gs: StateService, private router: Router, private modal: ModalService) {
|
||||||
this.http = axios.create({
|
this.http = axios.create({
|
||||||
baseURL: 'api/v1/'
|
baseURL: 'api/v1/'
|
||||||
});
|
});
|
||||||
|
@ -36,37 +39,53 @@ export class HttpService {
|
||||||
|
|
||||||
const reqUrl = queryStr.length > 0 ? this.makeUrl(url) + '?' + queryStr : this.makeUrl(url);
|
const reqUrl = queryStr.length > 0 ? this.makeUrl(url) + '?' + queryStr : this.makeUrl(url);
|
||||||
|
|
||||||
return (await this.http({
|
try {
|
||||||
url: reqUrl,
|
return (await this.http({
|
||||||
method: 'get',
|
url: reqUrl,
|
||||||
headers: this.buildHeaders()
|
method: 'get',
|
||||||
})).data;
|
headers: this.buildHeaders()
|
||||||
|
})).data;
|
||||||
|
} catch (e) {
|
||||||
|
this.handleException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async post(url: string | Array<string>, data: Object = {}): Promise<any> {
|
public async post(url: string | Array<string>, data: Object = {}): Promise<any> {
|
||||||
return (await this.http({
|
try {
|
||||||
url: this.makeUrl(url),
|
return (await this.http({
|
||||||
method: 'post',
|
url: this.makeUrl(url),
|
||||||
data: data,
|
method: 'post',
|
||||||
headers: this.buildHeaders()
|
data: data,
|
||||||
})).data;
|
headers: this.buildHeaders()
|
||||||
|
})).data;
|
||||||
|
} catch (e) {
|
||||||
|
this.handleException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async put(url: string | Array<string>, data: Object = {}): Promise<any> {
|
public async put(url: string | Array<string>, data: Object = {}): Promise<any> {
|
||||||
return (await this.http({
|
try {
|
||||||
url: this.makeUrl(url),
|
return (await this.http({
|
||||||
method: 'put',
|
url: this.makeUrl(url),
|
||||||
data: data,
|
method: 'put',
|
||||||
headers: this.buildHeaders()
|
data: data,
|
||||||
})).data;
|
headers: this.buildHeaders()
|
||||||
|
})).data;
|
||||||
|
} catch (e) {
|
||||||
|
this.handleException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async delete(url: string | Array<string>): Promise<any> {
|
public async delete(url: string | Array<string>): Promise<any> {
|
||||||
return (await this.http({
|
try {
|
||||||
url: this.makeUrl(url),
|
return (await this.http({
|
||||||
method: 'delete',
|
url: this.makeUrl(url),
|
||||||
headers: this.buildHeaders()
|
method: 'delete',
|
||||||
})).data;
|
headers: this.buildHeaders()
|
||||||
|
})).data;
|
||||||
|
} catch (e) {
|
||||||
|
this.handleException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private buildHeaders(): Object {
|
private buildHeaders(): Object {
|
||||||
|
@ -84,4 +103,26 @@ export class HttpService {
|
||||||
return url;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue