陈金洲 - Rails + Next.js:一种提升开发者和用户体验的开发架构范式 - RubyConf China 2024
本地环境:
rails 中的验证通过 authenticate 自动生成,其他都如上述视频中设置,但是 nextjs 请求登录,在浏览器中没有对应响应的 cookie,确认后端已经生成了对应的 session 记录; 不知道有没有其他人试过视频中的方案遇到过这个问题没有?求大佬解惑!!!
同问
假设 Rails 正常运行在 localhost:3000, Next.js 运行在 localhost:3001
localhost:3000
localhost:3001
http://localhost:3000
POST /session
next.config.mjs
看看 URL Rewrite 是否包含类似的配置:
const nextConfig = { async rewrites() { return [ { source: '/api/:path*', destination: `http://localhost:3000/:path*`, }, ]; }, }; export default nextConfig;
POST /api/session
注意看 Next.js console 和 Rails console
如果正确,那么 Postman 中应该会拿到返回的 session token
演讲中提到的参考代码:
@mechiland 使用 get 请求可以拿到想要的 cookie;但是 post 不行,后端用 redirect 和 render 都没有 cookie;
Rails.application.config.middleware.insert_before 0, Rack::Cors do allow do origins 'http://localhost:3000' # 前端地址 resource '*', headers: :any, methods: [:get, :post, :put, :patch, :delete, :options, :head], credentials: true # 允许携带凭据 end end