i'm looking to fix this specifically with a custom failureapp
I want to reroute a non-authenticated user to my login page, lets say my route is http://localhost:3000/users/sign_in
aka devise/sessions#new
right now by default devise will reroute automatically only with the edit page when not logged in since this is built in. But any custom routes will just break the app if you're not logged in since it is not apart of the devise reroutes. How do I set these routes to reroutes if not logged in?
Quoting directly from devise below, i'm still at a loss on how to implement this correctly, what should the code look like?
goes inside lib
class CustomFailure < Devise::FailureApp
def redirect_url
new_user_session_url(:subdomain => 'secure')
end
# You need to override respond to eliminate recall
def respond
if http_auth?
http_auth
else
redirect
end
end
end
goes inside initializers
config.warden do |manager|
manager.failure_app = CustomFailure
end
goes inside application.rb
config.autoload_paths << Rails.root.join('lib')
1st off the code will NOT redirect your routes if the user is not logged in. That is a separate issue that can be fixed inside a custom controller inherited to registrations_controller. What this WILL do is redirect when a login has failed by the user. IE you type in the wrong password > redirects to new page.
For the first branch of code create an app called custom_failure.rb inside the lib folder (top level) and paste that code within.
for the second branch of code go into
/Users/admin/acltc_projects/roomkick/config/initializers/devise.rb search for
warden do
with cmd f or ctrl f and remove the # on the warden do and "end" line.
paste the manager.failure_app = CustomFailure
right below warden do. under config/locals in application.rb add config.autoload_paths << Rails.root.join('lib')
inside your module.
Finally go back to the app inside lib and modify the line new_user_session_url(:subdomain => 'secure')
for the session_url you can modify this directly to whatever you want for a route.
Restart your server and test the app.