ruby-on-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

你们能帮助我吗?谢谢

解决方法

您是否在facebook app config / rails的初始化中启用了电子邮件权限?

像这样:

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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...