# Vue3 - Pinia + Auth0 - isAuthenticated always false

**URL:** <https://community.auth0.com/t/vue3-pinia-auth0-isauthenticated-always-false/95030>\
**Category:** Get Help\
**Tags:** sdks-quickstarts, node-auth0\
**Created:** [November 23, 2022, 7:51pm UTC](https://community.auth0.com/t/vue3-pinia-auth0-isauthenticated-always-false/95030 "2022-11-23T19:51:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinicius.andrade](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vinicius.andrade/32/33201_2.png) [@vinicius.andrade](https://community.auth0.com/u/vinicius.andrade)\
**Post date:** [November 23, 2022, 7:51pm UTC](https://community.auth0.com/t/vue3-pinia-auth0-isauthenticated-always-false/95030/1 "2022-11-23T19:51:02Z")

</div>

I’m developing a vue3 app using pinia as state manager and auth0 as authprovider.

In my vue router, I’ve the following code to manage the authentication:

```typescript
router.beforeEach(async (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
    const authStore = useAuthStore();
    const isLogged = authStore.isLogged();
    if (!isLogged) await handleNotLogged(to, from, next);
    else await handleLogged(to, from, next);
});

async function handleNotLogged(to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) {
    const authStore = useAuthStore();
    if (to?.query?.code && to?.query?.state) {
        next({ name: '/logged/home' });
    } else {
        await authStore.login();
    }
}
async function handleLogged(to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) {next()}

```

here is my **authStore.ts**

```typescript
import { defineStore } from 'pinia';
import { User } from '@models/user';
import { useStorage } from '@vueuse/core';
import { RouteLocation } from 'vue-router';
import { createAuth0 } from '@auth0/auth0-vue';
const authService = createAuth0({
    domain: import.meta.env.VITE_APP_AUTH_URL,
    client_id: import.meta.env.VITE_APP_AUTH_CLIENT_ID,
    redirect_uri: `${window.location.origin}`,
});

const defaultUserData = {} as User;
const defaultLastRoute = { path: '/' } as RouteLocation;
export const useAuthStore = defineStore('AuthStore', {
    state: () => ({
        userData: useStorage('userData', defaultUserData, localStorage),
        lastRoute: useStorage('lastRoute', defaultLastRoute, localStorage),
        authService,
    }),
    actions: {
        isLogged(): boolean {
            try {
                return this.authService.isAuthenticated;
            } catch (error) {
                return false;
            }
        },
        async login(): Promise<boolean> {
            try {
                await this.authService.loginWithRedirect();
                return true;
            } catch (error) {
                console.error(error);
                return false;
            }
        },
        async logout(): Promise<boolean> {
            try {
                await this.authService.logout();
                return true;
            } catch (error) {
                console.error(error);
                return false;
            }
        },
    },
});

```

And also my **main.ts**

```typescript
import App from './App.vue';
import { createApp } from 'vue';
import { createPinia } from 'pinia';
import { registerPlugins } from '@plugins';
import { useAuthStore } from '@store/auth';
import router from '@router';
import vuetify from './plugins/vuetify';

async function main() {
    const app = createApp(App);
    registerPlugins();
    const pinia = createPinia();
    app.use(pinia);
    const authStore = useAuthStore();
    const { authService } = authStore;
    app.use(authService);
    app.use(router);
    app.use(vuetify).mount('#app');
}
main();

```

The problem is that everytime the beforeEach is triggered, the auth0 isAuthenticated returns false. Even when i’ve just succesfully logged.

I’ve searched for some answers, and some said that ewhen there is a code and state in query params we should call the auth0.handleRedirectCallback but there’s a note in the method saying

> Note: The Auth0-Vue SDK handles this for you, unless you set skipRedirectCallback to true. In that case, be sure to explicitly call handleRedirectCallback yourself.

PS: The application in auth0 is configured as Single Page Application

---

<div class="post-metadata">

**Author:** ![donniekerr01](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/donniekerr01/32/39308_2.png) [@donniekerr01](https://community.auth0.com/u/donniekerr01)\
**Post date:** [November 28, 2022, 7:30pm UTC](https://community.auth0.com/t/vue3-pinia-auth0-isauthenticated-always-false/95030/2 "2022-11-28T19:30:43Z")

</div>

Have you tried using beforeEnter: authGuard from import { authGuard } from “@auth0/auth0-vue”;  
That is how I protect routes in my Vue 3 app. I am using Universal Login.

---

<div class="post-metadata">

**Author:** ![vinicius.andrade](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vinicius.andrade/32/33201_2.png) [@vinicius.andrade](https://community.auth0.com/u/vinicius.andrade)\
**Post date:** [November 28, 2022, 7:54pm UTC](https://community.auth0.com/t/vue3-pinia-auth0-isauthenticated-always-false/95030/3 "2022-11-28T19:54:14Z")

</div>

No, because I want to guard it according some rules that were defined to me.

---

<div class="post-metadata">

**Author:** ![morphatic](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/morphatic/32/6997_2.png) [@morphatic](https://community.auth0.com/u/morphatic)\
**Post date:** [November 30, 2022, 2:34am UTC](https://community.auth0.com/t/vue3-pinia-auth0-isauthenticated-always-false/95030/4 "2022-11-30T02:34:54Z")

</div>

I don’t know if this is the issue in your case, but when using the built in `authGuard` the `@auth0/auth0-vue` library docs section on [protecting a route](https://www.npmjs.com/package/@auth0/auth0-vue#protect-a-route) indicate that you need to register the router BEFORE the Auth0 module in `main.ts`, i.e.

```plaintext
// what you have now
const app = createApp(App)
app.use(authService)
app.use(router)

// what you might want instead
const app = createApp(App)
app.use(router) // <== PUT THIS BEFORE AUTH
app.use(authService)

```

Hope this helps!

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [November 30, 2022, 11:09am UTC](https://community.auth0.com/t/vue3-pinia-auth0-isauthenticated-always-false/95030/5 "2022-11-30T11:09:11Z")

</div>

Thank you for sharing that @morphatic !

---

<div class="post-metadata">

**Author:** ![vinicius.andrade](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vinicius.andrade/32/33201_2.png) [@vinicius.andrade](https://community.auth0.com/u/vinicius.andrade)\
**Post date:** [December 1, 2022, 7:56pm UTC](https://community.auth0.com/t/vue3-pinia-auth0-isauthenticated-always-false/95030/6 "2022-12-01T19:56:42Z")

</div>

It still returning is authenticated as false. =/

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [December 15, 2022, 7:57pm UTC](https://community.auth0.com/t/vue3-pinia-auth0-isauthenticated-always-false/95030/7 "2022-12-15T19:57:32Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
