Trying to sign in a user with Devise, I get an invalid authenticity token error.
I have csrf_meta_tags
in my layout, and there is an authenticity_token
present in the request params. As is suggested in answers to other questions, protect_from_forgery with: :exception
is before before_action :authenticate_user!
.
If I comment out protect_from_forgery
, I see the following in my server log:
Started POST "/users/sign_in" for 127.0.0.1 at 2018-01-19 16:13:46 -0500
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"[TOKEN]", "user"=>{"badge_number"=>"0285", "password"=>"[FILTERED]"}, "commit"=>"Log in"}
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`badge_number` = '0285' ORDER BY `users`.`id` ASC LIMIT 1
(0.2ms) SELECT `divisions`.`id` FROM `divisions` INNER JOIN `divisions_users` ON `divisions`.`id` = `divisions_users`.`division_id` WHERE `divisions_users`.`user_id` = 2
CACHE (0.0ms) SELECT `divisions`.`id` FROM `divisions` INNER JOIN `divisions_users` ON `divisions`.`id` = `divisions_users`.`division_id` WHERE `divisions_users`.`user_id` = 2 [["user_id", 2]]
Redirected to http://localhost:3000/
Completed 302 Found in 141ms (ActiveRecord: 2.1ms)
Started GET "/" for 127.0.0.1 at 2018-01-19 16:13:46 -0500
Processing by IncidentsController#index as HTML
Completed 401 Unauthorized in 0ms (ActiveRecord: 0.0ms)
So it looks like the login is working, but then the redirect to the requested path (/
) results in a 401 and being redirected to the login page, as if the login isn't working.
However none of this really explains why I'm getting the authenticity token error in the first place.
Not sure what the next step to investigate the problem might be, given that as far as I can see all the required elements for CSRF token verification are in place.