I've found a wonderful ActionCable gem, which is a good solution for SPA.
I want to send only the html
, css
and js
assets, all other connections will be implemented through ActionCable
. It's not difficult to exchange strings or integers, but how can I login through ActionCable?
FYI, if you have
devise
already installed in your application, then you can use the environment variable set bywarden
to find theauthenticated user
. For every authenticated user warden stores the user object in environment var. Every request is authenticated bywarden
middleware.Note: this env is different from
ENV
.If you have not used
devise
then, here is another solution. Precondition is, you will have to set a signed cookie calleduser_id
in yoursessions_controller
or something like that. egand for connection:
The solution is to use HTTP authorization token. It's simple, widespread and obvious. This article helped me a lot
From the Readme
So it looks like you could insert your own
find_verified_user
logic here. Thereject_unauthorized_connection
method lives inlib/action_cable/connection/authorization.rb
for reference.From Heroku:
With this in mind it would likely be a real pain not to just use a normal web login flow to set your auth cookie, delivering your SPA after the authentication step, but hopefully this can give you some pointers.