restore session from localstorage

This commit is contained in:
j3d1 2024-01-07 23:03:22 +01:00
parent 54e5ba6e8e
commit f9a95317a2
2 changed files with 16 additions and 10 deletions

View file

@ -68,6 +68,10 @@ const routes = [
path: 'users/', name: 'users', component: AccessControl, meta: path: 'users/', name: 'users', component: AccessControl, meta:
{requiresAuth: true, requiresPermission: 'delete_event'} {requiresAuth: true, requiresPermission: 'delete_event'}
}, },
{
path: 'boxes/', name: 'admin_boxes', component: BoxesAdmin, meta:
{requiresAuth: true, requiresPermission: 'delete_event'}
},
] ]
}, },
{path: '/user', name: 'user', component: Empty, meta: {requiresAuth: true}}, {path: '/user', name: 'user', component: Empty, meta: {requiresAuth: true}},

View file

@ -110,12 +110,14 @@ const store = new Vuex.Store({
}, },
isLoggedIn(state) { isLoggedIn(state) {
if (!state.local_loaded) { if (!state.local_loaded) {
state.remember = localStorage.getItem('remember') === 'true' state.remember = localStorage.getItem('remember') === 'true';
state.user = localStorage.getItem('user') state.user = localStorage.getItem('user');
state.userPermissions = JSON.parse(localStorage.getItem('permissions') || '[]') //state.password = localStorage.getItem('password');
state.token = localStorage.getItem('token') state.userPermissions = JSON.parse(localStorage.getItem('permissions') || '[]');
state.token_expiry = localStorage.getItem('token_expiry') state.token = localStorage.getItem('token');
state.local_loaded = true state.token_expiry = localStorage.getItem('token_expiry');
state.local_loaded = true;
axios.defaults.headers.common['Authorization'] = `Token ${state.token}`;
} }
return state.user !== null && state.token !== null; return state.user !== null && state.token !== null;
@ -234,7 +236,7 @@ const store = new Vuex.Store({
body: JSON.stringify({username: username, password: password}), body: JSON.stringify({username: username, password: password}),
credentials: 'omit' credentials: 'omit'
}).then(r => r.json()) }).then(r => r.json())
if (data.token) { if (data && data.token) {
commit('setToken', data); commit('setToken', data);
commit('setUser', username); commit('setUser', username);
commit('setPassword', password); commit('setPassword', password);
@ -251,14 +253,14 @@ const store = new Vuex.Store({
}, },
async reloadToken({commit, state}) { async reloadToken({commit, state}) {
try { try {
if (data.password) { if (state.password) {
const data = await fetch('/api/2/login/', { const data = await fetch('/api/2/login/', {
method: 'POST', method: 'POST',
headers: {'Content-Type': 'application/json'}, headers: {'Content-Type': 'application/json'},
body: JSON.stringify({username: state.user, password: state.password}), body: JSON.stringify({username: state.user, password: state.password}),
credentials: 'omit' credentials: 'omit'
}).then(r => r.json()) }).then(r => r.json()).catch(e => console.error(e))
if (data.token) { if (data && data.token) {
commit('setToken', data); commit('setToken', data);
axios.defaults.headers.common['Authorization'] = `Token ${data.token}`; axios.defaults.headers.common['Authorization'] = `Token ${data.token}`;
return true; return true;