Cache authorization in local storage.

This commit is contained in:
cyberemissary 2019-05-17 16:50:49 -04:00
parent 61b1ed35ad
commit 31ef0bdc58

View File

@ -21,5 +21,30 @@ window.onload = () => {
layout: 'StandaloneLayout'
});
const storageKey = 'nelmio_api_auth';
// if we have auth in storage use it
if (sessionStorage.getItem(storageKey)) {
try {
ui.authActions.authorize(JSON.parse(sessionStorage.getItem(storageKey)));
} catch (ignored) {
// catch any errors here so it does not stop script execution
}
}
// hook into authorize to store the auth in local storage when user performs authorization
const currentAuthorize = ui.authActions.authorize;
ui.authActions.authorize = function (payload) {
sessionStorage.setItem(storageKey, JSON.stringify(payload));
return currentAuthorize(payload);
};
// hook into logout to clear auth from storage if user logs out
const currentLogout = ui.authActions.logout;
ui.authActions.logout = function (payload) {
sessionStorage.removeItem(storageKey);
return currentLogout(payload);
};
window.ui = ui;
};