Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails http basic authentication for more than one user?

So I have a very basic static pages rails app, and I want to create a number of usernames and passwords for people to access these static pages. Naturally, the rails api has the http_basic_atenticate, but how are you supposed to have more than one user?

To be clear, I want to only use http_basic for more than one user. How to go about this?

Eg; user1 has password1, user2 has password2, user3 has password3, and so forth?

class StaticController < ApplicationController

http_basic_authenticate_with :name => "user", :password => "password"


  def home
  end

  def content
  end

end
like image 991
mazing Avatar asked Feb 07 '26 08:02

mazing


1 Answers

Simple solution:

h = Hash[user1: "Password1", user2: "Password2"]
authenticate_or_request_with_http_basic do |id, password|
  if h.has_key?(id.to_sym)
    password == h[id.to_sym]
  end
end      
like image 168
drummerroma Avatar answered Feb 09 '26 12:02

drummerroma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!