From 1e64d439118d8927e678663dadfde2aa638dfcb3 Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Fri, 19 Sep 2025 18:15:56 +0200 Subject: [PATCH] Faster frontend authentication. --- frontend/src/router/index.js | 3 --- frontend/src/stores/auth.store.js | 17 ++++------------- frontend/src/views/login/LoginCallbackView.vue | 6 +----- modoboa/core/commands/templates/settings.py.tpl | 1 - test_project/test_project/settings.py | 1 - 5 files changed, 5 insertions(+), 23 deletions(-) diff --git a/frontend/src/router/index.js b/frontend/src/router/index.js index d745f7c30..c8afeb1f5 100644 --- a/frontend/src/router/index.js +++ b/frontend/src/router/index.js @@ -490,9 +490,6 @@ router.beforeEach(async (to, from, next) => { const previousPage = window.location.href sessionStorage.setItem('previousPage', previousPage) const authStore = useAuthStore() - if (!authStore.authUser) { - await authStore.initialize() - } const isAuth = await authStore.validateAccess() if (isAuth) { if (to.meta.allowedRoles !== undefined) { diff --git a/frontend/src/stores/auth.store.js b/frontend/src/stores/auth.store.js index 5bd4899f8..275a1b9c8 100644 --- a/frontend/src/stores/auth.store.js +++ b/frontend/src/stores/auth.store.js @@ -106,17 +106,6 @@ export const useAuthStore = defineStore('auth', () => { }) } - async function initialize() { - if (isAuthenticated.value) { - return null - } - const user = await manager.getUser() - if (!user) { - return null - } - return fetchUser() - } - async function validateAccess() { const user = await manager.getUser() if (!user || user.expired) { @@ -124,6 +113,9 @@ export const useAuthStore = defineStore('auth', () => { } repository.defaults.headers.common.Authorization = `Bearer ${user.access_token}` repository.defaults.headers.post['Content-Type'] = 'application/json' + if (!isAuthenticated.value) { + await fetchUser() + } return true } @@ -137,7 +129,7 @@ export const useAuthStore = defineStore('auth', () => { async function completeLogin(redirectUrl) { try { - const user = await manager.signinRedirectCallback() + const user = await manager.signinCallback() isAuthenticated.value = true const previousPage = sessionStorage.getItem('previousPage') // Redirect the user to the previous page if available @@ -204,7 +196,6 @@ export const useAuthStore = defineStore('auth', () => { addFidoCred, deleteFidoCreds, editFidoCred, - initialize, login, $reset, updateAccount, diff --git a/frontend/src/views/login/LoginCallbackView.vue b/frontend/src/views/login/LoginCallbackView.vue index ca47858be..c3d8e7f07 100644 --- a/frontend/src/views/login/LoginCallbackView.vue +++ b/frontend/src/views/login/LoginCallbackView.vue @@ -1,5 +1,4 @@ diff --git a/modoboa/core/commands/templates/settings.py.tpl b/modoboa/core/commands/templates/settings.py.tpl index 8eb4cf226..4b21ab032 100644 --- a/modoboa/core/commands/templates/settings.py.tpl +++ b/modoboa/core/commands/templates/settings.py.tpl @@ -235,7 +235,6 @@ REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'oauth2_provider.contrib.rest_framework.OAuth2Authentication', 'rest_framework.authentication.TokenAuthentication', - 'rest_framework.authentication.SessionAuthentication', ), 'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema', 'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.NamespaceVersioning', diff --git a/test_project/test_project/settings.py b/test_project/test_project/settings.py index 3a9c79627..545f28f08 100644 --- a/test_project/test_project/settings.py +++ b/test_project/test_project/settings.py @@ -252,7 +252,6 @@ REST_FRAMEWORK = { "DEFAULT_AUTHENTICATION_CLASSES": ( "oauth2_provider.contrib.rest_framework.OAuth2Authentication", "rest_framework.authentication.TokenAuthentication", - "rest_framework.authentication.SessionAuthentication", ), "DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema", "DEFAULT_VERSIONING_CLASS": "rest_framework.versioning.NamespaceVersioning",