'use strict';

// User routes use users controller
var users = require('../controllers/users');

module.exports = function(MeanUser, app, auth, database, passport) {

  app.route('/users/me')
    .get(users.me);

  // Setting up the userId param
  app.param('userId', users.user);

  app.route('/user/setting')
    .post(users.update);

  // AngularJS route to check for authentication
  app.route('/loggedin')
    .get(function(req, res) {
      res.send(req.isAuthenticated() ? req.user : '0');
    });

  // Setting the local strategy route
  app.route('/login')
    .get(users.auth, passport.authenticate('local', {
      failureFlash: true
    }), function(req, res) {
      res.statusCode = 302;
      res.setHeader('location', '/#!/fellow');
      res.send();
    })
    .post(users.auth, passport.authenticate('local', {
      failureFlash: true
    }), function(req, res) {
      res.send({
        user: req.user
      });
    });

};
