Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

raise custom error based on different conditions in resource_owner_authenticator in config/initializers/doorkeeper.rb

I want to raise custom error and customise error response based on different conditions. for authentication, I have a few conditions where a user is not authenticated, for example, not active user, user profile is incomplete etc etc.

I followed this, #315 , I configured the same but execution stops at line where it raises the custom doorkeeper error.

Doorkeeper.configure do resource_owner_authenticator do @user = User.find(session[:user_id]) || redirect_to(user_login_path(return_to: request.fullpath)) raise Doorkeeper::Errors::OwnError unless @user.status == 'active' end end

Please help if anybody have any clue.

like image 283
Shrey Patel Avatar asked Dec 29 '25 17:12

Shrey Patel


1 Answers

I had the same issue until I read the NEWS.md file:

- [#749] Allow user to raise authorization error with custom messages.
  Under `resource_owner_authenticator` block a user can
  `raise Doorkeeper::Errors::DoorkeeperError.new('custom_message')`

For password flow POST /oauth/token:

 resource_owner_from_credentials do |routes|
    raise Doorkeeper::Errors::DoorkeeperError.new('custom_message')
 end

Gives a response like:

 {
      "error": "custom_message",
      "error_description": "The authorization server encountered an unexpected condition which prevented it from fulfilling the request."
 }
like image 173
Craig Wetzelberger Avatar answered Jan 01 '26 13:01

Craig Wetzelberger



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!