【问题标题】:Devise skip confirmation when using omniauth使用omniauth时设计跳过确认
【发布时间】:2015-09-29 01:03:39
【问题描述】:

如何更改以下代码,以便使用 facebook 登录的用户可以通过可确认的设计跳过确认?我尝试在user.email... 行下添加user.skip_confirmation!,但这不起作用。

user.rb:

# Finds or creates user based on omniauth hash from given provider
  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.first_name = auth.info.first_name
      user.last_name = auth.info.last_name
      user.email = auth.info.email
    end
  end

  # Overrides class method 'new_with_session' to persist and validate attributes
  def self.new_with_session(params, session)
    if session["devise.user_attributes"]
      new(session["devise.user_attributes"], without_protection: true) do |user|
        user.attributes = params
        user.valid?
      end
    else
      super
    end
  end

omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

  def all
    user = User.from_omniauth(request.env["omniauth.auth"])
    if user.persisted?
      flash.notice = "Signed in!"
      sign_in_and_redirect user
    else  
      session["devise.user_attributes"] = user.attributes
      redirect_to new_user_registration_url
    end
  end
  alias_method :facebook, :all
end

【问题讨论】:

    标签: ruby-on-rails devise omniauth


    【解决方案1】:

    first_or_initialize试试这个:

    def self.from_omniauth(auth)
        where(provider: auth.provider, uid: auth.uid). first_or_initialize do |user|
          user.provider = auth.provider
          user.uid = auth.uid
          user.first_name = auth.info.first_name
          user.last_name = auth.info.last_name
          user.email = auth.info.email
          user.skip_confirmation!
          user.save!
        end
      end
    

    【讨论】:

    • 我实际上添加了user.confirmed_at = Time.zone.now 行,它似乎正在工作。你能看到这样做与你的建议相比有什么问题吗?谢谢
    • 你可以使用user.confirm @user4584963 rubydoc.info/github/plataformatec/devise/Devise/Models/…
    • @user4584963 实际上是一样的,但skip_confirmation! 是有原因的。据我所知,更好的编码实践。见docs for it here
    【解决方案2】:

    如果您正在寻找最简单的方法,只需在documentation 中提到的方法中添加user.skip_confirmation! 现在就可以了:

     # app/model/user.rb
    
      def self.from_omniauth(access_token)
        data = access_token.info
        user = User.where(email: data['email']).first
    
        # Users are created if they don't exist
        unless user
          user = User.create(username: data['name'],
                             email: data['email'],
                             password: Devise.friendly_token[0, 20])
          user.skip_confirmation! # Add this line to skip confirmation
        end
        user
      end
    

    如果你想跳过确认和电子邮件确认通知(这很有意义),你只需要在保存用户之前添加user.skip_confirmation!,你可以这样做:

     # app/model/user.rb
    
      def self.from_omniauth(access_token)
        data = access_token.info
        user = User.where(email: data['email']).first
    
        # Users are created if they don't exist
        unless user
          user = User.new(username: data['name'],
                          email: data['email'],
                          password: Devise.friendly_token[0, 20])
          user.skip_confirmation! # Add this line to skip confirmation
          user.save
        end
        user
      end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      • 1970-01-01
      • 2015-08-09
      • 1970-01-01
      • 1970-01-01
      • 2011-11-20
      相关资源
      最近更新 更多