Stabilize session handling
This commit is contained in:
@@ -8,7 +8,7 @@ const guard = (to, from, next) => {
|
|||||||
if (store.state.loading) {
|
if (store.state.loading) {
|
||||||
next(false);
|
next(false);
|
||||||
} else if (store.state.loggedIn) {
|
} else if (store.state.loggedIn) {
|
||||||
if (to.meta.requiresTeamer && !store.state.isTeamer) {
|
if (to.meta.requiresTeamer && !store.getters.isTeamer) {
|
||||||
next(false);
|
next(false);
|
||||||
} else {
|
} else {
|
||||||
next();
|
next();
|
||||||
|
|||||||
@@ -26,8 +26,10 @@ export default new Vuex.Store({
|
|||||||
state: {
|
state: {
|
||||||
config: config,
|
config: config,
|
||||||
loading: false,
|
loading: false,
|
||||||
|
authToken: sessionStorage.getItem('token'),
|
||||||
loggedIn: !!sessionStorage.getItem('token'),
|
loggedIn: !!sessionStorage.getItem('token'),
|
||||||
isTeamer: JSON.parse(sessionStorage.getItem('teamerId')) !== null,
|
isTeamer: JSON.parse(sessionStorage.getItem('teamerId')) !== null,
|
||||||
|
teamerId: JSON.parse(sessionStorage.getItem('teamerId')),
|
||||||
countries: [],
|
countries: [],
|
||||||
abilities: [],
|
abilities: [],
|
||||||
timeFrames: [],
|
timeFrames: [],
|
||||||
@@ -38,22 +40,30 @@ export default new Vuex.Store({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
mutations: {
|
mutations: {
|
||||||
loginSuccess (state) {
|
loginSuccess (state, token) {
|
||||||
state.loggedIn = true;
|
state.loggedIn = true;
|
||||||
|
state.authToken = token;
|
||||||
|
sessionStorage.setItem('token', token);
|
||||||
},
|
},
|
||||||
loginFailure (state) {
|
loginFailure (state) {
|
||||||
state.loggedIn = false;
|
state.loggedIn = false;
|
||||||
},
|
},
|
||||||
logout (state) {
|
logout (state) {
|
||||||
state.loggedIn = false;
|
state.loggedIn = false;
|
||||||
state.teamer = false;
|
state.isTeamer = false;
|
||||||
|
state.teamerId = null;
|
||||||
|
state.authToken = null;
|
||||||
|
sessionStorage.removeItem('token');
|
||||||
|
sessionStorage.removeItem('teamerId');
|
||||||
},
|
},
|
||||||
alert (state, payload) {
|
alert (state, payload) {
|
||||||
state.message.text = payload.message;
|
state.message.text = payload.message;
|
||||||
state.message.class = payload.class;
|
state.message.class = payload.class;
|
||||||
},
|
},
|
||||||
isTeamer (state) {
|
isTeamer (state, teamerId) {
|
||||||
state.teamer = true;
|
state.isTeamer = true;
|
||||||
|
state.teamerId = teamerId;
|
||||||
|
sessionStorage.setItem('teamerId', teamerId);
|
||||||
},
|
},
|
||||||
loadingBegin (state) {
|
loadingBegin (state) {
|
||||||
state.loading = true;
|
state.loading = true;
|
||||||
@@ -88,12 +98,11 @@ export default new Vuex.Store({
|
|||||||
method: 'post',
|
method: 'post',
|
||||||
withCredentials: true
|
withCredentials: true
|
||||||
}).then((response) => {
|
}).then((response) => {
|
||||||
sessionStorage.setItem('token', response.data.token);
|
let teamerId = parseInt(response.data.teamer_id);
|
||||||
sessionStorage.setItem('teamerId', response.data.teamer_id);
|
if (teamerId > 0) {
|
||||||
if (response.data.teamer_id) {
|
commit('isTeamer', teamerId);
|
||||||
commit('isTeamer');
|
|
||||||
}
|
}
|
||||||
commit('loginSuccess');
|
commit('loginSuccess', response.data.token);
|
||||||
commit('loadingFinish');
|
commit('loadingFinish');
|
||||||
}).catch((error) => {
|
}).catch((error) => {
|
||||||
commit('loginFailure');
|
commit('loginFailure');
|
||||||
@@ -105,15 +114,13 @@ export default new Vuex.Store({
|
|||||||
throw error;
|
throw error;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
logout({ commit }) {
|
logout({ commit, state }) {
|
||||||
return axios({
|
return axios({
|
||||||
url: '/api/logout',
|
url: '/api/logout',
|
||||||
headers: {
|
headers: {
|
||||||
'x-auth-token': sessionStorage.getItem('token')
|
'x-auth-token': state.authToken
|
||||||
}
|
}
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
sessionStorage.removeItem('token');
|
|
||||||
sessionStorage.removeItem('teamerId');
|
|
||||||
commit('logout');
|
commit('logout');
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -157,11 +164,14 @@ export default new Vuex.Store({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
getters: {
|
getters: {
|
||||||
authToken () {
|
authToken: state => {
|
||||||
return sessionStorage.getItem('token')
|
return state.authToken;
|
||||||
},
|
},
|
||||||
teamerId () {
|
isTeamer: state => {
|
||||||
return sessionStorage.getItem('teamerId')
|
return state.isTeamer;
|
||||||
|
},
|
||||||
|
teamerId: state => {
|
||||||
|
return state.teamerId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
<router-link :to="{ name: 'newsletter' }" tag="li" active-class="active" :class="{ disabled: loading }"><a>Newsletter</a></router-link>
|
<router-link :to="{ name: 'newsletter' }" tag="li" active-class="active" :class="{ disabled: loading }"><a>Newsletter</a></router-link>
|
||||||
<router-link :to="{ name: 'profile' }" tag="li" active-class="active" :class="{ disabled: loading }"><a>Mein Profil</a></router-link>
|
<router-link :to="{ name: 'profile' }" tag="li" active-class="active" :class="{ disabled: loading }"><a>Mein Profil</a></router-link>
|
||||||
<router-link :to="{ name: 'events' }" tag="li" active-class="active" :class="{ disabled: loading }"><a>Reise-Historie</a></router-link>
|
<router-link :to="{ name: 'events' }" tag="li" active-class="active" :class="{ disabled: loading }"><a>Reise-Historie</a></router-link>
|
||||||
<router-link :to="{ name: 'teamer' }" tag="li" active-class="active" :class="{ disabled: loading }" v-if="isTeamer"><a>Teamer</a></router-link>
|
<router-link :to="{ name: 'teamer' }" tag="li" active-class="active" :class="{ disabled: loading }" v-if="$store.getters.isTeamer"><a>Teamer</a></router-link>
|
||||||
</ul>
|
</ul>
|
||||||
<ul class="nav navbar-nav navbar-right">
|
<ul class="nav navbar-nav navbar-right">
|
||||||
<li :class="{ disabled: loading }"><a href="#" @click.prevent="logout()">Logout</a></li>
|
<li :class="{ disabled: loading }"><a href="#" @click.prevent="logout()">Logout</a></li>
|
||||||
@@ -62,9 +62,6 @@
|
|||||||
isLoggedIn () {
|
isLoggedIn () {
|
||||||
return this.$store.state.loggedIn;
|
return this.$store.state.loggedIn;
|
||||||
},
|
},
|
||||||
isTeamer () {
|
|
||||||
return this.$store.state.isTeamer;
|
|
||||||
},
|
|
||||||
loaderUrl () {
|
loaderUrl () {
|
||||||
return this.$store.state.config.loaderUrl;
|
return this.$store.state.config.loaderUrl;
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -5,12 +5,12 @@ export default {
|
|||||||
namespaced: true,
|
namespaced: true,
|
||||||
state: {},
|
state: {},
|
||||||
actions: {
|
actions: {
|
||||||
load ({ commit }) {
|
load ({ commit, rootState }) {
|
||||||
commit('loadingBegin', null, { root: true });
|
commit('loadingBegin', null, { root: true });
|
||||||
|
|
||||||
const client = axios.create({
|
const client = axios.create({
|
||||||
headers: {
|
headers: {
|
||||||
'X-Auth-Token': sessionStorage.getItem('token')
|
'X-Auth-Token': rootState.authToken
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -29,12 +29,12 @@ export default {
|
|||||||
throw error;
|
throw error;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
save ({ commit }, data) {
|
save ({ commit, rootState }, data) {
|
||||||
commit('loadingBegin', null, { root: true });
|
commit('loadingBegin', null, { root: true });
|
||||||
|
|
||||||
const client = axios.create({
|
const client = axios.create({
|
||||||
headers: {
|
headers: {
|
||||||
'X-Auth-Token': sessionStorage.getItem('token')
|
'X-Auth-Token': rootState.authToken
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -5,12 +5,12 @@ export default {
|
|||||||
namespaced: true,
|
namespaced: true,
|
||||||
state: {},
|
state: {},
|
||||||
actions: {
|
actions: {
|
||||||
load ({ commit }) {
|
load ({ commit, rootState }) {
|
||||||
commit('loadingBegin', null, { root: true });
|
commit('loadingBegin', null, { root: true });
|
||||||
|
|
||||||
const client = axios.create({
|
const client = axios.create({
|
||||||
headers: {
|
headers: {
|
||||||
'X-Auth-Token': sessionStorage.getItem('token')
|
'X-Auth-Token': rootState.authToken
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -29,12 +29,12 @@ export default {
|
|||||||
throw error;
|
throw error;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
save ({ commit }, data) {
|
save ({ commit, rootState }, data) {
|
||||||
commit('loadingBegin', null, { root: true });
|
commit('loadingBegin', null, { root: true });
|
||||||
|
|
||||||
const client = axios.create({
|
const client = axios.create({
|
||||||
headers: {
|
headers: {
|
||||||
'X-Auth-Token': sessionStorage.getItem('token')
|
'X-Auth-Token': rootState.authToken
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -5,12 +5,12 @@ export default {
|
|||||||
namespaced: true,
|
namespaced: true,
|
||||||
state: {},
|
state: {},
|
||||||
actions: {
|
actions: {
|
||||||
load ({ commit }) {
|
load ({ commit, rootState }) {
|
||||||
commit('loadingBegin', null, { root: true });
|
commit('loadingBegin', null, { root: true });
|
||||||
|
|
||||||
const client = axios.create({
|
const client = axios.create({
|
||||||
headers: {
|
headers: {
|
||||||
'X-Auth-Token': sessionStorage.getItem('token')
|
'X-Auth-Token': rootState.authToken
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -5,12 +5,12 @@ export default {
|
|||||||
namespaced: true,
|
namespaced: true,
|
||||||
state: {},
|
state: {},
|
||||||
actions: {
|
actions: {
|
||||||
registration ({ commit }, data) {
|
registration ({ commit, rootState }, data) {
|
||||||
commit('loadingBegin', null, { root: true });
|
commit('loadingBegin', null, { root: true });
|
||||||
|
|
||||||
const client = axios.create({
|
const client = axios.create({
|
||||||
headers: {
|
headers: {
|
||||||
'X-Auth-Token': sessionStorage.getItem('token')
|
'X-Auth-Token': rootState.authToken
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -5,16 +5,16 @@ export default {
|
|||||||
namespaced: true,
|
namespaced: true,
|
||||||
state: {},
|
state: {},
|
||||||
actions: {
|
actions: {
|
||||||
load ({ commit }) {
|
load ({ commit, rootState }) {
|
||||||
commit('loadingBegin', null, { root: true });
|
commit('loadingBegin', null, { root: true });
|
||||||
|
|
||||||
const client = axios.create({
|
const client = axios.create({
|
||||||
headers: {
|
headers: {
|
||||||
'X-Auth-Token': sessionStorage.getItem('token')
|
'X-Auth-Token': rootState.authToken
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return client.get('/api/teamers/' + sessionStorage.getItem('teamerId'))
|
return client.get('/api/teamers/' + rootState.teamerId)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
commit('loadingFinish', null, { root: true });
|
commit('loadingFinish', null, { root: true });
|
||||||
return response.data;
|
return response.data;
|
||||||
@@ -29,16 +29,16 @@ export default {
|
|||||||
throw error;
|
throw error;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
save ({ commit }, data) {
|
save ({ commit, rootState }, data) {
|
||||||
commit('loadingBegin', null, { root: true });
|
commit('loadingBegin', null, { root: true });
|
||||||
|
|
||||||
const client = axios.create({
|
const client = axios.create({
|
||||||
headers: {
|
headers: {
|
||||||
'X-Auth-Token': sessionStorage.getItem('token')
|
'X-Auth-Token': rootState.authToken
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return client.put('/api/teamers/' + sessionStorage.getItem('teamerId'), data)
|
return client.put('/api/teamers/' + rootState.teamerId, data)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
commit('loadingFinish', null, { root: true });
|
commit('loadingFinish', null, { root: true });
|
||||||
commit('alert', {
|
commit('alert', {
|
||||||
|
|||||||
Reference in New Issue
Block a user