使用 Devise 和 Omniauth 的路由问题
我试图让 OAuth 和 Devise 一起工作,但在尝试通过 Facebook 进行身份验证时,我收到 Controller::RoutingError (No Route matches "/users/auth/facebook/callback"):
。
奇怪的是,使用 Google Apps 却不会出现此问题。 (相同的回调路线)。
有什么想法吗?
callback_controller:
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model
@user = User.find_for_facebook_oauth(env["omniauth.auth"], current_user)
if @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
sign_in_and_redirect @user, :event => :authentication
else
session["devise.facebook_data"] = env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def google_apps
@user = User.find_for_google_apps_oauth(env["omniauth.auth"], current_user)
if @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google Apps"
sign_in_and_redirect @user, :event => :authentication
else
session["devise.google_apps_data"] = env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def passthru
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
end
paths.rb:
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } do
get '/users/auth/:provider' => 'users/omniauth_callbacks#passthru'
end
为什么:provider =>; "facebook"
触发 RoutingError,但不触发 :provider =>; “google_apps”
?
I am trying to get OAuth and Devise working together but I get Controller::RoutingError (No route matches "/users/auth/facebook/callback"):
when trying to auth via Facebook.
Weirdly, the problem doesn't happen with Google Apps. (same callback route).
Any ideas?
callback_controller:
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model
@user = User.find_for_facebook_oauth(env["omniauth.auth"], current_user)
if @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
sign_in_and_redirect @user, :event => :authentication
else
session["devise.facebook_data"] = env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def google_apps
@user = User.find_for_google_apps_oauth(env["omniauth.auth"], current_user)
if @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google Apps"
sign_in_and_redirect @user, :event => :authentication
else
session["devise.google_apps_data"] = env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def passthru
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
end
routes.rb:
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } do
get '/users/auth/:provider' => 'users/omniauth_callbacks#passthru'
end
Why would the :provider => "facebook"
trigger a RoutingError, but not :provider => "google_apps"
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到它:
在设计用户模型中,我有模块:
:omniauth_providers => [:facebook, :openid, :google_apps]
添加了(显然)它没有做我想象的事情。我删除了该行,现在我的所有身份验证逻辑再次正常工作。Found it:
in the devise user model, i had the module:
:omniauth_providers => [:facebook, :openid, :google_apps]
added which (apparently) doesn't do what I thought it did. I removed the line and now all my auth logic works properly once again.