开发者

HTTP Basic Custom Error Possible in Rails?

I am building an application in Rails 2.3.14 using Ruby 1.8.7.

My client has requested a very simple authentication on a webinars page. I thought using http_auth would be very fitting, as it just needs a very basic username and password.

Now, she has requested that if they hit cancel or use the wrong information, they get redirected to a page that basically says "if you forget login information, contact us."

How do I make it so that when we get the "HTTP Basic: Access denied." error, I can instead redirect to a page? Or, instead, just customize this page with our custom styles/content?

Thanks in advance.

Here is the code from my webinars controller:

class WebinarsController < ApplicationController
before_filter :authenticate, :only => [:bpr]

def bpr
 render :action => :bpr
end

pr开发者_StackOverflow社区otected 
def authenticate
  authenticate_or_request_with_http_basic do |username, password|
    username == "abc" && password == "123"
  end
end

end


If you look at the authenticate_or_request_with_http_basic source, you'll see this:

def authenticate_or_request_with_http_basic(realm = "Application", &login_procedure)
  authenticate_with_http_basic(&login_procedure) || request_http_basic_authentication(realm)
end

def authenticate_with_http_basic(&login_procedure)
  HttpAuthentication::Basic.authenticate(request, &login_procedure)
end

#...

def authenticate(request, &login_procedure)
  unless request.authorization.blank?
    login_procedure.call(*user_name_and_password(request))
  end
end

So your login_procedure block can do pretty much anything it wants as long as it returns true for a successful login. In particular, it can call redirect_to:

def authenticate
  authenticate_or_request_with_http_basic do |username, password|
    if(username == "abc" && password == "123")
      true
    else
      redirect_to '/somewhere/else/with/instructions'
    end
  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