18

is there a way to only use

http_basic_authenticate_with :name => 'user', :password => 'secret'

when the server is running on production mode?

Cojones
  • 2,930
  • 4
  • 29
  • 41

3 Answers3

34

Yes, try:

class ApplicationController < ActionController::Base
  before_filter :authenticate

  def authenticate
    if Rails.env.production?
      authenticate_or_request_with_http_basic do |username, password|
        username == "user" && password == "%$§$§"
      end 
    end
  end
end
Tim Brandes
  • 2,103
  • 15
  • 14
  • Thank you too for the quick reply! – Cojones Jan 05 '12 at 11:31
  • 1
    I'm not a fan of `Rails.env.production?` because it ends up true no matter which server the app is running on. I prefer using putting the username/password in environment variables and deciding to authenticate based on whether those are set. But I realize that's out of the scope of this question. :) –  Apr 18 '14 at 00:55
16

Just add this to your example

http_basic_authenticate_with :name => 'user', :password => 'secret' if Rails.env.production?
bokor
  • 1,829
  • 1
  • 19
  • 25
6
authenticate_or_request_with_http_basic do |username, password|
  username == "user" && password == "secret"
end if Rails.env.production?
amiuhle
  • 2,673
  • 1
  • 19
  • 28