Skip to content

Commit

Permalink
chore: redirect from dashboard, prevent authentication error
Browse files Browse the repository at this point in the history
  • Loading branch information
EwoutV committed Apr 15, 2024
1 parent efa6c9c commit 14795ce
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 35 deletions.
24 changes: 16 additions & 8 deletions frontend/src/router/guards/authentication.guard.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import { type RouteLocationNormalized } from 'vue-router';
import { useAuthStore } from '@/store/authentication.store.ts';
import { storeToRefs } from 'pinia';
import { useMessagesStore } from '@/store/messages.store.ts';
import { i18n } from '@/config/i18n.ts';

export async function AuthenticationGuard(to: RouteLocationNormalized): Promise<{ name: string } | undefined> {
const { t } = i18n.global;
const { addErrorMessage } = useMessagesStore();
const { refreshUser } = useAuthStore();
const { intent } = storeToRefs(useAuthStore());
const { intent, isAuthenticated } = storeToRefs(useAuthStore());

const { isAuthenticated } = storeToRefs(useAuthStore());

if (!['login', 'verify'].includes(to.name as string)) {
if (!isAuthenticated.value) {
if (!isAuthenticated.value && !['login', 'verify'].includes(<string> to.name)) {
try {
await refreshUser();
} catch (error: any) {
const detail: string = error.response.data.detail.toString();

if (!isAuthenticated.value) {
intent.value = to.fullPath;
return { name: 'login' };
if (to.name !== 'dashboard') {
addErrorMessage(t('toasts.messages.error'), detail);
}
}

if (!isAuthenticated.value) {
intent.value = to.fullPath;
return { name: 'login' };
}
}
}
45 changes: 18 additions & 27 deletions frontend/src/store/authentication.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,20 @@ export const useAuthStore = defineStore('auth', () => {
* Refresh the user objects in the API endpoint.
*/
async function refreshUser(): Promise<void> {
// Display toast messages.
const { addErrorMessage } = useMessagesStore();

// Get the user information (using a cookie).
try {
// Get the user information when it is not set.
if (user.value === null) {
const response = await axios.get(endpoints.auth.whoami);
user.value = User.fromJSON(response.data as User);
}

if (view.value === null || !user.value.hasRoles(view.value)) {
view.value = user.value.roles[0];
}
if (user.value === null) {
const response = await axios.get(endpoints.auth.whoami);
user.value = User.fromJSON(response.data as User);
}

// Get the role information.
await initUser();
} catch (error: any) {
addErrorMessage(error.response.statusText as string, error.response.data.detail as string);
if (view.value === null || !user.value.hasRoles(view.value)) {
view.value = user.value.roles[0];
}

// Get the role information.
// This throws an error on a failed login, to be caught by the caller.
// Get the user information when it is not set.
await initUser();
}

/**
Expand All @@ -103,18 +97,15 @@ export const useAuthStore = defineStore('auth', () => {
async function login(ticket: string): Promise<void> {
// Display toast messages.
const { t } = useI18n();
const { addSuccessMessage, addErrorMessage } = useMessagesStore();
const { addSuccessMessage } = useMessagesStore();

// Attempt to log in the user using the ticket.
try {
await axios.post(endpoints.auth.token.obtain, {
ticket,
});

addSuccessMessage(t('toasts.messages.success'), t('toasts.messages.login.success'));
} catch (error: any) {
addErrorMessage(t('toasts.messages.error'), error.response.data.detail as string);
}
// This throws an error on a failed login, to be caught by the caller.
await axios.post(endpoints.auth.token.obtain, {
ticket,
});

addSuccessMessage(t('toasts.messages.success'), t('toasts.messages.login.success'));
}

/**
Expand Down

0 comments on commit 14795ce

Please sign in to comment.