【发布时间】:2022-01-26 13:15:12
【问题描述】:
我现在正在尝试使用 nuxt-auth 编写登录功能。
我有一个 FastAPI 服务器,它设置为使用 HTTPOnly cookie,因此它需要一个 csrf 令牌来将用户扔给我的客户端。我无法处理令牌,因为它是 HTTPOnly 所以没有 LocalStorage
登录工作正常,但我无法获取存储的用户。我在向我的/login 端点提出请求后,Nuxt 还请求/me 端点上的用户。但我收到了 401 响应,并且
缺少 cookie access_token_cookie
/me 出错。不知道怎么处理。
我的登录请求方法
async userLogin() {
await this.$auth.loginWith('cookie', {
data: `grant_type=&username=${this.emailInput}&password=${this.passwordInput}&scope=&client_id=&client_secret=&`,
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
})
await this.$router.push('/account')
}
我读到 nuxt-auth 不擅长 cookie 模式,但该帖子是 2018 年发布的,我们现在有一个“cookie”策略。那么有没有更好的手动处理身份验证的解决方法?
我的auth 输入nuxt.config.js
auth: {
strategies: {
cookie: {
endpoints: {
login: {
url: "/api/v1/login/login",
method: "post",
withCredentials: true
},
logout: { url: "/api/v1/login/logout", method: "post" },
user: {
url: "/api/v1/users/me",
method: "get"
}
},
tokenType: "bearer"
}
}
}
【问题讨论】:
标签: vue.js nuxt.js fastapi nuxt-auth