In this article, we will see how to create URLs secured by a login and a password, in Single Page Applications. I will provide an example using node.js, ExpressJS for the server, and AngularJS for the client.
The concept remains the same for Backbone, or other front-end frameworks.
First of all, we need to know what part of the application to secure. And to know it, we need to roughly understand how a Single Page Application works (although I'm sure you are already familiar with this world).
We are going to code the server with node.js, ExpressJS and PassportJS (a nodejs library to deal with authentication). To make it simple, I created a single file for the entire application.
In the client-side, we need to detect when an AJAX call returns a 401 status (which means that the user needs to authenticate), and display to the user the login form.
With AngularJS, it can easily be done by adding an interceptor to every AJAX calls ...
Your new post is loading...