text: var express = require('express');
  var path = require('path');
  var router = express.Router();
  var User = require('../models/user');
  
  // pages
  
  // GET route for reading data
  router.get('/', function (req, res, next) {
    User.findById(req.session.userId)
      .exec(function (error, user) {
        if (error) {
          return next(error);
        } else {
          if (user === null) {
            return res.redirect('/login');
          } else {
            return res.redirect('/home');
          }
        }
      });
  });
  
  router.get('/login', function (req, res, next) {
    User.findById(req.session.userId)
      .exec(function (error, user) {
        if (error) {
          return next(error);
        } else {
          if (user === null) {
            return res.sendFile(path.join(__dirname + '/login.html'));
          } else {
            return res.redirect('/');
          }
        }
      });
  });
  
  router.get('/register', function (req, res, next) {
    User.findById(req.session.userId)
      .exec(function (error, user) {
        if (error) {
          return next(error);
        } else {
          if (user === null) {
            return res.sendFile(path.join(__dirname + '/register.html'));
          } else {
            return res.redirect('/');
          }
        }
      });
  });
  
  router.get('/home', function (req, res, next) {
    User.findById(req.session.userId)
      .exec(function (error, user) {
        if (error) {
          return next(error);
        } else {
          if (user) {
            return res.sendFile(path.join(__dirname + '/home.html'));
          } else {
            return res.redirect('/login');
          }
        }
      });
  });
  
  // // GET route after registering
  // router.get('/profile', function (req, res, next) {
  //   User.findById(req.session.userId)
  //     .exec(function (error, user) {
  //       if (error) {
  //         return next(error);
  //       } else {
  //         if (user === null) {
  //           var err = new Error('Not authorized! Go back!');
  //           err.status = 400;
  //           return next(err);
  //         } else {
  //           return res.send('<h1>Name: </h1>' + user.username + '<h2>Mail: </h2>' + user.email + '<br><a type="button" href="/logout">Logout</a>')
  //         }
  //       }
  //     });
  // });
  
  
  
  // Requests
  
  //POST route for updating data
  router.post('/', function (req, res, next) {
    // confirm that user typed same password twice
    if (req.body.password !== req.body.passwordConf) {
      const err = new Error('Passwords do not match.');
      err.status = 400;
      res.send("passwords dont match");
      return next(err);
    }
  
    if (req.body.email &&
      req.body.username &&
      req.body.password &&
      req.body.passwordConf) {
  
      var userData = {
        email: req.body.email,
        username: req.body.username,
        password: req.body.password,
        passwordConf: req.body.passwordConf,
      }
  
      User.create(userData, function (error, user) {
        if (error) {
          return next(error);
        } else {
          req.session.userId = user._id;
          return res.redirect('/home');
        }
      });
  
    } else if (req.body.logemail && req.body.logpassword) {
      User.authenticate(req.body.logemail, req.body.logpassword, function (error, user) {
        if (error || !user) {
          var err = new Error('Wrong email or password.');
          err.status = 401;
          return next(err);
        } else {
          req.session.userId = user._id;
          return res.redirect('/');
        }
      });
    } else {
      const err = new Error('All fields required.');
      err.status = 400;
      return next(err);
    }
  })
  
  // GET for logout
  router.get('/logout', function (req, res, next) {
    if (req.session) {
      // delete session object
      req.session.destroy(function (err) {
        if (err) {
          return next(err);
        } else {
          return res.redirect('/login');
        }
      });
    }
  });
  
  module.exports = router;