我曾尝试使用 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”?