How to use Devise: rememberable?

自古美人都是妖i 提交于 2019-12-08 14:50:36

问题


I'm making a Rails App.
I'd like to implement a check box 'remember me' for users to skip enter password from next time with using Devise:rememberable.but I can't figure out how to implement.
if you have any idea with this , please show me some sample code for that.


回答1:


Add the :rememberable option in your User model

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable,
         :validatable, :token_authenticatable, :lockable, :omniauthable

  # ...

end

Create a migration to add the remember_created_at column in the table of users

class AddRememberCreatedAtToUsers < ActiveRecord::Migration
  def change
    add_column :users, :remember_created_at, :datetime
  end
end

If you're not using the Devise default views, add the checkbox to your view:

<%= f.check_box :remember_me %> <%= f.label :remember_me %>

I think that's all you need...




回答2:


You may encounter issues with rememberable if you write your own authentication strategies. The resource object (e.g. User) returned by your authenticate! method needs to be responsible for setting the resource.remember_me from the form data. This is normally handled by the parent Authenticable's validate method. If you don't use this method, you'll have to set it yourself.



来源:https://stackoverflow.com/questions/11810486/how-to-use-devise-rememberable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!