Omniauth session ends when browser is closed

In my rails 3 application, I use Omniauth for the user authentication part (fb / twitter).

Actually, I follow this:

https://github.com/RailsApps/rails3-mongoid-omniauth

https://github.com/RailsApps/rails3-mongoid-omniauth/wiki/Tutorial

But, when I close the browser session and I need to log in again. How can I save a session to return users?

Any help would be greatly appreciated!

+7
source share
3 answers

Devise offers this feature through its memory module. OmniAuth integrates seamlessly with it through the OmniAuth module (you'll never guess). This is even mentioned in the second link you posted!

+4
source

What you do not need, you need to set a persistent cookie when creating a session, and then get this value when setting the current user.

In ApplicationController just change the current_user method to:

 def current_user return unless cookies.signed[:permanent_user_id] || session[:user_id] begin @current_user ||= User.find(cookies.signed[:permanent_user_id] || session[:user_id]) rescue Mongoid::Errors::DocumentNotFound nil end end 

And in your SessionsController change your create to set a cookie if the user wants:

 def create auth = request.env["omniauth.auth"] user = User.where(:provider => auth['provider'], :uid => auth['uid']).first || User.create_with_omniauth(auth) session[:user_id] = user.id cookies.permanent.signed[:permanent_user_id] = user.id if user.really_wants_to_be_permanently_remembered redirect_to root_url, :notice => "Signed in!" end 
+9
source

Please make sure that the cookie policy that your rail should use has reasonable settings for your use case (see link in my comment above). All I can imagine right now (knowing that I know, sitting where I am sitting) is the cookie (s) ha (s / ve) properties, which are suboptimal / undesirable in your context.

Please check your cookie settings in a browser debugging / development tool such as firebug, firecookie or chrome development tools.

Sorry, everything I can come up with, given my knowledge of the problem. Feel free to contact me again with detailed information about your cookie settings and testing.

My 2Cents.

+1
source

All Articles