I have a root_path
on my Rails application that is not user-protected i.e. it's a simple portal homepage, with a login form.
After the users log in, I'd like it to go to dashboard_path
.
I've done this:
def signed_in_root_path(scope_or_resource)
dashboard_path
end
This apparently should be used when an user signs in, and I don't want it to go to the root_path
, while still keeping the user going back to a previous page if it tries to hit a restricted area and it's either timed out or not logged in.
i.e.:
restricted_page -> login -> restricted_page_but_logged_in
I don't want to change this behavior, and that's why I haven't used after_sign_in_path
, but want to redirect it if it's on root_path
, or any route that doesn't require user authentication.
My problem is that this is not working. After signing in, I'm getting redirected back to root_path
, which I believe is because of after_sign_in_path
getting triggered before.
Is there any way to do this? Thanks!
Edit: This works the second time I log in, i.e. I go to root_path
, log in, gets the flash message stating me that I'm logged in, and enter username and password again on the form on root_path
. I successfully get redirected to dashboard_path
. Still, not quite the behavior I want.
Devise has a helper method after_sign_in_path_for which can be used to override the default Devise route to root after login/sign-in. To implement a redirect to another path after login, simply add this method to your application controller. #class ApplicationController < ActionController::Base def after_sign_in_path_for (resource) users_path end
We will also discuss how you can return the user back to the page they were on after logging in. In React Router v6, there are two ways you can use to redirect a user — the Navigate component and the useNavigate () hook. Create a simple React application using the create-react-app command.
This is where your application receives and processes the response from Auth0, and is often the URL to which users are redirected once the authentication is complete. To learn more about how the redirect_uri works, see OAuth 2.0 Authorization Framework. You can use a cookie or the browser session to store a return URL value.
In React Router v6, there are two ways you can use to redirect a user — the Navigate component and the useNavigate () hook. Create a simple React application using the create-react-app command. You will use this application to test out how the Navigate component and the useNavigate () hook work.
Just a thought
You can define two root url one for signed in url which will point to dashboard and second for non signed in users which will point to login page
define different root url based on some constraints in routes.rb
constraints(AuthenticatedUser) do
root :to => "dashboard"
end
root :to=>"users/signin"
then create class AuthenticatedUser in lib/authenticated_user.rb
class AuthenticatedUser
def self.matches?(request)
user_signed_in?
end
end
now if user is signed in root_url will point to dashboard else it will point to signin page
Your can also create two roots using(did not tested it yet)
root :to => "dashboard", :constraints => {user_signed_in?}
root :to => "users/signin"
more on constrains http://edgeguides.rubyonrails.org/routing.html#request-based-constraints
Note
The priority of url is based upon order of creation, first created -> highest priority resources
It sounds like you're over complicating the issue. If you get into overriding routing variables it just leads to headaches down the line. I would recommend using a before filter to require a login and use the except param or skip that before filter for your landing page if you're using a separate controller. As an example:
class ApplicationController < ActionController::Base
before_filter :require_login, :except => :root
def root
# Homepage
end
protected
def require_login
redirect_to login_path and return unless logged_in?
end
end
(Make sure you have logged_in? defined)
If you are using a separate controller it will look something like this:
class HomepageController < ApplicationController
skip_before_filter :require_login
before_filter :route
protected
def route
redirect_to dashboard_path and return if logged_in?
end
end
Regarding proper routing after a login, that would come down to what you're doing when you're creating your session. Regardless, this setup should catch anyone that's logged in trying to hit the homepage, and route them to your dashboard and anyone trying to hit restricted content (Anything besides root) and route them to the login_path
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With