微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从nuxtjs打字稿中的auth模块nuxtjs/auth-next获取access_token?

如何解决如何从nuxtjs打字稿中的auth模块nuxtjs/auth-next获取access_token?

我尝试使用 this.$auth.$state.accesstoken 和 this.$route.query.token 来获取令牌,但它们不包含任何 access_token。 用户也已成功登录 throw this.$auth.loginWith('laravelPassportPassword') Auth.

然后我尝试使用 this.$auth.strategy.token.get() 方法获取令牌。它显示以下错误

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

代码如下。

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',

},

登录.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)

    }
}

}

我还看到下面的链接

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

如何获得“access_token”?

解决方法

最后,我使用通用存储方法获得了 access_token:

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

我想使用一些在 nuxt-auth 文档中看到的其他令牌方法,但似乎令牌对象不存在。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。