Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Password-only Login

What is the best way to password protect a view? I already am generating a password but I don't want a username and password login, just the password. I have the password being stored currently as a attribute in the File class, and was using this:

before_filter :restrict, :only => :show    

authenticate_or_request_with_http_basic do |password|
  password == @file.password
end

However, it still prompts the user for a username and doesn't log in correctly because it is missing a username. Is there a way to use this method and only have the prompt ask for the password only? If not what is the best way to go about doing this?

like image 784
user1470511 Avatar asked Nov 27 '22 10:11

user1470511


1 Answers

authenticate_or_request_with_http_basic is implemented using HTTP Basic Auth that requires combination of username and password to be entered. If you want to enable password only authentication, you will have to write your own authentication method

before_filter :restrict, :only=>:show

def restrict(password)
  render :status=>401, :text=>"Unathorized" unless password == @file.password
end 
like image 149
Yuriy Goldshtrakh Avatar answered Dec 05 '22 13:12

Yuriy Goldshtrakh