Redirect User After Log in Only If It's on Root_Path

Redirect user after log in only if it's on root_path

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

root url for signed in users

constraints(AuthenticatedUser) do 
root :to => "dashboard"
end

root url for non signed in users

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

Second root which redirects to users profile page when logged in without using Devise

In your static_pages_controller.rb file just add:

def home 
if logged_in?
redirect_to current_user
end
end

How do I redirect a user to their home page if they are already logged in?

Your basic problem is that you are providing a Boolean within braces which is invalid syntax. Braces signify either a hash or a block. The Rails Routing Guide section 3.10, available here, explains what you probably want to do. There are other options within section 3 that might provide a solution.

Per the guide, and the information you provided, one solution would be

root :to => "users/", constraints: lambda { |request| user_signed_in?(request) }

Depending upon your security solution, this has the concern that the user_signed_in? method must be available and it must be able to identify the user using the HTTP request.

EDIT:
I worked with your route table, making changes to it until Rails liked it. It didn't like the leading slash and it didn't like having two root paths, even with the constraint. Here is a recommended configuration, though you might need to modify it to make sure that it is doing exactly what you want:

  get    'signup',  to: 'users#new'
get 'forgot_password', to: 'users#forgot_password'
get 'login', to: 'sessions#new'
post 'login', to: 'sessions#create'
delete 'logout', to: 'sessions#destroy'

get 'dashboard' => 'users#show', as: :dashboard
resources :users

resources :scenarios do
get :download
resources :confidential_memos
end

resources :scenario_files, :only => %i[ show ]

get '/', to: 'users#show', constraints: lambda { |request| user_signed_in?(request) }
root :to => redirect('/login')

This has the concerns I mentioned above in that the user_signed_in? method must be available and it must be able to identify the user using the HTTP request.

Recommendation:

root  to: 'sessions#check'

And there:

def check
if logged_in?
return redirect_to user_profiles_path
else
# You could execute the new method here, or:
return redirect_to user_login_path
end
end

This is not exactly RESTful, and it could instead be integrated into sessions#new if you would rather do that.

Alternate, based on Duyet's suggestion:

root  to: 'sessions#new'

And, then in sessions only:

before_action :user_authenticated, only: [:new]

def user_authenticated
if logged_in?
return redirect_to user_profiles_path
end
end

Redirect User back to Page on Login


def after_sign_in_path_for(resource)
sign_in_url = new_user_session_url
if request.referer == sign_in_url
super
else
stored_location_for(resource) || request.referer || root_path
end
end

add above in application_controller. If last request will be sign in then it will call parent request.

Failing to understand Devise after sign in redirect with Rails

The answer from this post solved my problem: redirect back to current page using omniauth and devise

I had to add a store location call back in my application controller:

#application_controller.rb
before_filter :store_current_location, :unless => :devise_controller?

def store_current_location
store_location_for(:user, request.url)
end

How to redirect user after submitting form with React

The thing is that you are doing this after history.push("/"):

setReturnMessage(data.message) 
setSubmitBtn(false)

Comment those lines and the error should disappear.

You can also do this:

setReturnMessage(data.message)
setSubmitBtn(false)
history.push("/") <--- as the last line of code

Redirecting to specific homepages based on sign in with user role in rails

Devise has a controller method you can override called after_sign_in_path_for this is talked about in the devise docs here. (The devise wiki and especially the how to examples are really helpful)

In your application controller

def after_sign_in_path_for(resource)
# Here you can write logic based on roles to return different after sign in paths
if current_user.has_role? :admin
videolibrary_path
elsif current_user.has_role? :retail
retail_page_path
elsif current_user.has_role? :commercial
commercial_page_path
elsif current_user.has_role? :business
business_page_path
elsif current_user.has_role? :manager
videolibrary_path
else
new_user_path
end
end

You will need to add the routes for the pages e.g

get 'pages/retail => "pages#retail", as: :retail_page # this sets up a named link 'retail_page_path' which you can use in controllers and views

Check out the rails routing guide for more info. Also once you have written your routes you can run rake routes in the console to output all your apps routes as well as their named routes (like retail_page_path)



Related Topics



Leave a reply



Submit