I want to add HTTP Basic Authentication to a some of my routes in my routes.rb
file.
I got the idea from http://asciicasts.com/episodes/271-resque
routes.rb
Coderbits::Application.routes.draw do
resources :snippets
root :to => "snippets#new"
authenticate :admin do
mount Resque::Server, :at => "/resque"
end
end
config/initializers/resque_auth.rb
Resque::Server.use(Rack::Auth::Basic) do |user, password|
password == "secret"
end
If I just want to protect routes that are in my rails app, what should I put in the initializer file?
My work around for right now is to add a before filter in my application controller using if the request is not for a controller that I've whitelisted:
authenticate_or_request_with_http_basic do |user, password|
user == ENV["ADMIN_USER"] && password == ENV["ADMIN_PASS"]
end
Basic HTTP authentication protects certain resources or routes with a username and password. When a user attempts to access that resource, their browser pops up a dialog asking for credentials before sending anything over. The admin panels of most home routers are secured in this way.
For example, to create an authenticated route, we can write: RequireAuth is a component that takes a Component prop. Component is a prop that is a React component. We check if there’s an authentication token present in local storage, and if it is, then we render our route, the Component , that requires authentication.
In this article, we will make a simple Bitbucket app with authenticated React routes. We will let users sign up for an account and set their Bitbucket username and password for their account. Once the user is logged in and set their Bitbucket credentials, they can view their repositories and the commits for each repository.
Often times when building a web app, you'll need to protect certain routes in your application from users who don't have the proper authentication. Protected routes let us choose which routes users can visit based on whether they are logged in.
I just put
http_basic_authenticate_with :name => "admin", :password => "secret"
inside my controller
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