Remember Me in Devise fails to Login User

188 Views Asked by At

If I don't use remember_me option, it works perfectly fine. The app flows as expected.

However, if I check remember_me, it will login in as expected, redirect me to a protected page, which will then redirect me back to the sign_in page (Since I use before_action :authenticate_user!. One other thing that user_signed_in? will retrun false as well).


Here is the request cycle. enter image description here


Here is the logs dump

Started POST "/users/sign_in" for 127.0.0.1 at 2023-06-08 22:33:44 +0500
Processing by Users::SessionsController#create as TURBO_STREAM
  Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "remember_me"=>"1"}, "commit"=>"Log in"}
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."email" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["email", "[email protected]"], ["LIMIT", 1]]
Redirected to http://127.0.0.1:3000/projects
Completed 303 See Other in 253ms (ActiveRecord: 0.4ms | Allocations: 2776)


Started GET "/projects" for 127.0.0.1 at 2023-06-08 22:33:44 +0500
Processing by ProjectsController#index as TURBO_STREAM
Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms | Allocations: 356)


Started GET "/users/sign_in" for 127.0.0.1 at 2023-06-08 22:33:44 +0500
Processing by Users::SessionsController#new as TURBO_STREAM
  Rendering layout layouts/application.html.erb
  Rendering devise/sessions/new.html.erb within layouts/application
  Rendered devise/shared/_links.html.erb (Duration: 0.6ms | Allocations: 230)
  Rendered devise/sessions/new.html.erb within layouts/application (Duration: 2.5ms | Allocations: 1060)
  Rendered shared/_navbar.html.erb (Duration: 0.1ms | Allocations: 47)
  Rendered layouts/_flash.html.erb (Duration: 0.1ms | Allocations: 20)
  Rendered layout layouts/application.html.erb (Duration: 6.7ms | Allocations: 3860)
Completed 200 OK in 9ms (Views: 7.4ms | ActiveRecord: 0.0ms | Allocations: 4622)

