How to disable browser's default login prompt on 401 response?

Mon Mar 13 2017

Paul Shan

Last week I was working on client side caching of user’s auth info, to save one extra call to the server. May be I’m not able to make you understand my task, but at the end of the day, my issues was, the server was responding with 401 error code against an ajax call and that’s why my chrome browser was throwing an ugly login prompt box, before even my client side 401 handler was called.

I was using token based authentication, so even if the user enters his account’s login id and password it was getting rejected. And the ugly looking box stole my sleep that night.

I didn’t want to make the response code to 200 and handle it with some flags inside the response; because I was using a very convention based client side framework ember.js and going against the convention will require a lot of code there. So I had to disable the login prompt programmatically.
But, unfortunately chrome doesn’t provide any way to do that.

How I solved it

After reading more on this behavior of chrome I came to know it’s not actually the only 401 response which causes the login prompt box; but it’s the combination of 401 response and also the header WWW-Authenticate: Basic.

So all I did was I changed the Basic to xBasic. You can change it to anything else or even can remove that; but a response code 401 along with WWW-Authenticate header value as Basic will trigger the login prompt. I can understand it’s kind of a hack. But this is the most convenient solution I could find keeping the conventions right.

Another way of solving

Instead of header you can also change the response code. Some may change it to 200 which is OK, but better to change it to generic 400. In both cases you need to send some flags in your response object which you can use from the client side to handle the UI flow.

SHARE THIS ARTICLE

post-thumbnail
Today everyone knows the importance of a lightning-fast website and how the speed impacts the conversion rate of a business. Today, everyone wants the site to be a PWA so that the mobile users can have an app-like experience with the website because, for the majority of the merchants, the customers come through mobile devices.
Tue Apr 20 2021
post-thumbnail
Here we are going to see how you can manage backup and restore of Postgres database with docker.
Thu Sep 03 2020
post-thumbnail
Image sliders or carousels always have increased the UI attraction of websites and they are pretty useful for reflecting the major roles/products too. In case, I am having a website that sells tee-shirts,
Mon Apr 30 2018

About VoidCanvas

This blog was created out of hobby and talks mostly about technology, web development, JavaScript, NodeJS and related topics. Thank you for reading my blog.

Copyright 2022 - www.voidcanvas.com

Popular Articles

Authentication using Google's oAuth api with node.js

Thu Mar 10 2016

OAuth authentications are pretty popular now a days and another thing which is popular is JavaScript. This article shows how to plugin google’s oAuth api for authentication in your own node application.

CSS3 Loader Snippet Collection: (Part 2 - Squares)

Sat Mar 01 2014

This is a continuation of my CSS3 loader snippet collection series. I've provided spinning css3 animation loader in the part 1 of this series and here in part 2, I'm providing various square type loading