1
votes

Im using http_basic method in Grape in my rails app, I have got it working but would like to provide a custom error if the authentication details are incorrect.

http_basic do |username, password|
  @project = Project.where(api_key: username).first
end

This works but I cant seem to be able to throw a custom error if the project cannot be found

1

1 Answers

0
votes
http_basic do |username, password|
  @project = Project.where(api_key: username).first
  @project ? true : error!('Unauthorized. Invalid or expired token.', 401) 
end

Don't forget to check that password is also valid :-)