From 598f758332429fe7765ad32a0e47fe57cab6a2c1 Mon Sep 17 00:00:00 2001 From: jedi Date: Tue, 24 Dec 2024 15:55:07 +0100 Subject: [PATCH] reduce shadow logouts, by actually logging out when token fails or is expired --- web/src/persistent-state-plugin/index.js | 3 ++ web/src/store.js | 9 ++++++ web/src/utils.js | 37 ++++++++++++++++++++---- web/src/views/Login.vue | 2 +- web/src/views/Register.vue | 2 +- 5 files changed, 46 insertions(+), 7 deletions(-) diff --git a/web/src/persistent-state-plugin/index.js b/web/src/persistent-state-plugin/index.js index b7cff69..14f5577 100644 --- a/web/src/persistent-state-plugin/index.js +++ b/web/src/persistent-state-plugin/index.js @@ -20,6 +20,9 @@ export default (config) => (store) => { } }); store.state[config.isLoadedKey] = true; + if ('validate' in config) { + config.validate(store.state); + } } const reload = initialize; diff --git a/web/src/store.js b/web/src/store.js index aa4820d..fa86f94 100644 --- a/web/src/store.js +++ b/web/src/store.js @@ -515,6 +515,15 @@ const store = createStore({ prefix: "lf_", debug: false, isLoadedKey: "persistent_loaded", + validate: (state) => { + if (state.user && state.user.expiry && state.user.token) { + const as_date = new Date(state.user.expiry); + if (as_date < new Date()) { + state.user.token = null; + state.user.expiry = null; + } + } + }, state: ["remember", "user", "events", "lastUsed",] }), sharedStatePlugin({ debug: false, diff --git a/web/src/utils.js b/web/src/utils.js index ebc911a..d753623 100644 --- a/web/src/utils.js +++ b/web/src/utils.js @@ -1,3 +1,5 @@ +import store from '@/store' + function ticketStateColorLookup(ticket) { if (ticket.startsWith('closed_')) { return 'secondary'; @@ -36,6 +38,8 @@ const http = { "Authorization": `Token ${token}`, }, }); + if (response.status === 401) + throw {http_status: response.status}; const success = response.status === 200 || response.status === 201; return {data: await response.json() || {}, success}; }, @@ -51,6 +55,8 @@ const http = { }, body: JSON.stringify(data), }); + if (response.status === 401) + throw {http_status: response.status}; const success = response.status === 200 || response.status === 201; return {data: await response.json() || {}, success}; }, @@ -66,6 +72,8 @@ const http = { }, body: JSON.stringify(data), }); + if (response.status === 401) + throw {http_status: response.status}; const success = response.status === 200 || response.status === 201; return {data: await response.json() || {}, success}; }, @@ -81,6 +89,8 @@ const http = { }, body: JSON.stringify(data), }); + if (response.status === 401) + throw {http_status: response.status}; const success = response.status === 200 || response.status === 201; return {data: await response.json() || {}, success}; }, @@ -95,17 +105,34 @@ const http = { "Authorization": `Token ${token}`, }, }); + if (response.status === 401) + throw {http_status: response.status}; const success = response.status === 204; return {data: await response.text() || {}, success}; } } const http_session = token => ({ - get: async (url) => await http.get(url, token), - post: async (url, data) => await http.post(url, data, token), - put: async (url, data) => await http.put(url, data, token), - patch: async (url, data) => await http.patch(url, data, token), - delete: async (url) => await http.delete(url, token), + get: async (url) => await http.get(url, token).catch((e) => { + if (e.http_status === 401) store.commit('logout'); + return {data: {}, success: false}; + }), + post: async (url, data) => await http.post(url, data, token).catch((e) => { + if (e.http_status === 401) store.commit('logout'); + return {data: {}, success: false}; + }), + put: async (url, data) => await http.put(url, data, token).catch((e) => { + if (e.http_status === 401) store.commit('logout'); + return {data: {}, success: false}; + }), + patch: async (url, data) => await http.patch(url, data, token).catch((e) => { + if (e.http_status === 401) store.commit('logout'); + return {data: {}, success: false}; + }), + delete: async (url) => await http.delete(url, token).catch((e) => { + if (e.http_status === 401) store.commit('logout'); + return {data: {}, success: false}; + }), }); export {ticketStateColorLookup, ticketStateIconLookup, http, http_session}; \ No newline at end of file diff --git a/web/src/views/Login.vue b/web/src/views/Login.vue index 8cff6c2..d002cc6 100644 --- a/web/src/views/Login.vue +++ b/web/src/views/Login.vue @@ -72,7 +72,7 @@ export default { name: 'Login', data() { return { - msg: 'Welcome to ' + window.location.hostname, + msg: 'Lost&Found Team Login', username: '', password: '', remember: false diff --git a/web/src/views/Register.vue b/web/src/views/Register.vue index 11ed693..15412f7 100644 --- a/web/src/views/Register.vue +++ b/web/src/views/Register.vue @@ -87,7 +87,7 @@ export default { name: 'Register', data() { return { - msg: 'Register', + msg: 'Register as team member', password2: '', form: { username: '',