【问题标题】:How can i allow user to authenticate using email and some other field in devise instead of email and password?如何允许用户使用电子邮件和设计中的其他字段而不是电子邮件和密码进行身份验证?
【发布时间】:2013-08-29 04:37:49
【问题描述】:

我正在使用 devise 进行身份验证,并且我使用 rails 作为 ios 应用程序的后端。我想保存用户属性,注册时没有密码字段,所以我重写了设计注册控制器并根据需要进行了设置。

现在我想允许用户在每个请求上都经过身份验证,所以通常我在其他控制器中使用"authenticate_user!" 方法,所以它会检查每个请求的用户,但它需要密码才能这样做,但在我的情况下,我现在没有密码,所以我如何在每个请求上验证用户。

我能否覆盖authenticate_user!方法并允许检查电子邮件和类型(例如)而不是电子邮件和密码,或者告诉我是否有其他方法。

另外请告诉我如何设计访问授权标头并在 authenticate_user 中使用它!方法。请帮我。我很长一段时间都沉迷于此。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 authentication devise ruby-on-rails-3.2


    【解决方案1】:

    正如我之前回答的那样,您可以在 User 模型中覆盖 find_first_by_auth_conditions 方法。请看我之前的回答。

    Is there a solution for Rails Gem Devise to allow a user to have multiple emails?

    【讨论】:

    • 我看了你的回答,我听不懂。你能再解释一下吗?在您的回答中,您使用了 find_first_by_auth_conditions 方法。我想知道的是如何像我们使用“before_filter :authenticate_user”方法一样对其他控制器中的用户进行身份验证?
    • 因为你所有的控制器都继承自ApplicationController,所以使用这个before_filter。方法find_first_by_auth_conditions 指定设计如何在数据库中查找记录,默认情况下它类似于User.where(email: email).first。如果您没有密码字段,我可以建议使用token_authenticatable 选项
    • 在 find_first_by_auth_conditions(warden_conditions) 中,warden_conditions 指的是什么?同样 conditions =warden_conditions.dup 和 super(warden_conditions) 指的是什么。抱歉打扰了,我是新手。
    • Warden_conditions - 是用作身份验证密钥的字段。前电子邮件、登录名和其他
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多