RoR
RoR

Reputation: 16502

Rails authenticate_or_request_with_http_basic

When a user tries to connect via this method and it fails. How do I redirect_to? Thanks in Advance.

    class ApplicationController < ActionController::Base
  protect_from_forgery

  USER_NAME, PASSWORD = "admin", "admin"
  helper_method :authenticate

  private
  def authenticate
    begin
      authenticate_or_request_with_http_basic do |user_name, password|
        user_name == USER_NAME && password == PASSWORD
      end
    rescue
      "HTTP Basic: Access denied"
    else

      redirect_to root_path
    end

  end
end

Upvotes: 0

Views: 4815

Answers (2)

theIV
theIV

Reputation: 25794

In the past, we've made authenticate a before_filter which will just perform the authenticate_or_request_with_http_basic, and then have our redirects/renders happen in their respective actions.

I just re-read your question & code sample. What exactly are you looking to have happen? A redirect on fail of the authentication?

Edit: I've never done this, but it seems like trying to follow the docs might be your best bet, in particular, the "more advanced basic example."

I ultimately don't know enough about the inner workings of basic auth, but it strikes me as it could also boil down to an Apache/(insert your awesome web server here) configuration. Someone will definitely correct me on that if I'm wrong.

Upvotes: 0

Related Questions