1

I implemented a static page with ruby on rails that shows all the stats related to my work. I can deploy to the admin page but it requires username/pwd which is annoying. So I want a static page with simple authentication like 4 digits of codes or only some devices and access it. Any suggestion?

david
  • 11
  • 1

1 Answers1

2

add a basic authentication method on your app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  ...

  private

  def authenticate
    authenticate_or_request_with_http_basic do |username, password|
      username == "admin" && password == "admin"
    end
  end

end

Then add a before_filter (or before_action for rails5) callback on any controller you want to use this authentication.

For example, if you want to authenticate to access reports action from PagesController

class PagesController < ApplicationController

  before_filter :authenticate, only: [:report]

  def about
  end

  def report
  end

end
sa77
  • 3,563
  • 3
  • 24
  • 37
  • Is there any other way without username? It seems that authenticate_or_request_with_http_basic method requires username. – david Jun 13 '17 at 14:14
  • 1
    keep the username blank if you don't want it `username == "" && password == "admin"` – sa77 Jun 13 '17 at 14:26
  • yes I can leave with blank for username but I meant only password field without an username input :) – david Jun 14 '17 at 03:57
  • what you are asking is not possible with http basic authentication .. to do that, you need to setup a controller action and associated page with dedicated form to intake password and match the submitted password – sa77 Jun 14 '17 at 04:01
  • I was hoping that http basic auth can do that haha. Thanks anyways! – david Jun 14 '17 at 09:49