As for my `Gemfile

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '3.2.1'

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem 'rails', github: 'rails/rails', branch: 'main'

# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem 'sprockets-rails'

# Use pg as the database for Active Record
gem 'pg', '~> 1.1'

# Use the Puma web server [https://github.com/puma/puma]
gem 'puma', '~> 5.0'

# Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]
gem 'importmap-rails'

# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
gem 'turbo-rails'

# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem 'stimulus-rails'

# Build JSON APIs with ease [https://github.com/rails/jbuilder]
gem 'jbuilder'

# Use Redis adapter to run Action Cable in production
gem 'redis', '~> 4.0'

# Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis]
# gem "kredis"

# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
# gem "bcrypt", "~> 3.1.7"

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: %i[ mingw mswin x64_mingw jruby ]

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', require: false

# Use Sass to process CSS
# gem "sassc-rails"

# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

group :development, :test do
  # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
  gem 'debug', platforms: %i[ mri mingw x64_mingw ]
  gem 'rubocop'
end

group :development do
  # Use console on exceptions pages [https://github.com/rails/web-console]
  gem 'web-console'

  # Annotate database models
  gem 'annotate'
  gem 'rails-erd'

  # Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler]
  # gem "rack-mini-profiler"

  # Speed up commands on slow machines / big apps [https://github.com/rails/spring]
  # gem "spring"
end

group :test do
  # Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
  gem 'capybara'
  gem 'selenium-webdriver'
  gem 'webdrivers'
end

# Custom Gems
gem 'acts_as_list', '~> 1.1'
gem 'acts_as_recursive_tree', '~> 3.3'
gem 'cssbundling-rails', '~> 1.1', '>= 1.1.2'
gem 'date_validator', '~> 0.12.0'
gem 'devise', '~> 4.9', '>= 4.9.2'
gem 'name_of_person', '~> 1.1'
gem 'pay', '~> 6.3'
gem 'requestjs-rails'
gem 'responders', '~> 3.1'
gem 'stripe', '~> 8.5'


I am just so confused, could be due to timezone issues? Or possibly some other misconfiguration?


UPDATE: Here is my devise config (removed all the comments)

Devise.setup do |config|
  config.mailer_sender = '[email protected]'

  require 'devise/orm/active_record'

  config.case_insensitive_keys = [:email]

  config.strip_whitespace_keys = [:email]

  config.skip_session_storage = [:http_auth]

  config.stretches = Rails.env.test? ? 1 : 12

  config.reconfirmable = true

  config.remember_for = 2.weeks

  config.expire_all_remember_me_on_sign_out = true

  config.password_length = 6..128

  config.email_regexp = /\A[^@\s]+@[^@\s]+\z/

  config.reset_password_within = 6.hours

  config.sign_out_via = :get

  config.navigational_formats = ['*/*', :html, :turbo_stream]

  config.responder.error_status = :unprocessable_entity
  config.responder.redirect_status = :see_other
end

The sessions_controller is just empty. I didn't override anything.

class Users::SessionsController < Devise::SessionsController
  # before_action :configure_sign_in_params, only: [:create]

  # GET /resource/sign_in
  # def new
  #   super
  # end

  # POST /resource/sign_in
  # def create
  #   super
  # end

  # DELETE /resource/sign_out
  # def destroy
  #   super
  # end

  # protected

  # If you have extra params to permit, append them to the sanitizer.
  # def configure_sign_in_params
  #   devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute])
  # end
end

The only changes I made were to registrations_controller.

# frozen_string_literal: true

class Users::RegistrationsController < Devise::RegistrationsController
  # before_action :configure_sign_up_params, only: [:create]
  before_action :configure_account_update_params, only: [:update]

  # GET /resource/sign_up
  # def new
  #   super
  # end

  # POST /resource
  # def create
  #   super
  # end

  # GET /resource/edit
  # def edit
  #   super
  # end

  # PUT /resource
  # def update
  #   super
  # end

  # DELETE /resource
  # def destroy
  #   super
  # end

  # GET /resource/cancel
  # Forces the session data which is usually expired after sign
  # in to be expired now. This is useful if the user wants to
  # cancel oauth signing in/up in the middle of the process,
  # removing all OAuth session data.
  # def cancel
  #   super
  # end

  protected

  # If you have extra params to permit, append them to the sanitizer.
  # def configure_sign_up_params
  #   devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute])
  # end

  # If you have extra params to permit, append them to the sanitizer.
  def configure_account_update_params
    devise_parameter_sanitizer.permit(:account_update, keys: [:name])
  end

  def after_update_path_for(resource)
    edit_user_registration_path(resource)
  end

  # The path used after sign up for inactive accounts.
  # def after_inactive_sign_up_path_for(resource)
  #   super(resource)
  # end
end

Here is my Gemfile.lock

GIT
  remote: https://github.com/rails/rails.git
  revision: 55c3066da325703ff7a9524dbdc479b860db3970
  branch: main
  specs:
    actioncable (7.1.0.alpha)
      actionpack (= 7.1.0.alpha)
      activesupport (= 7.1.0.alpha)
      nio4r (~> 2.0)
      websocket-driver (>= 0.6.1)
      zeitwerk (~> 2.6)
    actionmailbox (7.1.0.alpha)
      actionpack (= 7.1.0.alpha)
      activejob (= 7.1.0.alpha)
      activerecord (= 7.1.0.alpha)
      activestorage (= 7.1.0.alpha)
      activesupport (= 7.1.0.alpha)
      mail (>= 2.7.1)
      net-imap
      net-pop
      net-smtp
    actionmailer (7.1.0.alpha)
      actionpack (= 7.1.0.alpha)
      actionview (= 7.1.0.alpha)
      activejob (= 7.1.0.alpha)
      activesupport (= 7.1.0.alpha)
      mail (~> 2.5, >= 2.5.4)
      net-imap
      net-pop
      net-smtp
      rails-dom-testing (~> 2.0)
    actionpack (7.1.0.alpha)
      actionview (= 7.1.0.alpha)
      activesupport (= 7.1.0.alpha)
      nokogiri (>= 1.8.5)
      rack (>= 2.2.4)
      rack-session (>= 1.0.1)
      rack-test (>= 0.6.3)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.6)
    actiontext (7.1.0.alpha)
      actionpack (= 7.1.0.alpha)
      activerecord (= 7.1.0.alpha)
      activestorage (= 7.1.0.alpha)
      activesupport (= 7.1.0.alpha)
      globalid (>= 0.6.0)
      nokogiri (>= 1.8.5)
    actionview (7.1.0.alpha)
      activesupport (= 7.1.0.alpha)
      builder (~> 3.1)
      erubi (~> 1.11)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.6)
    activejob (7.1.0.alpha)
      activesupport (= 7.1.0.alpha)
      globalid (>= 0.3.6)
    activemodel (7.1.0.alpha)
      activesupport (= 7.1.0.alpha)
    activerecord (7.1.0.alpha)
      activemodel (= 7.1.0.alpha)
      activesupport (= 7.1.0.alpha)
    activestorage (7.1.0.alpha)
      actionpack (= 7.1.0.alpha)
      activejob (= 7.1.0.alpha)
      activerecord (= 7.1.0.alpha)
      activesupport (= 7.1.0.alpha)
      marcel (~> 1.0)
    activesupport (7.1.0.alpha)
      concurrent-ruby (~> 1.0, >= 1.0.2)
      connection_pool (>= 2.2.5)
      i18n (>= 1.6, < 2)
      minitest (>= 5.1)
      tzinfo (~> 2.0)
    rails (7.1.0.alpha)
      actioncable (= 7.1.0.alpha)
      actionmailbox (= 7.1.0.alpha)
      actionmailer (= 7.1.0.alpha)
      actionpack (= 7.1.0.alpha)
      actiontext (= 7.1.0.alpha)
      actionview (= 7.1.0.alpha)
      activejob (= 7.1.0.alpha)
      activemodel (= 7.1.0.alpha)
      activerecord (= 7.1.0.alpha)
      activestorage (= 7.1.0.alpha)
      activesupport (= 7.1.0.alpha)
      bundler (>= 1.15.0)
      railties (= 7.1.0.alpha)
    railties (7.1.0.alpha)
      actionpack (= 7.1.0.alpha)
      activesupport (= 7.1.0.alpha)
      irb
      rackup (>= 1.0.0)
      rake (>= 12.2)
      thor (~> 1.0, >= 1.2.2)
      zeitwerk (~> 2.6)

GEM
  remote: https://rubygems.org/
  specs:
    acts_as_list (1.1.0)
      activerecord (>= 4.2)
    acts_as_recursive_tree (3.4.0)
      activerecord (>= 5.2.0, < 8)
      activesupport (>= 5.2.0, < 8)
      zeitwerk (>= 2.4)
    addressable (2.8.4)
      public_suffix (>= 2.0.2, < 6.0)
    annotate (3.2.0)
      activerecord (>= 3.2, < 8.0)
      rake (>= 10.4, < 14.0)
    ast (2.4.2)
    bcrypt (3.1.18)
    bindex (0.8.1)
    bootsnap (1.16.0)
      msgpack (~> 1.2)
    builder (3.2.4)
    capybara (3.39.1)
      addressable
      matrix
      mini_mime (>= 0.1.3)
      nokogiri (~> 1.8)
      rack (>= 1.6.0)
      rack-test (>= 0.6.3)
      regexp_parser (>= 1.5, < 3.0)
      xpath (~> 3.2)
    choice (0.2.0)
    concurrent-ruby (1.2.2)
    connection_pool (2.4.1)
    crass (1.0.6)
    cssbundling-rails (1.1.2)
      railties (>= 6.0.0)
    date (3.3.3)
    date_validator (0.12.0)
      activemodel (>= 3)
      activesupport (>= 3)
    debug (1.8.0)
      irb (>= 1.5.0)
      reline (>= 0.3.1)
    devise (4.9.2)
      bcrypt (~> 3.0)
      orm_adapter (~> 0.1)
      railties (>= 4.1.0)
      responders
      warden (~> 1.2.3)
    erubi (1.12.0)
    globalid (1.1.0)
      activesupport (>= 5.0)
    i18n (1.14.1)
      concurrent-ruby (~> 1.0)
    importmap-rails (1.1.6)
      actionpack (>= 6.0.0)
      railties (>= 6.0.0)
    io-console (0.6.0)
    irb (1.7.0)
      reline (>= 0.3.0)
    jbuilder (2.11.5)
      actionview (>= 5.0.0)
      activesupport (>= 5.0.0)
    json (2.6.3)
    loofah (2.21.3)
      crass (~> 1.0.2)
      nokogiri (>= 1.12.0)
    mail (2.8.1)
      mini_mime (>= 0.1.1)
      net-imap
      net-pop
      net-smtp
    marcel (1.0.2)
    matrix (0.4.2)
    mini_mime (1.1.2)
    minitest (5.18.0)
    msgpack (1.7.1)
    name_of_person (1.1.1)
      activesupport (>= 5.2.0)
    net-imap (0.3.4)
      date
      net-protocol
    net-pop (0.1.2)
      net-protocol
    net-protocol (0.2.1)
      timeout
    net-smtp (0.3.3)
      net-protocol
    nio4r (2.5.9)
    nokogiri (1.15.2-aarch64-linux)
      racc (~> 1.4)
    nokogiri (1.15.2-x86_64-linux)
      racc (~> 1.4)
    orm_adapter (0.5.0)
    parallel (1.23.0)
    parser (3.2.2.1)
      ast (~> 2.4.1)
    pay (6.6.1)
      rails (>= 6.0.0)
    pg (1.5.3)
    public_suffix (5.0.1)
    puma (5.6.5)
      nio4r (~> 2.0)
    racc (1.6.2)
    rack (3.0.7)
    rack-session (2.0.0)
      rack (>= 3.0.0)
    rack-test (2.1.0)
      rack (>= 1.3)
    rackup (2.1.0)
      rack (>= 3)
      webrick (~> 1.8)
    rails-dom-testing (2.0.3)
      activesupport (>= 4.2.0)
      nokogiri (>= 1.6)
    rails-erd (1.7.2)
      activerecord (>= 4.2)
      activesupport (>= 4.2)
      choice (~> 0.2.0)
      ruby-graphviz (~> 1.2)
    rails-html-sanitizer (1.6.0)
      loofah (~> 2.21)
      nokogiri (~> 1.14)
    rainbow (3.1.1)
    rake (13.0.6)
    redis (4.8.1)
    regexp_parser (2.8.0)
    reline (0.3.5)
      io-console (~> 0.5)
    requestjs-rails (0.0.10)
      rails (>= 6.0.0)
    responders (3.1.0)
      actionpack (>= 5.2)
      railties (>= 5.2)
    rexml (3.2.5)
    rubocop (1.52.0)
      json (~> 2.3)
      parallel (~> 1.10)
      parser (>= 3.2.0.0)
      rainbow (>= 2.2.2, < 4.0)
      regexp_parser (>= 1.8, < 3.0)
      rexml (>= 3.2.5, < 4.0)
      rubocop-ast (>= 1.28.0, < 2.0)
      ruby-progressbar (~> 1.7)
      unicode-display_width (>= 2.4.0, < 3.0)
    rubocop-ast (1.29.0)
      parser (>= 3.2.1.0)
    ruby-graphviz (1.2.5)
      rexml
    ruby-progressbar (1.13.0)
    rubyzip (2.3.2)
    selenium-webdriver (4.10.0)
      rexml (~> 3.2, >= 3.2.5)
      rubyzip (>= 1.2.2, < 3.0)
      websocket (~> 1.0)
    sprockets (4.2.0)
      concurrent-ruby (~> 1.0)
      rack (>= 2.2.4, < 4)
    sprockets-rails (3.4.2)
      actionpack (>= 5.2)
      activesupport (>= 5.2)
      sprockets (>= 3.0.0)
    stimulus-rails (1.2.1)
      railties (>= 6.0.0)
    stripe (8.5.0)
    thor (1.2.2)
    timeout (0.3.2)
    turbo-rails (1.4.0)
      actionpack (>= 6.0.0)
      activejob (>= 6.0.0)
      railties (>= 6.0.0)
    tzinfo (2.0.6)
      concurrent-ruby (~> 1.0)
    unicode-display_width (2.4.2)
    warden (1.2.9)
      rack (>= 2.0.9)
    web-console (4.2.0)
      actionview (>= 6.0.0)
      activemodel (>= 6.0.0)
      bindex (>= 0.4.0)
      railties (>= 6.0.0)
    webdrivers (5.2.0)
      nokogiri (~> 1.6)
      rubyzip (>= 1.3.0)
      selenium-webdriver (~> 4.0)
    webrick (1.8.1)
    websocket (1.2.9)
    websocket-driver (0.7.5)
      websocket-extensions (>= 0.1.0)
    websocket-extensions (0.1.5)
    xpath (3.2.0)
      nokogiri (~> 1.8)
    zeitwerk (2.6.8)

PLATFORMS
  aarch64-linux
  x86_64-linux

DEPENDENCIES
  acts_as_list (~> 1.1)
  acts_as_recursive_tree (~> 3.3)
  annotate
  bootsnap
  capybara
  cssbundling-rails (~> 1.1, >= 1.1.2)
  date_validator (~> 0.12.0)
  debug
  devise (~> 4.9, >= 4.9.2)
  importmap-rails
  jbuilder
  name_of_person (~> 1.1)
  pay (~> 6.3)
  pg (~> 1.1)
  puma (~> 5.0)
  rails!
  rails-erd
  redis (~> 4.0)
  requestjs-rails
  responders (~> 3.1)
  rubocop
  selenium-webdriver
  sprockets-rails
  stimulus-rails
  stripe (~> 8.5)
  turbo-rails
  tzinfo-data
  web-console
  webdrivers

RUBY VERSION
   ruby 3.2.1p31

BUNDLED WITH
   2.4.12
0

There are 0 best solutions below