1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Do not use the dynamic :action segment on Omniauth routes.

This was deprecated on rails/rails#23980.

We now generate scope and provider specific routes, like `user_facebook_omniauth_callback`
or `user_github_omniauth_callback`.

We could deprecate the `omniauth_authorize_path` in favor of the generated routes, but
the `shared/links.html.erb` depends on it to generate all omniauth links at once.

Closes #3983.
This commit is contained in:
Lucas Mazza 2016-03-07 11:19:27 -03:00
parent cecb3ee45b
commit ec07bdb315
4 changed files with 20 additions and 24 deletions

View file

@ -4,14 +4,14 @@ module Devise
def self.define_helpers(mapping) def self.define_helpers(mapping)
end end
def omniauth_authorize_path(resource_or_scope, *args) def omniauth_authorize_path(resource_or_scope, provider, *args)
scope = Devise::Mapping.find_scope!(resource_or_scope) scope = Devise::Mapping.find_scope!(resource_or_scope)
_devise_route_context.send("#{scope}_omniauth_authorize_path", *args) _devise_route_context.send("#{scope}_#{provider}_omniauth_authorize_path", *args)
end end
def omniauth_callback_path(resource_or_scope, *args) def omniauth_callback_path(resource_or_scope, provider, *args)
scope = Devise::Mapping.find_scope!(resource_or_scope) scope = Devise::Mapping.find_scope!(resource_or_scope)
_devise_route_context.send("#{scope}_omniauth_callback_path", *args) _devise_route_context.send("#{scope}_#{provider}_omniauth_callback_path", *args)
end end
end end
end end

View file

@ -441,19 +441,17 @@ ERROR
set_omniauth_path_prefix!(path_prefix) set_omniauth_path_prefix!(path_prefix)
providers = Regexp.union(mapping.to.omniauth_providers.map(&:to_s)) mapping.to.omniauth_providers.each do |provider|
match "#{path_prefix}/#{provider}",
to: "#{controllers[:omniauth_callbacks]}#passthru",
as: "#{provider}_omniauth_authorize",
via: [:get, :post]
match "#{path_prefix}/:provider", match "#{path_prefix}/#{provider}/callback",
constraints: { provider: providers }, to: "#{controllers[:omniauth_callbacks]}##{provider}",
to: "#{controllers[:omniauth_callbacks]}#passthru", as: "#{provider}_omniauth_callback",
as: :omniauth_authorize, via: [:get, :post]
via: [:get, :post] end
match "#{path_prefix}/:action/callback",
constraints: { action: providers },
to: "#{controllers[:omniauth_callbacks]}#:action",
as: :omniauth_callback,
via: [:get, :post]
ensure ensure
@scope = current_scope @scope = current_scope
end end

View file

@ -1,23 +1,21 @@
require 'test_helper' require 'test_helper'
class OmniAuthRoutesTest < ActionController::TestCase class OmniAuthRoutesTest < ActionController::TestCase
ExpectedUrlGeneratiorError = ActionController::UrlGenerationError
tests ApplicationController tests ApplicationController
def assert_path(action, provider, with_param=true) def assert_path(action, provider, with_param=true)
# Resource param # Resource param
assert_equal @controller.send(action, :user, provider), assert_equal @controller.send(action, :user, provider),
@controller.send("user_#{action}", provider) @controller.send("user_#{provider}_#{action}")
# With an object # With an object
assert_equal @controller.send(action, User.new, provider), assert_equal @controller.send(action, User.new, provider),
@controller.send("user_#{action}", provider) @controller.send("user_#{provider}_#{action}")
if with_param if with_param
# Default url params # Default url params
assert_equal @controller.send(action, :user, provider, param: 123), assert_equal @controller.send(action, :user, provider, param: 123),
@controller.send("user_#{action}", provider, param: 123) @controller.send("user_#{provider}_#{action}", param: 123)
end end
end end
@ -32,7 +30,7 @@ class OmniAuthRoutesTest < ActionController::TestCase
test 'should generate authorization path' do test 'should generate authorization path' do
assert_match "/users/auth/facebook", @controller.omniauth_authorize_path(:user, :facebook) assert_match "/users/auth/facebook", @controller.omniauth_authorize_path(:user, :facebook)
assert_raise ExpectedUrlGeneratiorError do assert_raise NoMethodError do
@controller.omniauth_authorize_path(:user, :github) @controller.omniauth_authorize_path(:user, :github)
end end
end end

View file

@ -96,12 +96,12 @@ class DefaultRoutingTest < ActionController::TestCase
test 'map omniauth callbacks' do test 'map omniauth callbacks' do
assert_recognizes({controller: 'users/omniauth_callbacks', action: 'facebook'}, {path: 'users/auth/facebook/callback', method: :get}) assert_recognizes({controller: 'users/omniauth_callbacks', action: 'facebook'}, {path: 'users/auth/facebook/callback', method: :get})
assert_recognizes({controller: 'users/omniauth_callbacks', action: 'facebook'}, {path: 'users/auth/facebook/callback', method: :post}) assert_recognizes({controller: 'users/omniauth_callbacks', action: 'facebook'}, {path: 'users/auth/facebook/callback', method: :post})
assert_named_route "/users/auth/facebook/callback", :user_omniauth_callback_path, :facebook assert_named_route "/users/auth/facebook/callback", :user_facebook_omniauth_callback_path
# named open_id # named open_id
assert_recognizes({controller: 'users/omniauth_callbacks', action: 'google'}, {path: 'users/auth/google/callback', method: :get}) assert_recognizes({controller: 'users/omniauth_callbacks', action: 'google'}, {path: 'users/auth/google/callback', method: :get})
assert_recognizes({controller: 'users/omniauth_callbacks', action: 'google'}, {path: 'users/auth/google/callback', method: :post}) assert_recognizes({controller: 'users/omniauth_callbacks', action: 'google'}, {path: 'users/auth/google/callback', method: :post})
assert_named_route "/users/auth/google/callback", :user_omniauth_callback_path, :google assert_named_route "/users/auth/google/callback", :user_google_omniauth_callback_path
assert_raise ExpectedRoutingError do assert_raise ExpectedRoutingError do
assert_recognizes({controller: 'ysers/omniauth_callbacks', action: 'twitter'}, {path: 'users/auth/twitter/callback', method: :get}) assert_recognizes({controller: 'ysers/omniauth_callbacks', action: 'twitter'}, {path: 'users/auth/twitter/callback', method: :get})