【发布时间】:2015-02-03 20:35:43
【问题描述】:
我正在开发 Rails API 并使用 Devise 进行身份验证。
当我在控制器中使用current_user 时,它总是返回nil(好像没有登录用户一样),尽管用户已经登录。
为什么它在开发网站时有效而在开发 API 时无效?
我正在使用 curl 命令来测试应用程序:
登录:
curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST http://localhost:3000/api/v1/sessions -d "{\"user\":{\"email\":\"user1@example.com\",\"password\":\"secret123\"}}"
退出:
curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X DELETE http://localhost:3000/api/v1/sessions/\?auth_token\=JRYodzXgrLsk157ioYHf
【问题讨论】:
-
您是否将会话 cookie 与后续 api 请求一起发送回?
-
HTTPie 是一个很棒的 API 测试工具github.com/jakubroztocil/httpie
标签: ruby-on-rails api authentication devise