3

I want to use "authenticate_ with_ http_ basic" but I just can not get it working.

In my RoR app Authlogic is working fine and I'm using User Sessions for that. While keeping that method as it is now i need to use authenticate_with_http_basic.I have a iPhone SDK app and now I need to fetch some products from my webapp and display as list. So I'm assuming that i need to send the request to my webapp like this; http://username:[email protected]/products/

So my question is to validate this username and password and what I need to do to my UserSession Controller?

2 Answers 2

5

You don't need to do anything with UserSessionController, since that controller would only handle login form submit and logout.

Authlogic and authenticate_with_http_basic is irrelevant to each other. If you want to authenticate via HTTP basic, you just need to create a method to authenticate using method provided by Rails, and put that method on the before_filter. By logging in via HTTP authentication, I assume that the username and password should be mandatory for every request.

So finally, your ProductsController would be something like this

class ProductsController < ApplicationController
  before_filter :authenticate_via_http_basic

  # In case you have some method to redirect user to login page, skip it
  skip_before_filter :require_authentication

  ...

  protected

  def authenticate_via_http_basic
    unless current_user
      authenticate_with_http_basic do |username, password|
        if user = User.find_by_username(username)
          user.valid_password?(password)
        else
          false
        end
      end
    end
  end
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, This is working now. but after integrating the http_basic_athentication on the controller, UserSession based authentication is not working now. I always get the http_basic_athentication Login prompt.
1

Authentication via HTTP Auth is now integrated into AuthLogic, and it is enabled by default.

1 Comment

I see this method authenticate_with_http_basic(&block) in the method list of the docs, but I can't seem to figure out how to use it. What would a solution that uses the integrated Authentication via HTTP Auth look like versus the solution that Sikachu provided?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.