Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #209 from sarbull/master
Browse files Browse the repository at this point in the history
Restructure cronological order for requests and page access
  • Loading branch information
marius-patrascu authored Jul 21, 2021
2 parents 5eaf64d + 6f0ab04 commit 5008711
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
12 changes: 9 additions & 3 deletions new/commons/fetch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCookie, setCookie } from './local-storage';
import { getCookie, setCookie, removeCookie } from './local-storage';
import { ENV } from '../../env';

let spinnerCount = 0;
Expand Down Expand Up @@ -42,12 +42,18 @@ export const egeriaFetch = (url, headers) => {
)
.then((response) => {
if(response.status === 403 && !['/login'].includes(window.location.pathname)) {
removeCookie('token');
setCookie('token', '');

window.location.href='/login';
window.location.href = '/login';
}

return response.json();
return response.text();
})
.then(data => {
// adding this temporarily because /logout retrieves no content and
// response.json() fails to parse empty content as JSON
return data ? JSON.parse(data) : {};
})
.finally(() => {
spinner(false);
Expand Down
4 changes: 4 additions & 0 deletions new/commons/local-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ export const setCookie = (name, value) => {

export const getCookie = (name) => {
return window.localStorage.getItem(name);
};

export const removeCookie = (name) => {
localStorage.removeItem(name);
};
45 changes: 23 additions & 22 deletions new/egeria-app.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,9 @@ import { getCookie } from './commons/local-storage';
import { egeriaFetch } from './commons/fetch';

class EgeriaApp extends PolymerElement {
static get properties() {
return {
components: { type: Array, value: [] },
currentUser: { type: Object, value: {} },
appInfo: { type: Object, value: {} },
roles: { type: Array, value: [] },

isLoading: { type: Boolean, value: true },
isLoggedIn: { type: Boolean, value: false },

queryParams: {},
routeData: {},
tail: {},

route: { type: Object },
pages: { type: Array, value: [''], observer: '_pagesChanged' },
page: { type: String, value: '' }
};
}
constructor() {
super();

ready() {
super.ready();
this.isLoading = true;

Promise.all([
Expand All @@ -72,6 +53,26 @@ class EgeriaApp extends PolymerElement {
});
}

static get properties() {
return {
components: { type: Array, value: [] },
currentUser: { type: Object, value: {} },
appInfo: { type: Object, value: {} },
roles: { type: Array, value: [] },

isLoading: { type: Boolean, value: true },
isLoggedIn: { type: Boolean, value: false },

queryParams: {},
routeData: {},
tail: {},

route: { type: Object },
pages: { type: Array, value: [''], observer: '_pagesChanged' },
page: { type: String, value: '' }
};
}

static get observers() {
return [
'_routeCycle(route, components, currentUser)'
Expand Down Expand Up @@ -147,7 +148,7 @@ class EgeriaApp extends PolymerElement {
if(this.canAccess(route, components)) {
this.pages = route.split('/');
} else {
this.pages = ['forbidden'];
this.pages = !getCookie('token') ? ['empty'] : ['forbidden'];
}
}
break;
Expand Down
5 changes: 3 additions & 2 deletions new/egeria-user-options.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import '@vaadin/vaadin-grid/vaadin-grid-sort-column.js';

import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';

import { setCookie } from '../new/commons/local-storage';
import { setCookie, removeCookie } from '../new/commons/local-storage';
import { egeriaFetch } from './commons/fetch';

class EgeriaUserOptions extends PolymerElement {
Expand All @@ -29,7 +29,8 @@ class EgeriaUserOptions extends PolymerElement {
_logout() {
egeriaFetch(`/api/logout`)
.then(() => {
setCookie('token', null);
removeCookie('token');
setCookie('token', '');
});
}

Expand Down

0 comments on commit 5008711

Please sign in to comment.