简体   繁体   English

使用 omniauth 时设计跳过确认

[英]Devise skip confirmation when using omniauth

How can I change the following code so that a user that signs in with facebook gets to skip the confirmation with devise confirmable?如何更改以下代码,以便使用 facebook 登录的用户可以跳过与设计可确认的确认? I tried adding user.skip_confirmation!我尝试添加user.skip_confirmation! under the line user.email... but this did not work.user.email...user.email...但这不起作用。

user.rb:用户.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 omn​​iauth_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

Try this with first_or_initialize :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

If you're looking for the simplest way, just adding user.skip_confirmation!如果您正在寻找最简单的方法,只需添加user.skip_confirmation! in the method referred in the documentation does work now:文档中提到的方法现在确实有效:

 # 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

If you want to skip both confirmation and the email confirmation notification (which would make sense), you'll just need to add user.skip_confirmation!如果您想跳过确认和电子邮件确认通知(这很有意义),您只需要添加user.skip_confirmation! before saving the user, which you can do like this:在保存用户之前,您可以这样做:

 # 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未定义的方法skip_confirmation! - 设计,omniauth - undefined method skip_confirmation! - devise, omniauth 使用设计跳过omniauth用户的电子邮件确认 - skipping email confirmation for omniauth users using devise 在设计中跳过确认邮件 - Skip confirmation mail in devise 如何使用 devise_token_auth 跳过 email 确认? - how to skip email confirmation using devise_token_auth? 设计.skip_confirmation! 没有保存 - Devise .skip_confirmation! is not saving 使用 devise、omniauth 和 devise-token-auth 时,没有路由匹配“omniauth/:provider” - No route matches "omniauth/:provider" when using devise, omniauth and devise-token-auth Rails 3.2.2 Facebook 应用程序使用 Devise 和 Omniauth 在用户权限确认时重定向出 iframe - Rails 3.2.2 Facebook app using Devise and Omniauth redirecting out of iframe on user permissions confirmation 设计gem跳过确认和一次通过电子邮件跳过确认 - Devise gem skip confirmation and skip confirmation via email both at once 使用ajax,devise和omniauth时用户无法登录 - user fails to signin when using ajax, devise and omniauth 如何在使用devise + omniauth + rails 3时清除facebook会话和cookie? - How to clear facebook session and cookie when using devise + omniauth + rails 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM