惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
阮一峰的网络日志
阮一峰的网络日志
雷峰网
雷峰网
D
Docker
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
MyScale Blog
MyScale Blog
A
About on SuperTechFans
博客园 - 【当耐特】
U
Unit 42
H
Help Net Security
博客园 - 三生石上(FineUI控件)
V2EX - 技术
V2EX - 技术
T
Tor Project blog
博客园 - 叶小钗
G
Google Developers Blog
S
Securelist
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threat Research - Cisco Blogs
aimingoo的专栏
aimingoo的专栏
C
Cybersecurity and Infrastructure Security Agency CISA
博客园_首页
V
Vulnerabilities – Threatpost
P
Palo Alto Networks Blog
T
The Exploit Database - CXSecurity.com
The Register - Security
The Register - Security
Recorded Future
Recorded Future
NISL@THU
NISL@THU
量子位
L
LangChain Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CERT Recently Published Vulnerability Notes
The Hacker News
The Hacker News
D
DataBreaches.Net
小众软件
小众软件
罗磊的独立博客
Forbes - Security
Forbes - Security
The Last Watchdog
The Last Watchdog
Jina AI
Jina AI
I
InfoQ
S
Schneier on Security
Recent Announcements
Recent Announcements
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
S
Secure Thoughts

任峻宏的小站

Debugging Memory Leaks in a Next.js Application How to Customize a Right-Click Menu in React Using ActionCable and React to Create a Simple Chat App 纪念左耳朵耗子 - 任峻宏的小站 Using SSE to Implement ChatGPT in Rails Refresh Myself - 任峻宏的小站 战痘 - 任峻宏的小站 逆向的能量——生活需要批评者 - 任峻宏的小站 人活着必须要有目标 - 任峻宏的小站 《第一人称单数》读书笔记 - 任峻宏的小站 卸载英雄联盟 - 任峻宏的小站 我的 2021 年总结 - 任峻宏的小站 记我的爷爷 - 任峻宏的小站 Migrate from Webpacker to Vite 从入门到放弃 《人生算法》读书笔记 - 任峻宏的小站 《不拘一格》读书笔记 - 任峻宏的小站 我为什么很少写博客 - 任峻宏的小站 我的2020年总结 - 任峻宏的小站 如何避免冲动消费? - 任峻宏的小站 《不能不去爱的两件事》读书笔记 - 任峻宏的小站 记一次搬家 - 任峻宏的小站 My blog V3.0 has been published! 解决服务器上运行 bundle install 时 killed 的问题 解决生产环境不能发送邮件的问题 - 任峻宏的小站 解决内存泄漏记录 - 任峻宏的小站 勿忘初心 - 任峻宏的小站 关于 RSpec 的一点方法总结 - 任峻宏的小站 使用 AJAX 重载部分页面 - 任峻宏的小站 Nginx 添加 SSL 支持 - 任峻宏的小站 Nginx 中进行重定向配置 - 任峻宏的小站 记我的第二次部署 - 任峻宏的小站 写在“毕业”之际 - 任峻宏的小站 JS 简单实现弹出层效果 - 任峻宏的小站 速递易工作总结及感想 - 任峻宏的小站 在 Aliyun ECS 上用 Nginx + Passenger 部署 Rails 应用时安装 Passenger 的问题 [译] Common Rails Idioms that Kill Database Performance Git 常用操作总结 - 任峻宏的小站 [译] Terminal 功夫——方便开发者的实用技巧 - 任峻宏的小站 实例方法与类方法 - 任峻宏的小站 各种报错解决集合 - 任峻宏的小站
Troubleshooting: Resolving Devise Issue in an API-only Application
任峻宏 · 2023-09-18 · via 任峻宏的小站

今天想给应用快速撸一个用户系统,注册登录,没想到搞了半天。上了 devise, 发现 current_user 在 sign_in 之后还是 nil. sign_in 本身是正常的,但是这个 current_user 只能 per request 有效。

原因:session-based authentication 在 API mode Rails application 中无法使用

解决思路:

config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore

方法1 需要重新写代码,成本相对高,放弃

方法3 尝试了引入之后还是不行,以为是方向错了,实际可用以下方法检查:

rake middleware

还可在浏览器查看 cookie, 正常 sign_in, sign_out 之后应该会有 cookie change, 如果没有,那说明 application cookie set 有问题

方法2 尝试了之后发现还是不行,才明白不是 API-only 的问题,看浏览器里面 cookie 根本没有正常 set

后来发现是只解决了一半,还有一个跨域的问题,因为这次我这个应用是彻底前后端分离,不在一个端口,所以需要使用 credentials (https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials) 在前端请求的时候 来处理这种 cross-origin requests. 不然 cookie 在发送请求的时候就不会发过去

当然了,rack-cors 也要配置一下,允许 credentials:

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins "127.0.0.1:3000"

    resource "*",
      headers: :any,
      methods: [:get, :post, :put, :patch, :delete, :options, :head],
      credentials: true  // Add this line
  end
end

另外今天还被自己坑了一把,之前没用 devise 的时候,自己为了测试方便写了如下代码:

def current_user
  @user = User.first
end

我说怎么 sign out 了,current_user 还一直在,登出就是登不出去,devise 请求 检查半天... 被自己坑惨

哦对了还有个问题,我项目里前端请求用的是 rails/request.js (https://github.com/rails/request.js) 这个库,还不支持请求的时候带 credentials option,我看已经有人 MR 了但是还没有 release, 先用着 axios 或者 fetch 都行。