0

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

Community
  • 1
  • 1
Asan
  • 151
  • 2
  • 16

1 Answers1

2

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
Unathi
  • 21
  • 1
  • 5
  • both options work but still haven't been able to redirect to root path if the user closes or cancels the pop-up window. – Asan Sep 25 '16 at 13:14