DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. JavaScript
  4. Detecting Location Redirects from JavaScript

Detecting Location Redirects from JavaScript

Juri Strumpflohner user avatar by
Juri Strumpflohner
·
Jun. 06, 12 · Interview
Like (0)
Save
Tweet
Share
27.64K Views

Join the DZone community and get the full member experience.

Join For Free
A common scenario for using the HTTP Location response header is during an authentication process where some kind of filter redirects to a login page in case when the user is not or no more authenticated. Handling such redirects from JavaScript turned out to be not so simple as it might initially seem. Stackoverflow has already a post on that and here (or on SO) is how I solved it in the end.

My scenario is basically the following. I have an ISA server which intercepts all of the requests and verifies the user's authentication. In case when the user isn't logged in it responds with a 302 and adds the location header to my login page.

The Initial Approach

In the case of a rich JavaScript client you would then have something like
$(document).ajaxComplete(function(e, xhr, settings){
    if(xhr.status === 302){
        //check for location header and redirect...
    }
});


But unfortunately the browser handles location redirects completely himself, wherefore the ajaxComplete callback won't fire with the 302 status code, but instead it will fire on the location redirect target (when that redirect already happened successfully). As a result, the approach before obviously won't work.

The Solution

The solution is a bit hacky but it works and I didn't found a better one so far (comment if I'm wrong). Basically what you do is to inject a custom header in your Login page for which you then listen on your client-side. The trick is to inject a header like
LoginPage: http://www.mydomain.com/Login
where the url is the one of the invoked Login page due to the location redirect. On the client side you can now check for the presence of the header like
if(xhr.status === 200){
    var loginPageHeader = xhr.getResponseHeader("LoginPage");
    if(loginPageHeader && loginPageHeader !== ""){
        window.location.replace(loginPageHeader);
    }
}

You might wonder why I included the url directly in the header value. The problem is that you have no way of detecting the URL of the redirected request in the ajaxComplete callback. You just get the initial one you started, that is, when you execute a GET /person/1 and you're no more logged in (because the session expired) and the server sends you a 302 redirect to /login, then you'll see the /person/1 as the url in your ajaxComplete callback although the content of the response is the one of your login page. That's why I directly included the url in my custom header.

Here's my explanation on SO: http://stackoverflow.com/a/10717647/50109




 

JavaScript

Published at DZone with permission of Juri Strumpflohner. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The 5 Books You Absolutely Must Read as an Engineering Manager
  • Introduction to Spring Cloud Kubernetes
  • DevOps for Developers: Continuous Integration, GitHub Actions, and Sonar Cloud
  • Chaos Engineering Tutorial: Comprehensive Guide With Best Practices

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: