views:

677

answers:

2

Hello everyone, 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?

+4  A: 

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
Sikachu
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.
randika
Sorry for late response. I updated the answer :)
Sikachu
+1  A: 

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

Kevin Elliott
WIlliam Jones