使用 devise 跳过omniauth 用户的电子邮件确认

发布于 2024-12-05 14:05:05 字数 3781 浏览 0 评论 0原文

我这里有一些代码,用户可以通过他们的 Twitter 帐户登录。这里的问题是,我如何跳过用户从 Twitter 等外部服务注册的电子邮件确认。我正在使用 devise,但我不知道如何跳过此类用户的电子邮件确认。我的代码示例如下

class AuthenticationsController < ApplicationController
  # GET /authentications
  # GET /authentications.json
  def index
    @authentications = current_user.authentications if current_user
  end

  # POST /authentications
  # POST /authentications.json
  def create
    omniauth = request.env["omniauth.auth"]
    authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
    if authentication
      flash[:notice] = "Signed in successfully"
     sign_in_and_redirect(:user, authentication.user)
    elsif current_user
    current_user.authentications.create!(:provider => omniauth['provider'], :uid => ['uid'])
    flash[:notice] = "Authentication successful"
    redirect_to authentication_url
    else
      user = User.new
      user.apply_omniauth(omniauth)
     if user.save
      flash[:notice] = "Signed in successfully"
      sign_in_and_redirect(:user, user)
     else
      session[:omniauth] = omniauth.except('extra')
      redirect_to new_user_registration_url
     end
    end
  rescue Exception => e
    # Just spit out the error message and a backtrace.
    render :text => "<html><body><pre>" + e.to_s + "</pre><hr /><pre>" + e.backtrace.join("\n") + "</pre></body></html>"

  end


  # DELETE /authentications/1
  # DELETE /authentications/1.json
  def destroy
    @authentication = current_user.authentications.find(params[:id])
    @authentication.destroy

    respond_to do |format|
      format.html { redirect_to authentications_url }
      format.json { head :ok }
    end
  end
end

我的注册控制器如下

class RegistrationsController < Devise::RegistrationsController

  def create
    super
    session[:omniauth] = nil unless @user.new_record?
  end

  private
  def build_resource(*args)
    super
    if session[:omniauth]
      @user.apply_omniauth(session[:omniauth])
      @user.valid?
    end
  end
end

,我的用户模型低于

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable,  :lockable, :timeoutable and
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable


  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :about, :facebook_username, :twitter_username, :icon, :admin

  validates_uniqueness_of :username

  has_attached_file :icon, :styles => {:thumb => "64x64#"}, :default_url => 'icon_:style.png'
  validates_attachment_content_type :icon, :content_type => ['image/jpeg', 'image/png', 'image/gif']
  validates_attachment_size :icon, :less_than => 1.megabyte
  ajaxful_rater
  has_many :authentications
  validates_presence_of :username

  def apply_omniauth(omniauth)
    self.email = omniauth['user_info']['email'] if email.blank?
    self.name = omniauth['user_info']['name'] if name.blank?
    self.image = omniauth['user_info']['image'] if image.blank?
    authentications.build(:provider => omniauth['provider'], :uid => omniauth['uid'])
  end

  def password_required?
    (authentications.empty? || !password.blank?) && super
  end

end

我的喜欢,auth url低于

<a href="/auth/twitter" class="auth_provider">
  <%= image_tag "twitter_64.png", :size => "64x64", :alt => "Twitter" %>
</a>

我的routh是这样的

 match 'auth/:provider/callback' => "authentications#create"

I have a bit of code here where users can login via their twitter account. the problem here is, how can i skip email confirmation for user the sign up from external services like twitter. i am using devise and i do not know how to skip the email confirmation for this type of users. my code sample is as follows

class AuthenticationsController < ApplicationController
  # GET /authentications
  # GET /authentications.json
  def index
    @authentications = current_user.authentications if current_user
  end

  # POST /authentications
  # POST /authentications.json
  def create
    omniauth = request.env["omniauth.auth"]
    authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
    if authentication
      flash[:notice] = "Signed in successfully"
     sign_in_and_redirect(:user, authentication.user)
    elsif current_user
    current_user.authentications.create!(:provider => omniauth['provider'], :uid => ['uid'])
    flash[:notice] = "Authentication successful"
    redirect_to authentication_url
    else
      user = User.new
      user.apply_omniauth(omniauth)
     if user.save
      flash[:notice] = "Signed in successfully"
      sign_in_and_redirect(:user, user)
     else
      session[:omniauth] = omniauth.except('extra')
      redirect_to new_user_registration_url
     end
    end
  rescue Exception => e
    # Just spit out the error message and a backtrace.
    render :text => "<html><body><pre>" + e.to_s + "</pre><hr /><pre>" + e.backtrace.join("\n") + "</pre></body></html>"

  end


  # DELETE /authentications/1
  # DELETE /authentications/1.json
  def destroy
    @authentication = current_user.authentications.find(params[:id])
    @authentication.destroy

    respond_to do |format|
      format.html { redirect_to authentications_url }
      format.json { head :ok }
    end
  end
end

my registration controller is as follows

class RegistrationsController < Devise::RegistrationsController

  def create
    super
    session[:omniauth] = nil unless @user.new_record?
  end

  private
  def build_resource(*args)
    super
    if session[:omniauth]
      @user.apply_omniauth(session[:omniauth])
      @user.valid?
    end
  end
end

and my user model is below

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable,  :lockable, :timeoutable and
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable


  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :about, :facebook_username, :twitter_username, :icon, :admin

  validates_uniqueness_of :username

  has_attached_file :icon, :styles => {:thumb => "64x64#"}, :default_url => 'icon_:style.png'
  validates_attachment_content_type :icon, :content_type => ['image/jpeg', 'image/png', 'image/gif']
  validates_attachment_size :icon, :less_than => 1.megabyte
  ajaxful_rater
  has_many :authentications
  validates_presence_of :username

  def apply_omniauth(omniauth)
    self.email = omniauth['user_info']['email'] if email.blank?
    self.name = omniauth['user_info']['name'] if name.blank?
    self.image = omniauth['user_info']['image'] if image.blank?
    authentications.build(:provider => omniauth['provider'], :uid => omniauth['uid'])
  end

  def password_required?
    (authentications.empty? || !password.blank?) && super
  end

end

my like to the auth url is below

<a href="/auth/twitter" class="auth_provider">
  <%= image_tag "twitter_64.png", :size => "64x64", :alt => "Twitter" %>
</a>

my routh is like this

 match 'auth/:provider/callback' => "authentications#create"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄荷港 2024-12-12 14:05:05

每当您想跳过 Devise 期间的确认时,只需在 user.save 之前使用以下内容...

user.skip_confirmation! 

所以基本上,在您的创建控制器操作中,如果它检测到omniauth逻辑,则调用它。

Whenever you want to skip confirmation for Devise period, just use the following before the user.save...

user.skip_confirmation! 

So basically, in your create controller action, if it detects omniauth logic, then call that.

挥剑断情 2024-12-12 14:05:05

这可以通过设置 confirmed_at 属性来实现,Devise 在内部设置该属性以将帐户标记为已确认:

  user.update(
    confirmed_at: Time.now.utc,
    confirmation_token: nil
  )

This could be achieved by setting the confirmed_at attribute, which Devise sets internally to mark an account as confirmed:

  user.update(
    confirmed_at: Time.now.utc,
    confirmation_token: nil
  )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文