so, as the title says, how can I redirect the user to the root instead of the sign_in page after the user attempts to access a protected page?
This is my routes.rb file
.
.
.
root 'pages#home'
authenticate :user do
mount Kibana::Sinatra::Web => '/kibana', :trailing_slash => true
end
.
.
.
If you are using authenticate_user! in your controller do the following:
In Your Application Controller add this:
protected
def authenticate_user!
redirect_to root_path, notice: "You are not authorised to access this page" unless user_signed_in?
end
but if you want to authenticate from routes you have to use constraints
Edit your routes to look as following and match not found urls to root:
mount Kibana::Sinatra::Web => '/kibana', :trailing_slash => true, :constraints => AllowAccess.new match ':not_found' => redirect('/'), :constraints => { :not_found => /.*/ }
Create a new constraint "allow_access" in your lib folder and the following code
class AllowAccess def matches?(request) return false unless request.session["warden.user.user.key"] end end
And add this line in your application.rb to autoload the lib file
config.autoload_paths += Dir["#{config.root}/lib/**/"]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.