设计Omniauth remember_me

关于如何记住我使用Omniauth似乎有些困惑.

根据这个wiki,你需要在你的OmniauthCallbacksController中有以下内容:

remember_me(user)

另一方面,根据这个issue,你只需要这样做:

user.remember_me = true

此外,根据this将remember_me默认设置为true,您只需将以下内容添加到User.rb即可

def remember_me
  true
end

不确定哪一个是官方答案,这三个对我都不起作用.它仅适用于Mac上的Chrome,但不适用于Firefox Mac& Chrome Windows.不确定发生了什么.

我的代码看起来像这样:

# -*- 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

最佳答案 这是在
Jose Valim之前回答的
here.

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.

点赞