From 8e8ee29cf6a3f443150cacf198cfc6bb3265b2d6 Mon Sep 17 00:00:00 2001 From: Gabriel Massadas Date: Mon, 26 Feb 2024 22:19:35 +0000 Subject: [PATCH] Fix basic auth not working --- packages/dashboard-v2/src/boot/auth.js | 6 +++--- .../dashboard-v2/src/pages/auth/LoginPage.vue | 2 +- packages/dashboard-v2/src/stores/auth-store.js | 16 ++++++++-------- packages/dashboard-v2/src/stores/main-store.js | 5 ++--- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/packages/dashboard-v2/src/boot/auth.js b/packages/dashboard-v2/src/boot/auth.js index fed859e..18d934a 100644 --- a/packages/dashboard-v2/src/boot/auth.js +++ b/packages/dashboard-v2/src/boot/auth.js @@ -3,13 +3,13 @@ import { useAuthStore } from 'stores/auth-store'; import { useMainStore } from "stores/main-store"; import store from 'stores/index' -export default boot(async () => { +export default boot(async ({router}) => { // Check if theres any auth token stored, if there is, try to fetch or redirect const authStore = useAuthStore(store()); - const authResp = await authStore.CheckLoginInStorage(); + const authResp = await authStore.CheckLoginInStorage(router); if (authResp === false) { // No auth token stored, try to fetch without auth or redirect const mainStore = useMainStore(store()) - await mainStore.loadServerConfigs(true) + await mainStore.loadServerConfigs(router, true) } }); diff --git a/packages/dashboard-v2/src/pages/auth/LoginPage.vue b/packages/dashboard-v2/src/pages/auth/LoginPage.vue index 28dd400..bba3603 100644 --- a/packages/dashboard-v2/src/pages/auth/LoginPage.vue +++ b/packages/dashboard-v2/src/pages/auth/LoginPage.vue @@ -67,7 +67,7 @@ export default defineComponent({ async onSubmit() { this.loading = true try { - await authStore.LogIn(this.form) + await authStore.LogIn(this.$router, this.form) this.showError = ''; } catch (error) { this.showError = error.message; diff --git a/packages/dashboard-v2/src/stores/auth-store.js b/packages/dashboard-v2/src/stores/auth-store.js index 0c78985..b073deb 100644 --- a/packages/dashboard-v2/src/stores/auth-store.js +++ b/packages/dashboard-v2/src/stores/auth-store.js @@ -14,13 +14,13 @@ export const useAuthStore = defineStore('auth', { StateUser: state => state.user }, actions: { - async LogIn(form) { + async LogIn(router, form) { const mainStore = useMainStore(); const token = btoa(form.username + ':' + form.password) api.defaults.headers.common['Authorization'] = `Basic ${token}`; try { - await mainStore.loadServerConfigs(); + await mainStore.loadServerConfigs(router); } catch (e) { console.log(e) delete api.defaults.headers.common['Authorization']; @@ -38,9 +38,9 @@ export const useAuthStore = defineStore('auth', { sessionStorage.setItem(SESSION_KEY, token); } - this.router.replace(this.router.currentRoute.value.query?.next || '/'); + router.replace(router.currentRoute.value.query?.next || '/'); }, - async CheckLoginInStorage() { + async CheckLoginInStorage(router) { let token = sessionStorage.getItem(SESSION_KEY); if (!token) { token = localStorage.getItem(SESSION_KEY); @@ -54,24 +54,24 @@ export const useAuthStore = defineStore('auth', { api.defaults.headers.common['Authorization'] = `Basic ${token}`; try { - await mainStore.loadServerConfigs(); + await mainStore.loadServerConfigs(router); } catch (e) { // Auth token expired delete api.defaults.headers.common['Authorization']; - await this.router.replace({ name: 'login', query: { next: this.router.currentRoute.fullPath } }); + await router.replace({ name: 'login', query: { next: router.currentRoute.fullPath } }); return } this.user = atob(token).split(':')[0]; this.loginMethod = 'basic' }, - LogOut() { + async LogOut(router) { localStorage.removeItem(SESSION_KEY); sessionStorage.removeItem(SESSION_KEY); this.user = ''; this.loginMethod = ''; - this.router.replace('/auth/login'); + await router.replace({ name: 'login'}); }, } }); diff --git a/packages/dashboard-v2/src/stores/main-store.js b/packages/dashboard-v2/src/stores/main-store.js index 2b683ca..0ef5035 100644 --- a/packages/dashboard-v2/src/stores/main-store.js +++ b/packages/dashboard-v2/src/stores/main-store.js @@ -26,7 +26,7 @@ export const useMainStore = defineStore('main', { this.buckets = response.data.buckets return response.data.buckets }, - async loadServerConfigs(handleError = false) { + async loadServerConfigs(router, handleError = false) { // This is the initial requests to server, that also checks if user needs auth try { @@ -51,9 +51,8 @@ export const useMainStore = defineStore('main', { } if (handleError) { - // console.log(error) if (error.response?.status === 401) { - await this.router.push({ name: 'login' }) + await router.push({ name: 'login', query: { next: router.currentRoute.value.fullPath } }) return }