Skip to content

Commit

Permalink
✨ : add service for session storage
Browse files Browse the repository at this point in the history
  • Loading branch information
cdubuisson committed Apr 9, 2020
1 parent b4f9f72 commit 0a9ab01
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/main/client/app/shared/services/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
export * from './authentication/authentication.js';
export {
getRequestedUrl,
setRequestedUrl,
hasRequestedUrl,
removeRequestedUrl,
} from './session-storage-service';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const getRequestedUrl = () => sessionStorage.getItem('requested-url');

export const setRequestedUrl = (url) => sessionStorage.setItem('requested-url', url);

export const hasRequestedUrl = () => !!sessionStorage.getItem('requested-url');

export const removeRequestedUrl = () => sessionStorage.removeItem('requested-url');
26 changes: 18 additions & 8 deletions src/main/client/app/shared/store/modules/session-store-module.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
import {
doLogin,
doLogout,
getRoles,
getAuthorities,
getUser,
} from '@/shared/api';

const sessionState = {
login: false,
authenticated: false,
user: null,
roles: null,
authorities: null,
};

const sessionGetters = {};
const sessionGetters = {
hasAuthorities: (state) => (authorities) => {
if (!state.authenticated || !state.authorities) return false;

let authoritiesToCheck = authorities;
if (typeof authorities === 'string') {
authoritiesToCheck = [authorities];
}
return authoritiesToCheck.some((role) => state.authorities.includes(role));
},
};

const sessionMutations = {
login: (state) => {
state.login = true;
},
authenticated: (state, { user, roles }) => {
authenticated: (state, { user, authorities }) => {
state.login = false;
state.authenticated = true;
state.user = user;
state.roles = roles;
state.authorities = authorities;
},
logout: (state) => {
state.login = false;
state.authenticated = false;
state.user = null;
state.roles = null;
state.authorities = null;
},
};

Expand All @@ -40,8 +50,8 @@ const sessionActions = {
},
authenticated: async ({ commit }) => {
const user = await getUser();
const roles = await getRoles();
commit('authenticated', { user, roles });
const authorities = await getAuthorities();
commit('authenticated', { user, authorities });
},
logout: async ({ commit }) => {
await doLogout();
Expand Down

0 comments on commit 0a9ab01

Please sign in to comment.