2011-09-20 7 views
15

ここでは、ユーザーがTwitterアカウントでログインできるコードがあります。ここの問題は、どうやってTwitterのような外部サービスからサインアップしてユーザーにメールを送信するのをやめることができるのかです。私はdeviseを使用しています。このタイプのユーザーの電子メール確認をスキップする方法はわかりません。deviseを使用しているomniauthユーザーの確認メールをスキップ

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 

を下回っているように私の認証URLに

を下回っているが、次のように私のコードサンプルがあります
<a href="/auth/twitter" class="auth_provider"> 
    <%= image_tag "twitter_64.png", :size => "64x64", :alt => "Twitter" %> 
</a> 

my ro UTHがあれば、あなたの作成コントローラのアクションで、

user.skip_confirmation! 

だから、基本的には...あなただけのuser.save前に、次を使用し、工夫期間の確認をスキップしたいときはいつでも、この

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

答えて

34

のようなものですそれはomniauthロジックを検出し、それを呼び出します。

+0

を。私は外部のサービスからサインアップするたびにあなたが助けることができるかどうかはわかりません。それは電子メールを保存せず、代わりに電子メールを空白にすることはできないというエラーメッセージです。新しいユーザーと一緒に保存されるべきではない外部サービスの電子メールです – Uchenna

+2

Twitterはユーザーの電子メールアドレスを提供しませんので、別途提供するように依頼する必要があります。 – Undistraction

+2

そして、彼らが手動でメールを求めたら、それを確認する必要があります... – frandroid

0

これが確認されたように、アカウントをマークするために内部のセットを考案confirmed_at属性、設定することによって達成することができます。このヒントに感謝

user.update(
    confirmed_at: Time.now.utc, 
    confirmation_token: nil 
) 
関連する問題