diff --git a/src/runtime/middlewares/auth.ts b/src/runtime/middlewares/auth.ts index fab1811..732348c 100644 --- a/src/runtime/middlewares/auth.ts +++ b/src/runtime/middlewares/auth.ts @@ -6,11 +6,16 @@ import { useSanctum } from "../composables/sanctum"; * * The auth middleware is used to protect routes that require authentication. */ -export const auth = defineNuxtRouteMiddleware(async (to, from) => { +export const auth = defineNuxtRouteMiddleware(async () => { + const { authenticated, check } = useSanctum(); const config = useRuntimeConfig().public.sanctum; - - // If the user is not authenticated, redirect to the unauthenticated page. - if (!useSanctum().authenticated.value) { - return config.middlewares.auth.redirectsTo; + + // Because we know the last authenticated state, we can use it to determine + // if we should make a request to the server to check if the user is still + // authenticated. + if (authenticated.value || (await check())) { + return; } + + return config.middlewares.auth.redirectsTo; }); diff --git a/src/runtime/middlewares/check.ts b/src/runtime/middlewares/check.ts deleted file mode 100644 index 74a8a17..0000000 --- a/src/runtime/middlewares/check.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { defineNuxtRouteMiddleware, useNuxtApp } from "#imports"; -import { useSanctum } from "../composables/sanctum"; - -/** - * Check Middleware - * - * The check middleware is used to check if the user is authenticated - * by performing a request to the user endpoint. - */ -export const check = defineNuxtRouteMiddleware(async (to, from) => { - const { authenticated, check: authCheck } = useSanctum(); - - // If the user is already considered authenticated, we don't need to - // run the check. If by chance the user is not actually authenticated, - // the next request to the server will return an error that we will - // catch and reset the authenticated state. - if (authenticated.value === true) { - return; - } - - // On initial load, there is not need to run the check middleware if - // the application is server rendered. This is because the server will - // already have performed the check and set the authenticated state. - const nuxtApp = useNuxtApp(); - if (process.client && nuxtApp.isHydrating && nuxtApp.payload.serverRendered) { - return; - } - - await authCheck(); -}); diff --git a/src/runtime/plugin.ts b/src/runtime/plugin.ts index 182e68f..6326b57 100644 --- a/src/runtime/plugin.ts +++ b/src/runtime/plugin.ts @@ -1,13 +1,11 @@ import { addRouteMiddleware, defineNuxtPlugin, useRuntimeConfig } from "#app"; import { auth } from "./middlewares/auth"; -import { check } from "./middlewares/check"; import { guest } from "./middlewares/guest"; export default defineNuxtPlugin(async () => { const config = useRuntimeConfig().public.sanctum; // Register middlewares - addRouteMiddleware(config.middlewares.check.name, check, { global: true }); addRouteMiddleware(config.middlewares.auth.name, auth); addRouteMiddleware(config.middlewares.guest.name, guest); });