I see you're having trouble with the nuxt-auth-utils module and the useUserSession composable in your middleware. Let me help you fix your code.
There are a few issues with your middleware implementation:
- The import statement for
defineNuxtRouteMiddleware and navigateTo is incorrect
- The way you're importing
useUserSession might be causing problems
Here's the corrected version of your middleware:
export default defineNuxtRouteMiddleware(() => {
const { loggedIn } = useUserSession()
if (!loggedIn.value) {
return navigateTo('/login')
}
})
You don't need to explicitly import defineNuxtRouteMiddleware and navigateTo as they are auto-imported in Nuxt 3. Also, you don't need to explicitly import useUserSession as it's made available by the nuxt-auth-utils module once it's properly installed.
Make sure you've properly installed the nuxt-auth-utils module:
- Install it using:
npx nuxi@latest module add auth-utils
- Ensure it's in your
nuxt.config.ts modules array
- Set up the required environment variable
NUXT_SESSION_PASSWORD in your .env file as mentioned in the documentation
If you're still having issues, make sure your middleware is placed in the correct location (in the middleware directory at the root of your project) and that you're applying it correctly to your routes using definePageMeta:
definePageMeta({
middleware: ['authenticated'],
})