【问题标题】:Rails 3 + Devise + Google Omniuth + without confirmation letterRails 3 + Devise + Google Omniuth + 无确认信
【发布时间】:2012-08-13 07:32:50
【问题描述】:

Rails 3.0.9
设计 1.5.3 并确认

我正在使用 Devise 和 Google OAuth2 身份验证。
当 Google 检查登录并通过时,它会将控制权返回给我的应用程序。

我的问题是:Devise 发送一封带有确认说明的信函。但我希望 Devise 不会只为通过我的应用程序注册的用户发送 Google 帐户的此类信件。

用户模型是:

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

    validates_presence_of :user_name, :address, :tel

    # Setup accessible (or protected) attributes for your model
    attr_accessible :user_name, :address, :tel, :attorney_number, :email, :password, :password_confirmation, :remember_me 

    has_many :eclaims
    has_many :createdtemplates

    before_create do |user|
        user.with_agreement = 1
    end

    def self.find_for_google_oauth2(access_token, signed_in_resource=nil)
        data = access_token.info
        user = User.where(:email => data["email"]).first

        unless user
            user = User.create(
                       user_name: 'no defined',
                       address: 'no defined',
                       tel: 'no defined',
                       attorney_number: nil,
                       email: data["email"],
                       encrypted_password: Devise.friendly_token[0,20],
                      )
        end
        user
    end 
end

用户::OmniauthCallbacksController

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
    def google_oauth2
        # You need to implement the method below in your model (e.g. app/models/user.rb)
        @user = User.find_for_google_oauth2(request.env["omniauth.auth"], current_user)

        if @user.persisted?
          flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google"
          sign_in_and_redirect @user, :event => :authentication
        else
          session["devise.google_data"] = request.env["omniauth.auth"]
          redirect_to new_user_registration_url
        end
    end 
end

routes.rb:

EFiling2::Application.routes.draw do
  root :to => "home#index"

  devise_for :users, 
    :path_names => { :sign_up => "register", :sign_in => "login", :sign_out => "logout" }, 
    :controllers => { 
      :sessions => "sessions", 
      :registrations => "registrations", 
      :confirmations => "confirmations", 
      :passwords => "passwords",
      :omniauth_callbacks => "users/omniauth_callbacks"
    }
end

【问题讨论】:

    标签: ruby-on-rails authentication devise omniauth google-oauth


    【解决方案1】:

    而不是调用create...

    user = User.create(user_name: 'no defined', ...)
    

    构建用户对象,以便您可以调用confirm!...

    user = User.new
    ...
    user.confirm!
    user.save!
    

    【讨论】:

      【解决方案2】:

      只是 noomerikal 答案的一个插件。你也可以用这个,

      user.skip_confirmation! 
      

      它只是做几乎相同的事情的不同方式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-07
        • 2015-04-09
        • 2011-09-29
        • 2011-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-10
        相关资源
        最近更新 更多