Added handling of session expiry
This commit is contained in:
parent
e6ca551641
commit
16a56184b8
2 changed files with 66 additions and 25 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<string>, data: Object = {}): Promise<any> {
|
||||
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<string>, data: Object = {}): Promise<any> {
|
||||
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<string>): Promise<any> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue