[英]How to access current_user with devise-token_authenticatable gem?
I'm working on a small project that is using devise
and devise-token_authenticatable
in a Rails/GraphQL/Apollo/React
setup.我的工作是使用一个小的项目devise
和devise-token_authenticatable
在Rails/GraphQL/Apollo/React
设置。 Project is setup using webpacker
, so the front and back ends are both served from the same domain.项目是使用webpacker
设置的,因此前端和后端都来自同一个域。 Sign-in, register, log-out are all handle by the front end (React).登录、注册、注销都由前端(React)处理。 I also need to point out this is not an API only app.我还需要指出这不仅仅是一个 API 应用程序。
It seems, server is not setting the current_user
or the proper cookie and I'm getting a network error.看来,服务器没有设置current_user
或正确的 cookie,我收到了网络错误。
Heroku logs shows the following error: Heroku 日志显示以下错误:
NoMethodError (undefined method `authentication_token' for #<User:0x000055ca6c0e4c50>)
Is that mean it's trying 'authentication_token' method on a nil class?这是否意味着它正在 nil 类上尝试“authentication_token”方法?
At the same time it's showing this error in the console:同时它在控制台中显示此错误:
Network error: Unexpected token < in JSON at position 0 (I have no idea what this is)
In my login component's sign-in mutation, I'm storing the authentication_token of the user in local storage after a successful login and in my Apollo setup I'm retrieving it from storage and attaching it to each request.在我的登录组件的登录突变中,我在成功登录后将用户的 authentication_token 存储在本地存储中,在我的 Apollo 设置中,我从存储中检索它并将其附加到每个请求。 But Chrome dev tool's network tab shows I'm not getting the cookie set by devise after a signin in the response header.但是 Chrome 开发工具的网络选项卡显示在响应标头中登录后,我没有获得设计设置的 cookie。 I'm pretty new to all of this so not sure this is the problem or something else.我对这一切都很陌生,所以不确定这是问题还是其他问题。 PS: I'm not using Devise sessions since sign-in is handle in the from end. PS:我没有使用设计会话,因为登录是在最后处理的。 I have devise_for: users, skip: :sessions
in my routes.我有devise_for: users, skip: :sessions
在我的路线中。
Code snippets:代码片段:
Any assistance is greatly appreciated.非常感谢任何帮助。 Thank you in advance for your help.预先感谢您的帮助。
UPDATE: issue was fixed: (also see my answer below for additional info)更新:问题已修复:(另请参阅下面的答案以获取更多信息)
Issue was fixed by adding current_user
private method in application_controller.rb
.通过在application_controller.rb
添加current_user
私有方法修复了问题。
Here's the code...这是代码...
def current_user
token = request.headers["Authorization"].to_s
User.find_for_database_authentication(authentication_token: token)
end
Marko Kacanski was right I should have elaborate on my findings & how I fixed it. Marko Kacanski 是对的,我应该详细说明我的发现以及我如何修复它。 :-( :-(
I was wrong to assume that the current_user
is automatically sent by the gem devise-token_authenticatable
when it detects a authentication_token
in the header.我错误地假设当 gem devise-token_authenticatable
在标头中检测到authentication_token
时,它会自动发送current_user
是错误的。 At least the way I have setup my project, I wasn't getting the current_user
as expected, instead I get a nil object.至少在我设置项目的方式中,我没有按预期获得current_user
,而是获得了一个 nil 对象。
So... I created a current_user
private method in application_controller.rb
and, it fixed the issue.所以......我在application_controller.rb
创建了一个current_user
私有方法,它解决了这个问题。
Here's the code...这是代码...
def current_user
token = request.headers["Authorization"].to_s
User.find_for_database_authentication(authentication_token: token)
end
Fixed the issue.修复了问题。 It had nothing to do with GraphQl/Apollo code.它与 GraphQl/Apollo 代码无关。 Thanks谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.