2015-10-16 29 views
4

這裏是我的控制器的Rails如何從Omniauth Facebook的創建Authlogic會議

def social_login 
    user = User.from_omniauth(env["omniauth.auth"]) 
    session_params = user.attributes.merge("email" => user.email, "password" => user.crypted_password) 
    @user_session ||= UserSession.new(session_params, true) 
    if @user_session.save 
     user = User.where(email: @user_session.email).first 
     redirect_to root_path, :notice => "Signed in succesfully from #{env["omniauth.auth"].provider.titleize}. Greetings #{user.name.titleize} ;)"  
    else 
     flash.now[:alert] = "Sign in failed." 
     render "new" 
    end 
end 

這裏是模式處理omniauth過程

def self.from_omniauth(auth) 
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
     user.provider = auth.provider 
     user.uid = auth.uid 
     user.name = auth.info.name 
     user.email = auth.info.email 
     user.password = auth.credentials.token 
     user.password_confirmation = auth.credentials.token 
     user.oauth_token = auth.credentials.token 
     user.oauth_expires_at = Time.at(auth.credentials.expires_at) 
     user.save! 
    end 
    end 

努力時,我總是得到錯誤保存會話。它說:

Authlogic::Session::Existence::SessionInvalidError: Your session is invalid and has the following errors: Email is not valid 

你們能幫我嗎?謝謝

回答

0

您是否在Facebook應用程序config/rails的初始化中啓用了電子郵件許可?

像這樣:

config.omniauth :facebook, "APP_ID", "APP_SECRET", {:scope => 'email,...'} 
相關問題