Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding HTTP Basic Authentication to some routes in routes.rb

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
like image 804
Cyrus Avatar asked Aug 24 '12 22:08

Cyrus


People also ask

What is basic HTTP authentication?

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.

How do you authenticate a route in react?

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.

How to make a Bitbucket app with authenticated react routes?

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.

What are protected routes in web apps?

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.


1 Answers

I just put

  http_basic_authenticate_with :name => "admin", :password => "secret"

inside my controller

like image 94
stef Avatar answered Oct 06 '22 15:10

stef