5

There seems to be some confusion on how to get remember me working with Omniauth.

According to this wiki, you need to have the following in your OmniauthCallbacksController:

remember_me(user)

On the other hand, according to this issue, you just need to do this:

user.remember_me = true

In addition, making remember_me default to true according to this, you just need to add the following to your User.rb

def remember_me
  true
end

Not sure which one is the official answer, and all three doesn't work for me. It only works for Chrome on Mac, but doesn't for Firefox Mac & Chrome Windows. Not sure what is going on.

My code looks like this:

# -*- encoding : utf-8 -*-
class OmniauthCallbacksController < Devise::OmniauthCallbacksController

    include Devise::Controllers::Rememberable

    def all
        omniauth = request.env["omniauth.auth"]
        auth = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
        if auth

            auth.update_with_omniauth omniauth
            auth.save!

            # ???
            remember_me auth.user
            auth.user.remember_me = true

            if user_signed_in?
                redirect_back_or settings_path(current_user)
            else
                sign_in_and_redirect auth.user, event: :authentication
            end
        else
            if user_signed_in?
                current_user.build_auth(omniauth).save!
                redirect_back_or settings_path(current_user)
            else
                session["devise.omniauth"] = omniauth.except('extra')
                redirect_to new_user_registration_url
            end
        end
    end

    alias_method :facebook, :all
    alias_method :twitter, :all

end
JPN
  • 632
  • 12
  • 24

1 Answers1

3

This was answered here by Jose Valim.

The first option is the correct one. The other two simply set the default value of the field to true, which means it will be automatically remembered whenever the first one is called.

If it works in some browsers or not, it is likely a browser issue because the server is definitely sending the proper cookies. Try to confirm if the cookie is indeed correct and find out if the browser is storing it properly.

Community
  • 1
  • 1
JPN
  • 632
  • 12
  • 24