加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何从Omniauth Facebook创建Authlogic会话

发布时间:2020-12-17 01:56:01 所属栏目:百科 来源:网络整理
导读:这是我的控制器 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
这是我的控制器

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,...'}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读