How to use HTTP basic authorization in a controller.
FilterController uses a before_filter callback, calling authenticate_or_request_with_http_basic.
PostsController uses the http_basic_authentication_with callback.
How to use HTTP basic authorization in a controller.
FilterController uses a before_filter callback, calling authenticate_or_request_with_http_basic.
PostsController uses the http_basic_authentication_with callback.
| require 'digest' | |
| class FilterController < ApplicationController | |
| before_filter :authenticate | |
| def index | |
| render text: "hello world, from the index!" | |
| end | |
| protected | |
| def authenticate | |
| user = authenticate_or_request_with_http_basic do |username, password| | |
| username == 'foo' && password == 'bar' | |
| end | |
| @current_user = user | |
| end | |
| end |
| class PostsController < ApplicationController | |
| http_basic_authenticate_with name: "dhh", password: "secret", except: :index | |
| def index | |
| render text: "Everyone can see me!" | |
| end | |
| def edit | |
| render text: "I'm only accessible if you know the password" | |
| end | |
| end | |