我正在使用 Omniauth gem 在我的应用程序中提供 Facebook 身份验证,但是 Facebook 重定向用户的 url 是http://localhost:3000/users/login#_=_
在用户输入其电子邮件/密码后立即发生的。我不知道是什么原因造成的,谷歌似乎工作正常。
Omniauth Callback
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def all
user = User.from_omniauth(request.env["omniauth.auth"])
if user.persisted?
flash.notice = "Signed in!"
sign_in_and_redirect user
else
session["devise.user_attributes"] = user.attributes
redirect_to new_user_registration_url
end
end
# alias_method :twitter, :all
alias_method :facebook, :all
alias_method :google_oauth2, :all
end
Routes.rb
devise_for :users, path_names: {sign_in: "login", sign_out: "logout"},
controllers: {omniauth_callbacks: "omniauth_callbacks"}
User.rb
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.name = auth.info.name
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
end
end
def self.new_with_session(params, session)
if session["devise.user_attributes"]
new(session["devise.user_attributes"]) do |user|
user.attributes = params
user.valid?
end
else
super
end
end