Using htaccess password protection in rails application


In application_controller.rb

before_filter :authenticate

protected

 def authenticate
   authenticate_or_request_with_http_basic do |username, password|
     username == "admin" && password == "test"
   end
 end

OR if you don’t want to put text password in your code you can do this in authenticate method
Digest::MD5.hexdigest(password)


 def authenticate
   authenticate_or_request_with_http_basic do |username, password|
     md5_of_password = Digest::MD5.hexdigest(password)
     username == "admin" && md5_of_password == "662f707d5491e9bce8238a6c0be92190"
   end
 end

“662f707d5491e9bce8238a6c0be92190” is md5 of hidden

You can generate your own password md5 in rails console by using this:

Digest::MD5.hexdigest('hidden')
Advertisements

I am Senior Software Engineer. I love reading, writing, sharing,developing, hiking, movies, trips, mountains, brooks, hills etc.

Tagged with: ,
Posted in Ruby Little Codes
2 comments on “Using htaccess password protection in rails application
  1. […] First: it’s not necessary, but it makes me feel better to hide the app from public view. You can do the equivalent of .htaccess authentication with a before filter in Rails. […]

    • taimoor.changaiz says:

      It is already in before filter. What you actually mean? please elaborate your suggestion. May be it prove to be more accurate way

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

StackOverFlow
Categories
Archives
%d bloggers like this: