# \[Vue 3 & Vite 2\] Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'getTokenSilently')

**URL:** <https://community.auth0.com/t/vue-3-vite-2-uncaught-in-promise-typeerror-cannot-read-properties-of-undefined-reading-gettokensilently/81137>\
**Category:** Get Help\
**Tags:** auth0-spa-js\
**Created:** [April 5, 2022, 9:31am UTC](https://community.auth0.com/t/vue-3-vite-2-uncaught-in-promise-typeerror-cannot-read-properties-of-undefined-reading-gettokensilently/81137 "2022-04-05T09:31:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dur2dakka](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dur2dakka/32/36390_2.png) [@dur2dakka](https://community.auth0.com/u/dur2dakka)\
**Post date:** [April 5, 2022, 9:31am UTC](https://community.auth0.com/t/vue-3-vite-2-uncaught-in-promise-typeerror-cannot-read-properties-of-undefined-reading-gettokensilently/81137/1 "2022-04-05T09:31:47Z")

</div>

Hi!

@auth0/auth0-spa-js: v1.21.0  
node: v16.13.0  
vue-router: v4.0.14  
vue: v3.2.31  
vite: v2.9.1

Using getTokenSilently gives error in the title.

**Use** :

```auto
import { useAuth } from "@/backend/auth/useAuthService";
const { getTokenSilently } = useAuth();

async function getToken() {

  const accessTokenData = await getTokenSilently();

  return {

    access_token: accessTokenData,

  };

}

```

After a day of searching, I came close to understand that while ‘loading’ is true, the function won’t return anything so it is undefined.  
Writing something like `if (!loading) { const accessTokenData = await getTokenSilently(); ` would solve this but it doesn’t.

**Also refreshing the page will give the error but if I don’t refresh but change a code somewhere so it will compile without reloading, function works perfectly.**

I am not using [GitHub - auth0/auth0-vue: Auth0 authentication SDK for Vue.js apps](https://github.com/auth0/auth0-vue) because I had some problems with it.

These didn’t help:

- [Cannot read properties of undefined (reading 'isAuthenticated')](https://community.auth0.com/t/cannot-read-properties-of-undefined-reading-isauthenticated/74067)

- [Azure devops pipeline Auth0 Deployment error "Cannot read properties of undefined (reading 'getAll')"](https://community.auth0.com/t/azure-devops-pipeline-auth0-deployment-error-cannot-read-properties-of-undefined-reading-getall/78285)

- [Can't get Login page and prompt displayed, `Uncaught TypeError: Cannot read property 'login' of undefined`](https://community.auth0.com/t/cant-get-login-page-and-prompt-displayed-uncaught-typeerror-cannot-read-property-login-of-undefined/6271)

- [Running into crash in auth-spa library: TypeError: Cannot read properties of null (reading 'location')](https://community.auth0.com/t/running-into-crash-in-auth-spa-library-typeerror-cannot-read-properties-of-null-reading-location/71679)

- [javascript - Uncaught TypeError: Cannot destructure property `name` of 'undefined' or 'null' - Stack Overflow](https://stackoverflow.com/questions/54744949/uncaught-typeerror-cannot-destructure-property-name-of-undefined-or-null)

[Reproduction](https://stackblitz.com/edit/vitejs-vite-re5rbi?file=src/App.vue): (I made this but i can’t reproduce correctly, also it doesn’t give any errors. Just sharing)

[Github Issue link](https://github.com/auth0/auth0-spa-js/issues/894)

This may not be related to Auth0 but other libraries etc won’t give me this error.  
What am I doing wrong?

Thanks!

---

<div class="post-metadata">

**Author:** ![jaco](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jaco/32/36809_2.png) [@jaco](https://community.auth0.com/u/jaco)\
**Post date:** [April 22, 2022, 9:40am UTC](https://community.auth0.com/t/vue-3-vite-2-uncaught-in-promise-typeerror-cannot-read-properties-of-undefined-reading-gettokensilently/81137/2 "2022-04-22T09:40:56Z")

</div>

I believe I have a similar issue when using the new Vue3 SDK using authGuard to protect a Vue router route. I get the following error:  
`[Vue warn]: Error in mounted hook: "TypeError: Cannot read properties of undefined (reading 'getAccessTokenSilently')"`  
I am following the [instructions in the README](https://github.com/auth0/auth0-vue#protect-a-route), except I have the Home page protected by the guard, so I assume it is giving issues because of timing.

---

<div class="post-metadata">

**Author:** ![lizhong.gao](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lizhong.gao/32/25225_2.png) [@lizhong.gao](https://community.auth0.com/u/lizhong.gao)\
**Post date:** [May 18, 2022, 3:33pm UTC](https://community.auth0.com/t/vue-3-vite-2-uncaught-in-promise-typeerror-cannot-read-properties-of-undefined-reading-gettokensilently/81137/3 "2022-05-18T15:33:13Z")

</div>

I got same issue in vue 3 using auth0-vue sdk, I call getAccessTokenSilently in a Typescipt file, seems that getAccessTokenSilently only works in vue file

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘getAccessTokenSilently’)

any help on it?

---

<div class="post-metadata">

**Author:** ![jaco](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jaco/32/36809_2.png) [@jaco](https://community.auth0.com/u/jaco)\
**Post date:** [May 19, 2022, 8:11am UTC](https://community.auth0.com/t/vue-3-vite-2-uncaught-in-promise-typeerror-cannot-read-properties-of-undefined-reading-gettokensilently/81137/4 "2022-05-19T08:11:43Z")

</div>

I went back to using `@auth0/auth0-spa-js` and just using the vanilla implementation

---

<div class="post-metadata">

**Author:** ![dur2dakka](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dur2dakka/32/36390_2.png) [@dur2dakka](https://community.auth0.com/u/dur2dakka)\
**Post date:** [May 20, 2022, 1:24am UTC](https://community.auth0.com/t/vue-3-vite-2-uncaught-in-promise-typeerror-cannot-read-properties-of-undefined-reading-gettokensilently/81137/5 "2022-05-20T01:24:46Z")

</div>

We talked about this here for a long time and there are some different solutions:

> <https://github.com/auth0/auth0-vue/issues/99>
>
> \### Describe the problem you'd like to have solved
> 
> This plugin makes life a l…ot easier in Vue3 so thank you for that but there's one thing I can't seem to figure out: how to access the Auth0Client outside a component.
> 
> I'd like to use the Auth0 client to add a \`Authorziation: Bearer\` header to my Apollo requests with the help of \[\`setContext\`\](https://www.apollographql.com/docs/react/api/link/apollo-link-context/#overview). I tried using \`useAuth0()\` but this doesn't work because Vue cannot use \`inject\` outside the scope of a component.
> 
> \### Describe the ideal solution
> 
> Be able to access the Auth0Client instance in \_and\_ outside components.
> 
> \### Alternatives and current work-arounds
> 
> The \[given example\](https://github.com/auth0/auth0-vue#calling-an-api) requires me to provide the authorization header every time I make a request; that's quite a bit of duplication.

This worked for me: [Be able to access Auth0Client outside a component · Issue #99 · auth0/auth0-vue · GitHub](https://github.com/auth0/auth0-vue/issues/99#issuecomment-1104638005)

---

<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:** [May 20, 2022, 6:59am UTC](https://community.auth0.com/t/vue-3-vite-2-uncaught-in-promise-typeerror-cannot-read-properties-of-undefined-reading-gettokensilently/81137/6 "2022-05-20T06:59:39Z")

</div>

Thanks for sharing that with the rest of community!

---

<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:** [July 28, 2022, 12:54pm UTC](https://community.auth0.com/t/vue-3-vite-2-uncaught-in-promise-typeerror-cannot-read-properties-of-undefined-reading-gettokensilently/81137/7 "2022-07-28T12:54:32Z")

</div>


