Asan
Asan

Reputation: 151

Rails authenticate_or_request_with_http_basic response

I'm trying to redirect to the root_path if authentication fails. Currently, it's just changing the body of the page with HTTP Basic: Access denied.

Here is my controller:

class ProjectsController < ApplicationController
 before_filter :http_basic_auth, only: [:show]

  def http_basic_auth
    if (authenticate_or_request_with_http_basic do |user, password| password == @project.passcode && user == @project.passcode end if @project.private?)
    true
    else
      response.headers["Location"] = url_for(root_path)
    end
  end

end

Researched the following but I'm still not able to get it right:
Rails: What is the location option for in the render method

http://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Basic/ControllerMethods.html#method-i-authenticate_with_http_basic

http://guides.rubyonrails.org/v2.3.11/action_controller_overview.html#the-request-and-response-objects

Upvotes: 0

Views: 2967

Answers (1)

Unathi
Unathi

Reputation: 21

I you still haven't gotten this to work:

Try use authenticate_with_http_basic in place of authenticate_or_request_with_http_basic

An snippet of an example from the Rails API documentation:

    if user = authenticate_with_http_basic { |u, p| @account.users.authenticate(u, p) }
      @current_user = user
    else
      request_http_basic_authentication
    end

Upvotes: 2

Related Questions