Faster frontend authentication.

This commit is contained in:
Antoine Nguyen
2025-09-19 18:15:56 +02:00
parent ac6182f534
commit 1e64d43911
5 changed files with 5 additions and 23 deletions
-3
View File
@@ -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) {
+4 -13
View File
@@ -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,
@@ -1,5 +1,4 @@
<script setup>
import { onMounted } from 'vue'
import { useAuthStore } from '@/stores'
import { useModoboaTheme } from '@/composables/theme'
@@ -10,10 +9,7 @@ const props = defineProps({
const authStore = useAuthStore()
const { enableTheme } = useModoboaTheme()
onMounted(() => {
authStore.completeLogin(props.redirectUrl)
})
await authStore.completeLogin(props.redirectUrl)
await enableTheme()
</script>
@@ -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',
-1
View File
@@ -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",