From 7b8a9c0fcf56431105fb1ee631cb1ae2f502b6ec Mon Sep 17 00:00:00 2001 From: Victor Ananyev Date: Fri, 30 Dec 2022 22:28:44 +0100 Subject: [PATCH 1/5] check for auth token on app refresh --- .../static/dispatch/src/auth/store.js | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/dispatch/static/dispatch/src/auth/store.js b/src/dispatch/static/dispatch/src/auth/store.js index 4548da0c8f1c..90e278572e08 100644 --- a/src/dispatch/static/dispatch/src/auth/store.js +++ b/src/dispatch/static/dispatch/src/auth/store.js @@ -15,14 +15,37 @@ const getDefaultSelectedState = () => { } } +function authCurrentUser (base, token, bad_token_ok=false) { + let decodedToken + let loggedIn + try { + decodedToken = jwt_decode(token) + loggedIn = true + } catch (e) { + if (!bad_token_ok) { + throw e + } + token = null + decodedToken = {} + loggedIn = false + } + + return { + ...base, + loggedIn: loggedIn, + token: token, + ...decodedToken + } +} + const state = { - currentUser: { + currentUser: authCurrentUser({ loggedIn: false, token: null, email: "", projects: [], role: null, - }, + }, localStorage.getItem("token"), true), selected: { ...getDefaultSelectedState(), }, @@ -178,12 +201,7 @@ const mutations = { state.loading = value }, SET_USER_LOGIN(state, token) { - state.currentUser = { - ...state.currentUser, - ...jwt_decode(token), - token: token, - loggedIn: true, - } + state.currentUser = authCurrentUser(state.currentUser, token) localStorage.setItem("token", token) }, SET_USER_LOGOUT(state) { From 88cd0839102381e784a4debef0670723a5598998 Mon Sep 17 00:00:00 2001 From: Victor Ananyev Date: Thu, 5 Jan 2023 20:24:24 +0100 Subject: [PATCH 2/5] fix linting --- .../static/dispatch/src/auth/store.js | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/dispatch/static/dispatch/src/auth/store.js b/src/dispatch/static/dispatch/src/auth/store.js index 90e278572e08..7e03b8de9093 100644 --- a/src/dispatch/static/dispatch/src/auth/store.js +++ b/src/dispatch/static/dispatch/src/auth/store.js @@ -15,7 +15,7 @@ const getDefaultSelectedState = () => { } } -function authCurrentUser (base, token, bad_token_ok=false) { +function authCurrentUser(base, token, bad_token_ok = false) { let decodedToken let loggedIn try { @@ -34,18 +34,22 @@ function authCurrentUser (base, token, bad_token_ok=false) { ...base, loggedIn: loggedIn, token: token, - ...decodedToken + ...decodedToken, } } const state = { - currentUser: authCurrentUser({ - loggedIn: false, - token: null, - email: "", - projects: [], - role: null, - }, localStorage.getItem("token"), true), + currentUser: authCurrentUser( + { + loggedIn: false, + token: null, + email: "", + projects: [], + role: null, + }, + localStorage.getItem("token"), + true + ), selected: { ...getDefaultSelectedState(), }, From e19e4bf1bb5dfdc3faafd2bdd3c802bd39e4704a Mon Sep 17 00:00:00 2001 From: Victor Ananyev Date: Tue, 10 Jan 2023 22:39:05 +0100 Subject: [PATCH 3/5] Revert "fix linting" This reverts commit 88cd0839102381e784a4debef0670723a5598998. --- .../static/dispatch/src/auth/store.js | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/dispatch/static/dispatch/src/auth/store.js b/src/dispatch/static/dispatch/src/auth/store.js index 7e03b8de9093..90e278572e08 100644 --- a/src/dispatch/static/dispatch/src/auth/store.js +++ b/src/dispatch/static/dispatch/src/auth/store.js @@ -15,7 +15,7 @@ const getDefaultSelectedState = () => { } } -function authCurrentUser(base, token, bad_token_ok = false) { +function authCurrentUser (base, token, bad_token_ok=false) { let decodedToken let loggedIn try { @@ -34,22 +34,18 @@ function authCurrentUser(base, token, bad_token_ok = false) { ...base, loggedIn: loggedIn, token: token, - ...decodedToken, + ...decodedToken } } const state = { - currentUser: authCurrentUser( - { - loggedIn: false, - token: null, - email: "", - projects: [], - role: null, - }, - localStorage.getItem("token"), - true - ), + currentUser: authCurrentUser({ + loggedIn: false, + token: null, + email: "", + projects: [], + role: null, + }, localStorage.getItem("token"), true), selected: { ...getDefaultSelectedState(), }, From 02c17387e6b094e93db2816ec2141bd97e3d7507 Mon Sep 17 00:00:00 2001 From: Victor Ananyev Date: Tue, 10 Jan 2023 22:39:27 +0100 Subject: [PATCH 4/5] Revert "check for auth token on app refresh" This reverts commit 7b8a9c0fcf56431105fb1ee631cb1ae2f502b6ec. --- .../static/dispatch/src/auth/store.js | 34 +++++-------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/src/dispatch/static/dispatch/src/auth/store.js b/src/dispatch/static/dispatch/src/auth/store.js index 90e278572e08..4548da0c8f1c 100644 --- a/src/dispatch/static/dispatch/src/auth/store.js +++ b/src/dispatch/static/dispatch/src/auth/store.js @@ -15,37 +15,14 @@ const getDefaultSelectedState = () => { } } -function authCurrentUser (base, token, bad_token_ok=false) { - let decodedToken - let loggedIn - try { - decodedToken = jwt_decode(token) - loggedIn = true - } catch (e) { - if (!bad_token_ok) { - throw e - } - token = null - decodedToken = {} - loggedIn = false - } - - return { - ...base, - loggedIn: loggedIn, - token: token, - ...decodedToken - } -} - const state = { - currentUser: authCurrentUser({ + currentUser: { loggedIn: false, token: null, email: "", projects: [], role: null, - }, localStorage.getItem("token"), true), + }, selected: { ...getDefaultSelectedState(), }, @@ -201,7 +178,12 @@ const mutations = { state.loading = value }, SET_USER_LOGIN(state, token) { - state.currentUser = authCurrentUser(state.currentUser, token) + state.currentUser = { + ...state.currentUser, + ...jwt_decode(token), + token: token, + loggedIn: true, + } localStorage.setItem("token", token) }, SET_USER_LOGOUT(state) { From b122e41a55f95fd608d2ddbe5d114531160d585e Mon Sep 17 00:00:00 2001 From: Victor Ananyev Date: Tue, 10 Jan 2023 22:44:28 +0100 Subject: [PATCH 5/5] make basic auth provider default in vue --- src/dispatch/static/dispatch/src/router/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/dispatch/static/dispatch/src/router/index.js b/src/dispatch/static/dispatch/src/router/index.js index 43e63d47e79a..4b6df51dca40 100644 --- a/src/dispatch/static/dispatch/src/router/index.js +++ b/src/dispatch/static/dispatch/src/router/index.js @@ -18,6 +18,9 @@ const router = new Router({ routes: routes, }) +const authProviderSlug = + import.meta.env.VITE_DISPATCH_AUTHENTICATION_PROVIDER_SLUG || "dispatch-auth-provider-basic" + const originalPush = Router.prototype.push Router.prototype.push = function push(location, onResolve, onReject) { if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject) @@ -36,7 +39,6 @@ router.beforeEach((to, from, next) => { store.dispatch("app/setLoading", true) if (to.matched.some((record) => record.meta.requiresAuth)) { if (!store.state.auth.currentUser.loggedIn) { - let authProviderSlug = import.meta.env.VITE_DISPATCH_AUTHENTICATION_PROVIDER_SLUG if (authProviderSlug === "dispatch-auth-provider-basic") { basicAuthProvider.login(to, from, next) } else if (authProviderSlug === "dispatch-auth-provider-pkce") {