How to add simple http authentication in Ruby on Rails

How to add simple http authentication in Ruby on Rails

/ #Ruby on Rails


Sometimes you don't need a fancy authentication for your website, here's how to can implement that.

Instead of implementing something like Devise, or your own authentication, you can just use simple http authentication from Ruby on Rails.

Maybe you just need to hide a simple admin section, not ready to show the website to the world, etc. When that's the case, this simple solution is really great.

class Admin::PostsController < ApplicationController
  http_basic_authenticate_with name: "codewithstein", password: "rubyonrails"

  def index
    @posts = Post.order(created_at: :desc)
  end
end

So with this one line, you will now need a username and password to access this controller. Note that saving passwords like this in clear text is not so called best practice, so keep that in mind.

Comments

No comments yet...

Add comment

Info

Please log in to comment!

Newsletter

Subscribe to my weekly newsletter. One time per week I will send you a short summary of the tutorials I have posted in the past week.