2016-04-01 13 views
1

Facebookログインを使用してRoRアプリケーションを開発しています。私は2種類のユーザー(管理者とユーザー)を持っていて、どちらもFacebookでログインできます。omniauth-facebook gemを使用したRuby on RailsのFacebookログイン

管理者をコントローラの特定のアクションにリダイレクトし、クライアントを別のアクションにリダイレクトしたいとします。だから私はこれを試してみました:

マイルートを:

get 'auth/:provider/callback/admin', to: 'sessions#create_admin' 
get 'auth/:provider/callback/client', to: 'sessions#create_client' 

マイコントローラ:

def create_admin 
    auth = env["omniauth.auth"] 
    user = Spree::User.find_by(email: auth.info.email) 

    redirect_from_oauth(user) and return if user.present? 

    user = Spree::User.from_omniauth(auth, "admin") 
    redirect_to setup_step1_path(uid: user.id, t: user.confirmation_token) 
end 

def create_client 
    auth = env["omniauth.auth"] 
    user = Spree::User.find_by(email: auth.info.email) 
    user = Spree::User.from_omniauth(auth, "client") unless user.present? 

    redirect_back_or_default(root_path) 
end 

私のjavascript:

$('.facebook-btn').click(function(e) { 
    var userType = $(this).data('type'); 

    return FB.login(function(response) { 
    if (response.authResponse) { 
     return window.location = '/auth/facebook/callback/' + userType; 
    } 
    }, {scope: 'email,public_profile', return_scopes: true}) 
}); 

しかし、両方のエラーを与える:

undefined method `info' for nil:NilClass 

私のルートから削除すると、以下のようなユーザータイプが動作します。

get 'auth/:provider/callback', to: 'sessions#create' 

これを行うにはどうすればよいですか?パラマウントをオマワに送ることはできますか?

答えて

0

あなたは認証し、モデルのユーザーのために工夫を使用している場合管理クライアント属性を持っている、あなたはapplication_controller.rb上で、次の操作を行うことができます。

def after_sign_in_path_for(resource) 
    if resource.admin? 
     your_admin_path_here 
    elsif resource.client? 
     your_client_path_here 
    end 
    end 
関連する問題