我正在使用Omniauth gem在我的应用中提供Facebook身份验证,但Facebook重定向用户的网址为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