Aiming for elegance, one thought at a time

Wiring up basic Angular front-end for authentication (SPA Part 4)

Posted: May 10th, 2013 | Author: | Filed under: Uncategorized | No Comments »

This is the third part of a series of blog posts that will walk step-by-step through the process of creating a secure Single Page App (SPA) using node.js, passport.js, and Angular.

If you’re new here, you should start at Part 1, where we set up Passport.js. Today, we’re continuing on with Angular, and wiring up our basic authentication to the passport.js backend.

The changes required are pretty minimal, so let’s dive right in!

Front end

For login, we just need to update the login function in the security service to post the username and password to the backend:

Similarly, for signup, we post the new user’s details to the backend:


In the backend, all that’s required is updating the routes to return JSON instead of redirecting. For login:

exports.postlogin = function(req, res, next) {
  passport.authenticate('local', function(err, user, info) {
    if (err) { return next(err) }
    if (!user) {
      return res.json(403, {message: info.message});
    req.logIn(user, function(err) {
      if (err) { return next(err); }
      return res.json({user: user});
  })(req, res, next);

And for signup:

You can grab the complete code for this example at

Up next

Next week, we’ll have a couple of posts on verifying user’s email addresses.

Sign up for
* = required field

Leave a Reply