Skip to content

Commit

Permalink
Fix basic auth not working
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym committed Feb 26, 2024
1 parent ec43577 commit 8e8ee29
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
6 changes: 3 additions & 3 deletions packages/dashboard-v2/src/boot/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
});
2 changes: 1 addition & 1 deletion packages/dashboard-v2/src/pages/auth/LoginPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions packages/dashboard-v2/src/stores/auth-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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);
Expand All @@ -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'});
},
}
});
5 changes: 2 additions & 3 deletions packages/dashboard-v2/src/stores/main-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down

0 comments on commit 8e8ee29

Please sign in to comment.