Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom 401 page in Rails with Doorkeeper

I have a Rails 4 application using Doorkeeper to require authentication in most controllers. When I go to one of those restricted routes, I see a blank page. I notice in my terminal, Puma says:

Filter chain halted as #Proc:0x007fd650803350@/.../doorkeeper-0.7.2/lib/doorkeeper/helpers/filter.rb:8 rendered or redirected Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms)

How can I catch such 401 Unauthorized errors so I can display a custom page? My goal is to render a JSON response with a message about how to authenticate, instead of just showing a blank white page.

like image 231
Sarah Vessels Avatar asked Mar 21 '23 22:03

Sarah Vessels


1 Answers

This is actually an issue on Doorkeeper. To have a custom JSON response for 401 Unauthorized errors instead of a blank page, in ApplicationController I added:

def doorkeeper_unauthorized_render_options
  {json: '{"status": "failure", "message":"401 Unauthorized"}'}
end
like image 143
Sarah Vessels Avatar answered Apr 01 '23 07:04

Sarah Vessels