【问题标题】:Devise - Merge OAuth with existing account设计 - 将 OAuth 与现有帐户合并
【发布时间】:2014-07-27 15:08:23
【问题描述】:

我使用他们网站https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview 上的说明为 Devise 设置了omniauth

我现在希望允许已经拥有帐户的用户透明地使用他们的 google 帐户登录:如果 google 帐户使用与之前注册的用户相同的电子邮件地址,则网站应使用该帐户登录用户。

我只是在链接处修改了建议的代码如下:

def self.from_omniauth(auth)
  user = User.find_by(email: auth.info.email)
  if user
    user.skip_confirmation! 
    user.provider = auth.provider
    user.uid = auth.uid
    user.name = auth.info.name
    return user
  end

  where(auth.slice(:provider, :uid)).first_or_create do |user|
    user.skip_confirmation! 
    user.provider = auth.provider
    user.uid = auth.uid
    user.email = auth.info.email
    user.password = Devise.friendly_token[0,20]
    user.name = auth.info.name
  end
end

它似乎工作正常,但我想知道这段代码是否会引入我不知道的任何风险。设计对我来说是一个黑盒子。

【问题讨论】:

    标签: ruby-on-rails authentication devise omniauth


    【解决方案1】:

    只有在两个帐户都得到确认后才能关联帐户。谷歌是一家电子邮件提供商,因此电子邮件已经在他们的末端得到确认。但是,您并没有检查您已经存在的用户帐户是否得到确认。

    因此,如果我知道您的电子邮件,我可以在您的网站上注册并等待您稍后登录 Google。然后我就可以访问您的帐户了。

    只有在 Devise's confirmed? method 返回 true 时才应该合并帐户。

    【讨论】:

      【解决方案2】:

      这是我现在使用的代码。快乐的复制粘贴。

      def self.from_omniauth(auth)
        user = User.find_by(email: auth.info.email)
        if user and user.confirmed?
          user.provider = auth.provider
          user.uid = auth.uid
          return user
        end
      
        where(auth.slice(:provider, :uid)).first_or_create do |user|
          user.skip_confirmation! 
          user.provider = auth.provider
          user.uid = auth.uid
          user.email = auth.info.email
          user.password = Devise.friendly_token[0,20]
          user.name = auth.info.name
        end
      end
      

      【讨论】:

        猜你喜欢
        • 2014-07-15
        • 1970-01-01
        • 2014-02-17
        • 1970-01-01
        • 1970-01-01
        • 2017-03-20
        • 1970-01-01
        • 2014-09-26
        • 1970-01-01
        相关资源
        最近更新 更多