How to get access_token from auth module (nuxt/auth) in nuxtjs typescript?

I have tried to use “this.$auth.$state.accessToken” and “this.$route.query.token” to get token, but they don’t include “access_token”. Then I tried to get token with “this.$auth.strategy.token.get()” method. It shows the following error:

Property 'strategy' does not exist on type 'Auth<any>'.
           })
               .then(() => {
               console.log({ token: this.$auth.strategy.token.get() })
                                               ^^^^^^^^   
               this.$router.push('/')
           })

The code is follows.

nuxt.config.ts:

I have tried to use “this.$auth.$state.accessToken” and “this.$route.query.token” to get token, but they don’t include “access_token”. Then I tried to get token with “this.$auth.strategy.token.get()” method. It shows the following error:

 Property 'strategy' does not exist on type 'Auth<any>'.
           })
               .then(() => {
               console.log({ token: this.$auth.strategy.token.get() })
                                               ^^^^^^^^   
               this.$router.push('/')
           })

The code is follows.

nuxt.config.ts:

auth: {
redirect: {
  home: '/',
},
strategies: {
  laravelPassportPasswordGrant: {
    name: 'laravelPassportPassword',
    provider: 'laravel/passport',
    url: 'http://localhost:8000',
    endpoints: {
      logout: '/api/v1/logout',
      user: {
        url: '/api/v1/user',
      },
    },
    clientId: process.env.PASSPORT_CLIENT_ID,
    clientSecret: process.env.PASSPORT_CLIENT_SECRET,
    grantType: 'password',
  },
},

login.vue:

await this.$auth.loginWith('laravelPassportPassword', {
                data: {
                    username: this.username,
                    password: this.password,
                    captchaToken: recaptchaToken
                }
            })
                .then(() => {
                    console.log({ token: this.$auth.strategy.token.get() })

                    this.$router.push('/')
                })

How can I get “access_token”?

Hey,

I didn’t fully get it.

Are you having TypeScript issues only or it doesn’t work in general?

The way to access the tokens are right anyway: https://auth.nuxtjs.org/api/tokens

Dear viniciuskneves,
Thank you for your reply. It’s only typescript issue. Auth module return token correctly, but I can’t get access_token using auth token methods.

Ah, now I got it.

Did you add this to tsconfig? https://auth.nuxtjs.org/guide/setup#using-with-typescript

Yeah. I had added it up before, but I still receive the error.

Finally, I got access_token using universal storage method:

 this.$auth.$storage.getUniversal('_token.laravelPassportPassword')
1 Like

As per the docs:
viniciuskneves

this.$auth.strategy.token.get()