现在,我正在尝试使用诺克斯奥斯编写登录功能。
我有一个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不擅长曲奇模式,但这篇文章是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"
}
}
}发布于 2021-12-28 08:53:53
我有一个基于Nuxt + Django的基于http的cookie安装程序。
我的Nuxt应用程序反向代理API请求到后端。因此,它可以在服务器端读取cookie。
因此,我创建了auth-ssr.ts中间件来检查用户loggedIn。
import { Context, Middleware } from '@nuxt/types'
import { parse as parseCookie } from 'cookie' // this is lib https://github.com/jshttp/cookie
/**
* This middleware is needed when running with SSR
* it checks if the token in cookie is set and injects it into the nuxtjs/auth module
* otherwise it will redirect to login
* @param context
*/
const authMiddleware: Middleware = async (context: Context) => {
if (process.server && context.req.headers.cookie != null) {
const cookies = parseCookie(context.req.headers.cookie)
const token = cookies['session'] || '' // here your cookie name
if (token) {
context.$auth.$state.loggedIn = true
}
}
}
export default authMiddleware在这里我的nuxt.config.js
auth: {
strategies: {
cookie: {
user: {
property: 'user',
},
endpoints: {
login: {
url: '/api/v2/auth/login/',
method: 'post',
},
user: {
url: '/api/v2/auth/user/',
method: 'get',
},
logout: {
url: '/api/v2/auth/logout/',
method: 'post',
},
},
},
},
redirect: {
login: '/login',
},
plugins: ['@plugins/axios.ts'],
},
router: {
middleware: ['auth-ssr', 'auth'],
},
// Axios module configuration: https://go.nuxtjs.dev/config-axios
axios: {
proxy: true,
},
proxy: {
'/api': {
target: 'https://backend.com/',
},
},
...https://stackoverflow.com/questions/70497746
复制相似问题