Password protecting pages of Rails App

Often it is needed to close a whole app or some pages of a Rails app in Beta mode. A simple and quick way is using http authentication. So users (beta testers, admin) can access a Rails app by entering a single username and password.

This example shows how to password protect the whole app in beta environment using a single login/password.

Modify ApplicationController. Change username and password to your own.

[codesyntax lang=”rails”]

class ApplicationController < ActionController::Base
before_filter :authenticate if Rails.env.beta?

def authenticate
authenticate_or_request_with_http_basic('Administration') do |username, password|
username == 'admin' && password == 'password'
end
end
end

[/codesyntax]

 

 

 

 

 

References:

* authenticate_or_request_with_http_basic
* Lock down a Rails 3 app on Nginx

One thought on “Password protecting pages of Rails App”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>