當我嘗試在我的網站上使用Twitter登錄時,我遇到了一種錯誤。用Twitter登錄的Cookie溢出
ActionDispatch::Cookies::CookieOverflow in Users::OmniauthCallbacksController#twitter
ActionDispatch::Cookies::CookieOverflow
我找不到解決方案,這是我使用的代碼。
omniauth_callbacks_controller.rb
def twitter
@user = User.find_for_provider_oauth(request.env["omniauth.auth"], current_user)
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication
set_flash_message(:notice, :success, :kind => "twitter") if is_navigational_format?
else
session["devise.twitter_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
devise.rb
require "omniauth-twitter"
config.omniauth :twitter, 'xxxxxxx', 'xxxxxxxxxxxk', :strategy_class => OmniAuth::Strategies::Twitter
user.rb
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
def self.find_for_provider_oauth(auth, signed_in_resource=nil)
user = User.where(:provider => auth.provider, :uid => auth.uid).first
unless user
user = User.create(name:auth.extra.raw_info.name,
provider:auth.provider,
uid:auth.uid,
email:auth.info.email,
password:Devise.friendly_token[0,20],
invite_code:"42TXP"
)
end
user