Vajiheh mrp
Vajiheh mrp

Reputation: 11

How to get access_token from auth module (nuxtjs/auth-next) 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 any access_token. Also user have logged in successfully throw this.$auth.loginWith('laravelPassportPassword') Auth.

Then I tried to get token with this.$auth.strategy.token.get() method. It shows the following error:

TS2339: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:

import { Vue, Component } from 'vue-property-decorator'
import PageHeader from '~/components/shared/page-header.vue'


export default class Page extends Vue {
username: string = ''
email: string = ''
password: string = ''
confirmPassword: string = ''

private async login (): Promise<void> {
    try {
        const recaptchaToken = await this.$recaptcha.getResponse()
        
        if (recaptchaToken) {
            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('/')
                })

        }
        await this.$recaptcha.reset()

    } catch (e) {
        console.log(e)

    }
}

}

Also I see the link bellow:

https://auth.nuxtjs.org/api/tokens

How can I get "access_token"?

Upvotes: 0

Views: 1387

Answers (1)

Vajiheh mrp
Vajiheh mrp

Reputation: 11

Finally, I got access_token using universal storage method:

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

I would like to use some other token methods that saw in nuxt-auth docs, but it seems the token object dosn't exist.

Upvotes: 1

Related Questions