[ad_1]
I’m not sure if it’s a misunderstanding of the configuration or a bug. Better ask.
Here are my routes.
devise_scope :v1_user do
unauthenticated :v1_user do
root to: 'v1/users/sessions#new', as: :user_unauthenticated_root
end
authenticated :v1_user do
root to: 'v1/users#profile', as: :user_authenticated_root
end
end
devise_scope :v1_admin do
# useless since already catcher on `unauthenticated` for `v1_user`
# unless a user is actually sign in...
unauthenticated :v1_admin do
root to: 'v1/admins/sessions#new', as: :admin_unauthenticated_root
end
authenticated :v1_admin do
root to: 'v1/deliveries#index', as: :admin_authenticated_root
end
end
I put a comment on the useless part.
Other interesting part is the after_sign_in|out_path_for
. I wanted to hide the real path to the main root "/" for both users and admins. I did something like:
class << self
def after_sign_in_path_for(resource)
if resource.is_a?(Admin)
SecretMigration::Engine.routes.url_helpers.admin_authenticated_root_path
else
SecretMigration::Engine.routes.url_helpers.user_authenticated_root_path
end
end
def after_sign_out_path_for(resource)
if resource == :v1_admin
SecretMigration::Engine.routes.url_helpers.admin_unauthenticated_root_path
else
SecretMigration::Engine.routes.url_helpers.user_unauthenticated_root_path
end
end
end
But since the result is always /
but controller is not specified, I had to update the code to
# application_controller.rb
class << self
def after_sign_in_path_for(resource)
if resource.is_a?(Admin)
MyEngine::Engine.routes.url_helpers.admin_authenticated_root_path
else
MyEngine::Engine.routes.url_helpers.user_authenticated_root_path
end
end
def after_sign_out_path_for(resource)
if resource == :v1_admin
MyEngine::Engine.routes.url_helpers.new_v1_admin_session_path
else
MyEngine::Engine.routes.url_helpers.new_v1_user_session_path
end
end
end
# routes.rb
devise_scope :v1_user do
authenticated :v1_user do
root to: 'v1/users#profile', as: :user_authenticated_root
end
end
devise_scope :v1_admin do
authenticated :v1_admin do
root to: 'v1/deliveries#index', as: :admin_authenticated_root
end
end
What’s the best way to use unauthenticated
in this case?
Bonus question: Do you have an idea to add spec for both authenticated
and unauthenticated
? 🤔
[ad_2]