[英]Nuxt-auth v5 module does not set loggedin user in store state
我目前正在 Nuxt Auth 模塊的幫助下研究身份驗證功能。 在前端我運行 Nuxt Js,在后端我運行 FastApi。
在 nuxt.config.js 中,我設置了身份驗證設置:
//Nuxt Auth module configuration https://auth.nuxtjs.org/schemes/local
auth: {
rewriteRedirects: false,
cookie: {
options: {
maxAge: 60 * 60 * 60 // 60 hours
}
},
localStorage: {
prefix: 'auth.'
},
strategies: {
local: {
token: {
prefix: 'access_token.',
property: 'access_token',
type: 'Bearer',
maxAge: 60 * 60 * 60
},
user: {
property: 'user',
autoFetch: true
},
endpoints: {
login: { url: '/api/v1/login/access-token', method: 'post' },
logout: false,
user: { url: '/api/v1/users/me', method: 'get' }
},
redirect: {
login: '/login',
logout: '/',
// callback: '/login',
home: '/dashboard'
}
}
}
}
在我的 Login.vue 中,我有一個帶有登錄方法的表單: import materialCard from '~/components/material/AppCard'
export default {
components: {
materialCard
},
middleware: 'authenticated',
auth: 'guest',
data () {
return {
username: 'admin',
password: 'admin'
}
},
methods: {
async authenticate () {
const form = new FormData()
form.append('username', this.username)
form.append('password', this.password)
await this.$auth.loginWith('local', { data: form })
.then((res) => {
console.log(res)
}).catch((err) => {
console.log(err.res)
})
}
}
}
當我嘗試登錄時,正在調用異步 function“登錄”。 返回與用戶名和密碼對應的用戶。 我遇到的唯一問題是,當我查看 vuex state 時,auth.loggedIn 保持 false 並且 auth.user 保持未定義。
我以為 Nuxt Auth 會自動更新 state,還是我遺漏了什么?
我在 oauth2 上遇到了同樣的問題,請確保您的 redirectUri 已啟用身份驗證。 當我設置redirectUri: '/'
時它不會調用 $auth.fetchUser() 因為'/'
是auth: false
。 我的更多發現: https://github.com/nuxt-community/auth-module/discussions/1592#discussioncomment-4250173
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.