How to automatically keep user remembered in Devise

后端 未结 3 1380
误落风尘
误落风尘 2020-12-10 14:16

I am building an app in which I would like the users to automatically be remembered on their computers, without having a \"remember me\" check box.

I read that I may

相关标签:
3条回答
  • 2020-12-10 15:02

    I think customizing your devise controller is the way to go here.

    Goal: automatically set remember-me for everybody.

    First, create a devise sessions controller. Let's tell rails routes about it

    config/routes.rb

    devise_for :users, :controllers => {:sessions => 'sessions'}
    

    app/controllers/sessions_controller.rb

    class SessionsController < Devise::SessionsController
    
      def create
        params[:user].merge!(remember_me: 1)
        super
      end
    
    end
    

    This way, the user's remember me will always be set to true. yay!

    You'll then want to edit the login form to not display the remember_me checkbox.

    Also, change this in the initializer to something far off:

    #config.remember_for = 2.weeks
    config.remember_for = 1.year
    
    0 讨论(0)
  • 2020-12-10 15:10

    That should be a persistent option so you can do this on an after create hook for the user since you want it to be a permanent option.

    0 讨论(0)
  • 2020-12-10 15:16

    If you read this pull request in devise: https://github.com/plataformatec/devise/issues/1513, the sanctioned way to remember by default appears to simply define on your User class:

    class User
       def remember_me
         (super == nil) ? true : super
       end
    end
    
    0 讨论(0)
提交回复
热议问题