Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 1910
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 31, 20222022-05-31T07:14:09+00:00 2022-05-31T07:14:09+00:00

Devise and `unauthenticated` with 2 models

  • 0

[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]

  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 4795 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1063 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1009 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.