randika
randika

Reputation: 1539

Authlogic - Authentication via basic HTTP authentication

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?

Upvotes: 3

Views: 1823

Answers (2)

Kevin Elliott
Kevin Elliott

Reputation: 2728

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

Upvotes: 1

sikachu
sikachu

Reputation: 1221

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

Upvotes: 5

Related Questions