atinux / nuxt-auth-utils

Add Authentication to Nuxt applications with secured & sealed cookies sessions.
MIT License
976 stars 91 forks source link

Session not set on index.vue / middleware #111

Closed luminous8 closed 3 months ago

luminous8 commented 4 months ago

Hi,

I have a client middleware auth.global.js that looks like this

export default defineNuxtRouteMiddleware((to, from) => {
    try {
        const { loggedIn, user, session } = useUserSession();
        const {
            meta: { redirectIfLoggedIn, requiresAuth, requiresPremium },
        } = to || {};

        if (loggedIn.value) {
             if (to.fullPath === '/') {
                return navigateTo({ name: 'anotherPage' });
            } 
        }
        ...
    } catch (error) {
        console.log('auth.global error: ', { error });
    }
});

that works well but when landing "/", loggedIn is false, user & session are not defined

I've tried to this in index.page without any luck

<script setup>
const { loggedIn } = useUserSession();
onMounted(() => {
    console.log('on mounted', loggedIn.value);
    if (loggedIn.value) {
        return navigateTo({ name: 'apps' });
    }
});
</script>

but when using loggedIn or user inside