title: Node / Angular Authentication
body: <p>I'm trying to figure out how to authenticate a user using node.js and angular.js. I have a node server which has a route pointing to api/login. Users can register and log in everything works. But when I get to the client side, I have no idea where to go. I have set up a http interceptor which send the user back to the login page if a session doesn't exist. I am currently using session express. Below are snippets of my code:</p>
  
  <p>Login Route:</p>
  
  <pre><code>router.post('/login', function(req, res, next) {
  var username = req.body.username;
  var password = req.body.password;
  
  User.findOne({username: username, password: password}, function(err, user) {
    if(err) {
      console.log(err);
      res.status(500).send();
    }
    if(!user) {
      res.status(404).send();
    } else {
      console.log('Set' + user);
      req.session.user = user;
      res.status(200).send();
    }
  });
  </code></pre>
  
  <p>Protecting this endpoint:</p>
  
  <pre><code>app.get('/api/endpoint', function(req, res) {
  if(!req.session.user) {
    console.log('Unauthorized');
    res.status(401).send();
  } else {
    console.log('Logged in');
    res.status(200).send();
  }
  });
  app.use(bodyParser.urlencoded({extended: true}));
  
  app.use(bodyParser.json());
  
  app.use('/api', require('./routes/login'));
  app.use('/api', require('./routes/register'));
  
  app.listen(port);
  </code></pre>
  
  <p>I'm not sure if this is detailed enough but I know there's something really simple going wrong here I just cannot see it haha! Any help would be greatly appreciated!</p>