【问题标题】:Create password and authentication for existing users为现有用户创建密码和身份验证
【发布时间】:2018-11-02 12:01:48
【问题描述】:

我有 Rails 应用程序: 带有 Devise 身份验证的管理表 带有电子邮件和名称的用户表,无需身份验证(但要记住它们的会话)

用户可以在任何地方浏览,但现在在某些页面上我想增强它并添加身份验证 - 允许用户创建密码并且只有使用密码才能访问它,但我很迷茫什么是最好的方法当前设置?

我允许用户添加他们的详细信息,例如姓名和电子邮件,我正在创建一个 cookie 来记住他们,而无需任何身份验证或密码:

  UsersController

  def create
    user = User.find_or_create_by(email: params[:user][:email])
    cookies.permanent.signed[:user_id] = user.id
    session[:user_id] = user.id # for users/edit temporary
    render json: user
  end

假设我在用户中有以下方法: before_filter :authenticate_user!, 仅: :your_order 定义你的订单 结束

如果用户将访问此页面并且之前没有设置密码,我该如何提示他创建一个,我如何要求他在使用 Devise 之后登录?我正在考虑更多的解决方案,但没有一个是完美的。

【问题讨论】:

  • 您可以为用户添加密码列。然后添加一个过滤器来检查current_user.password.empty? 之类的内容。如果是真的,您可以将它们发送到用户可以创建密码的页面。您还可以设置一个过滤器来检查用户是否已登录。由于您已经为此 User 模型使用了 cookie,也许您应该考虑创建另一个模型,例如 AuthenticatedUser,这就是用户保存密码、登录等的地方.

标签: ruby-on-rails devise


【解决方案1】:

根据给定的规格,下面提到的标准可能会对您有所帮助。

def your_order #before_filter
  if user.password.present?
   # authenticate using valid_password? method of devise
 else
   #redirect user to say set_password 
 end
end

def set_password
 #set the user password in this method and after successful completion redirect to login page where before filter your_order will be called
end

【讨论】:

    猜你喜欢
    • 2021-06-29
    • 2017-05-19
    • 2017-07-31
    • 2011-07-04
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 2018-07-13
    相关资源
    最近更新 更多