Rails: Basic Auth with Authlogic

I use Authlogic and I would like to implement Basic HTTP Authentication in my controller so that I can determine which action requires authentication.

I know how to perform basic HTTP authentication authenticate_or_request_with_http_basic before filter, but I would like here, from others, how to implement it with the Authlogic plugin.

class ItemsController < ApplicationController  
  before_filter :authenticate , :only => [:index, :create]
  ...
end
+3
source share
2 answers

Here is a great screencast that explains, step by step, how to use authlogic in your rails project.

authlogic , , Application Controller.

def current_user_session
  return @current_user_session if defined?(@current_user_session)
  @current_user_session = UserSession.find
end

def current_user
  return @current_user if defined?(@current_user)
  @current_user = current_user_session && current_user_session.record
end

def require_user
  unless current_user
    store_location
    flash[:notice] = "You must be logged in to access this page"
    redirect_to new_user_session_url
    return false
  end
end

def require_no_user
  if current_user
    store_location
    flash[:notice] = "You must be logged out to access this page"
    redirect_to root_url
    return false
  end
end

, , :

before_filter :require_user, :only => [:new, :edit]
+4

:

application_controller.rb

def require_http_auth_user
  authenticate_or_request_with_http_basic do |username, password|
    if user = User.find_by_login(username) 
      user.valid_password?(password)
    else
      false
    end
  end
end

:

  before_filter : require_http_auth_user

:

http://username:password@yoursite.com (.. HTTP)

, .

+28

Source: https://habr.com/ru/post/1733291/


All Articles