Sam Mason
Sam Mason

Reputation: 1037

Custom error with grape when using http_basic

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

Upvotes: 2

Views: 272

Answers (1)

Jonny
Jonny

Reputation: 471

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 :-)

Upvotes: 1

Related Questions