2012-03-11 20 views
1

私はRails、Devise、Omniauthを使用していますが、facebook connectを使用してログインしようとしています。Facebookに接続した後、ログインを強制する方法は?

しかし、私のアプリケーションでは、facebookの部分の後に、私は '/ users/sign_in#='にリダイレクトされています。

ログによると、(コードを参照)、ユーザーが永続化されるので、このコールバックはsign_in_and_redirectメソッドを呼び出している:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController 
    def facebook 
    # You need to implement the method below in your model 
    @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user) 

    if @user.persisted? 
     logger.debug "User #{@user.email} is persisted" 
     flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook" 
     sign_in_and_redirect @user, :event => :authentication 
    else 
     logger.error "User #{@user.email} is not persisted" 
     session["devise.facebook_data"] = request.env["omniauth.auth"] 
     redirect_to new_user_registration_url 
    end 
    end 
end 

間違って行くことができるもの?

注:問題がある場合は、私はMongoIDを使用しています。ユーザーがDBに正しく追加されています。

UPDATE:私は、私がFacebookの認証を使用しているときでも、私の登録を確認する電子メールを私に送信していることに気がつきました。多分これは問題ですか?ユーザーモデルを作成する方法:確認可能、ただしfacebookで認証する場合は除きますか?それは可能ですか?ここに私のUserモデルは次のとおりです。

class User 
    include Mongoid::Document 
    # 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 

    ## Database authenticatable 
    field :email,    :type => String, :null => false, :default => "" 
    field :encrypted_password, :type => String, :null => false, :default => "" 

    ## Recoverable 
    field :reset_password_token, :type => String 
    field :reset_password_sent_at, :type => Time 

    ## Rememberable 
    field :remember_created_at, :type => Time 

    ## Trackable 
    field :sign_in_count,  :type => Integer, :default => 0 
    field :current_sign_in_at, :type => Time 
    field :last_sign_in_at, :type => Time 
    field :current_sign_in_ip, :type => String 
    field :last_sign_in_ip, :type => String 

    ## Encryptable 
    # field :password_salt, :type => String 

    ## Confirmable 
    field :confirmation_token, :type => String 
    field :confirmed_at,   :type => Time 
    field :confirmation_sent_at, :type => Time 
    field :unconfirmed_email, :type => String # Only if using reconfirmable 

    ## Lockable 
    # field :failed_attempts, :type => Integer, :default => 0 # Only if lock strategy is :failed_attempts 
    # field :unlock_token, :type => String # Only if unlock strategy is :email or :both 
    # field :locked_at,  :type => Time 

    ## Token authenticatable 
    # field :authentication_token, :type => String 

    def self.find_for_facebook_oauth(access_token, signed_in_resource=nil) 
    data = access_token.extra.raw_info 
    if user = User.where(:email => data.email).first 
     user 
    else # Create a user with a stub password. 
     User.create!(:email => data.email, :password => Devise.friendly_token[0,20]) 
    end 
    end 

    def self.new_with_session(params, session) 
    super.tap do |user| 
     if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"] 
     user.email = data["email"] 
     end 
    end 
    end 
end 

私は工夫/ omniauthドキュメントからコードを取っ:https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview

+0

DeviseControllerをオーバーライドしない場合、同じ動作をしますか? – shingara

+0

私はdevise/omniauthのドキュメントからコードを取得しました。オムニバスがなければ、念入りにすることができます(私は登録、確認、ログイン/ログアウトが可能です)、あなたが求めているのかどうかは分かりません – HappyDeveloper

答えて

0

が確認

def self.find_for_facebook_oauth(access_token, signed_in_resource=nil) 
    data = access_token.extra.raw_info 
    if user = User.where(:email => data.email).first 
    user 
    else # Create a user with a stub password. 
    user = User.new(:email => data.email, :password => Devise.friendly_token[0,20]) 
    user.skip_confirmation! 
    user.save! 
    user 
    end 
end 

をスキップするfind_for_facebook_oauth方法を変更しなければならなかった今では動作しますが、それでもURLにガベージが追加されています。私は '/#='にリダイレクトされています。これはホームページを示しています。私はこれについて別の質問を掲載するつもりです。

1

この現象を回避するには、ユーザーがfacebookで認証されたときにconfirmed_atフィールドを定義するだけで済みます。

お使いのモデルのfind_for_facebook_oauthの方法では、ユーザ作成時に:confirmed_at => Time.nowを追加する必要があります。

+0

はい、skip_confirmationが見つかりました!方法、ありがとう – HappyDeveloper

関連する問題