简体   繁体   English

Rails 4 - 设计不绑定会话

[英]Rails 4 - Devise not binding session

I recently upgraded my app to Rails 4. I have been authenticating the users with and only with a custom openid port over omniauth. 我最近将我的应用程序升级到Rails 4.我一直在使用omniauth上的自定义openid端口对用户进行身份验证。

Controller (Omniauth Callbacks) 控制器(Omniauth回调)

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def openid
    @user = User.where(provider: auth_hash["provider"], uid: auth_hash["uid"]).first_or_initialize
    @user.name = auth_hash["information"]["name"]
    @user.email = auth_hash["information"]["email"]
    @user.save!
    sign_in_and_redirect @user, :event => :authentication
  end

  protected

  def auth_hash
    request.env['omniauth.auth']
  end
end

Routes 路线

devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

devise_scope :user do
  get 'sign_in', :to => 'devise/sessions#new', :as => :new_user_session
  get 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
end

root :to => "users#front"

[...]

Issue 问题

After logging in correctly at the extern service, the user is being redirected back to OmniauthCallback#openid . 在extern服务上正确登录后,用户将被重定向回OmniauthCallback#openid The user is getting created correctly without any issues. 正确创建用户没有任何问题。 After that the user is redirected to root without any problems. 之后,用户被重定向到root而没有任何问题。 But at that point he is still not logged in! 但那时他还没有登录!

My sessions are stored in the Database correctly aswell. 我的会话也正确地存储在数据库中。 I do not get any errors, it simply does not work as it should at sign_in_and_redirect @user, :event => :authentication . 我没有收到任何错误,它根本无法在sign_in_and_redirect @user, :event => :authentication How can I bind the session to the user properly? 如何正确地将会话绑定到用户?

Update 更新

Using Cookies to store Sessions instead of the database doesn't solve. 使用Cookie来存储Sessions而不是数据库无法解决。
Using sign_in @user and reloading page doesn't solve. 使用sign_in @user和重新加载页面无法解决。
Using @user.save instead of @user.save! 使用@user.save而不是@user.save! doesn't solve. 不解决。

I'm grateful for any ideas. 我很感激任何想法。

I finally figured something out. 我终于搞清楚了。 I added this line to my oauth callback method before "sign_in_and_redirect @user": 我在“sign_in_and_redirect @user”之前将此行添加到我的oauth回调方法中:

session[:user_id] = @user.id

After that, current_user is set properly. 之后,正确设置current_user。 Hope this helps somebody. 希望这有助于某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM