Stopping users from going back to original root_path after login with Devise?

17 views
Skip to first unread message

David Williams

unread,
Jan 19, 2017, 11:56:45 AM1/19/17
to Ruby on Rails: Talk
The authenticated user root path works perfectly. What I'm trying to do is stop the user from returning back to the root 'welcome#index' after login?

authenticated :user do
root 'hub#index', as: :authenticated_root
end

# How can I stop users from going back to root 'welcome#index' after they've logged in.

Carlos Ramirez

unread,
Jan 19, 2017, 5:26:46 PM1/19/17
to Ruby on Rails: Talk
Hey David,

Devise has an additional constraint named unauthenticated which you can use to restrict access to routes for a logged in user.

For example,

authenticated :user do
   root 'hub#index', as: :authenticated_root
end

unauthenticated :user do
   # place routes for unauthenticated users only here
   resources :welcomes
end

What you'll find is that now if the user tries to navigate to the /welcomes path while logged in, the route will be undefined. 

Documentation is here:

Hope that helps!
Reply all
Reply to author
Forward
0 new messages