Anybody have good resources to check out for how to implement devise authentication with react_on_rails? New to react_on_rails (and pretty new to react). Trying to keep what I like about rails (ruby, devise authentication) and implement the stuff I like about react (redux, JSX, etc)
Well device integration is quite easy Use the normal login box via layout and then start your websocket with a active session. Then you just need to use this code in your connection.rb
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
logger.add_tags 'ActionCable', current_user.email
end
def find_verified_user
## this is where devise stores the current user
env['warden'].user || reject_unauthorized_connection
end
But when I attempt to login on this page: http://localhost:3000/users/sign_in, I can sign in successfully. It’s the standard devise form that works from /users/sign_in.
After logging in, it takes me to my homepage and shows that i’ve logged in (because I check if current_user exists). I’ll then be able to sign out with a button on my homepage. However when I use my dropdown login form, I get the error from the beginning.
A lot of answers I’ve found on google involved adding protect_from_forgery with: :null_session to application_controller.rb. My guess as to what is happening is that the Rails app thinks the post request is coming from another app (which it is, right?) because the ‘React’ part of the app is it’s own client-side app. Does that make sense? If anyone can help bring light to the situation, I’d really appreciate it! Thanks Shakacoders! =D
P.S. Is devise the best way to handle user auth with React on Rails?
This worked, thanks a lot! I used ruby to get the token and I ended up passing the token as a prop to the react_component helper method in my index.html.erb page. I made a helper to get the token, and put it in helpers/application_helper.rb. Here’s the helper I made (for others’ future reference):