Skip to content
Snippets Groups Projects
Commit ec823496 authored by robinwilliam.hundt's avatar robinwilliam.hundt
Browse files

token/username etc. is saved in sessionstorage

If the user now accidentally presses reload, the relevant session state (token, etc. ) will be retrieved from the session storage.
parent 49dcf645
Branches
Tags
1 merge request!23Resolve "Logout of tutors after inactivity"
Pipeline #
...@@ -63,10 +63,10 @@ ...@@ -63,10 +63,10 @@
this.loading = true this.loading = true
this.getJWTToken(this.credentials).then(() => { this.getJWTToken(this.credentials).then(() => {
this.loading = false this.loading = false
this.$router.push('/student/')
this.getExamModule() this.getExamModule()
this.getUserRole() this.getUserRole()
this.getJWTTimeDelta() this.getJWTTimeDelta()
this.$router.push('/student/')
}).catch(() => { this.loading = false }) }).catch(() => { this.loading = false })
} }
} }
......
...@@ -63,6 +63,8 @@ router.beforeEach((to, from, next) => { ...@@ -63,6 +63,8 @@ router.beforeEach((to, from, next) => {
} else { } else {
const now = new Date() const now = new Date()
if (now - store.state.logInTime > store.state.jwtTimeDelta * 1000) { if (now - store.state.logInTime > store.state.jwtTimeDelta * 1000) {
console.log(now)
console.log(store.state.logInTime)
store.dispatch('logout').then(() => { store.dispatch('logout').then(() => {
store.commit('API_FAIL', 'You\'ve been logged out due to inactivity') store.commit('API_FAIL', 'You\'ve been logged out due to inactivity')
next('/') next('/')
......
import axios from 'axios' import axios from 'axios'
let ax = axios.create({ let ax = axios.create({
baseURL: 'http://localhost:8000/' baseURL: 'http://localhost:8000/',
headers: {'Authorization': 'JWT ' + sessionStorage.getItem('jwtToken')}
}) })
export default ax export default ax
...@@ -14,12 +14,12 @@ const store = new Vuex.Store({ ...@@ -14,12 +14,12 @@ const store = new Vuex.Store({
studentPage studentPage
}, },
state: { state: {
token: '', token: sessionStorage.getItem('jwtToken'),
loggedIn: false, loggedIn: !!sessionStorage.getItem('jwtToken'),
logInTime: {}, logInTime: sessionStorage.getItem('logInTime'),
username: '', username: sessionStorage.getItem('username'),
jwtTimeDelta: 0, jwtTimeDelta: sessionStorage.getItem('jwtTimeDelta'),
userRole: '', userRole: sessionStorage.getItem('userRole'),
error: '', error: '',
examInstance: '' examInstance: ''
}, },
...@@ -34,21 +34,26 @@ const store = new Vuex.Store({ ...@@ -34,21 +34,26 @@ const store = new Vuex.Store({
}, },
'SET_JWT_TOKEN': function (state, token) { 'SET_JWT_TOKEN': function (state, token) {
state.token = token state.token = token
state.logInTime = new Date() state.logInTime = Date.now()
ax.defaults.headers.common['Authorization'] = 'JWT ' + token ax.defaults.headers['Authorization'] = 'JWT ' + token
sessionStorage.setItem('jwtToken', token)
sessionStorage.setItem('logInTime', state.logInTime)
}, },
'SET_JWT_TIME_DELTA': function (state, timeDelta) { 'SET_JWT_TIME_DELTA': function (state, timeDelta) {
state.jwtTimeDelta = timeDelta state.jwtTimeDelta = timeDelta
sessionStorage.setItem('jwtTimeDelta', timeDelta)
}, },
'LOGIN': function (state, username) { 'LOGIN': function (state, username) {
state.loggedIn = true state.loggedIn = true
state.username = username state.username = username
sessionStorage.setItem('username', username)
}, },
'LOGOUT': function (state) { 'LOGOUT': function (state) {
state.loggedIn = false state.loggedIn = false
}, },
'SET_USER_ROLE': function (state, userRole) { 'SET_USER_ROLE': function (state, userRole) {
state.userRole = userRole state.userRole = userRole
sessionStorage.setItem('userRole', userRole)
}, },
'SET_EXAM_INSTANCE': function (state, examInstance) { 'SET_EXAM_INSTANCE': function (state, examInstance) {
state.examInstance = examInstance state.examInstance = examInstance
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment