var app = require('express').Router();

process.env.NTB_USER_AGENT = 'Hytteadmin/' + require('../package.json');
var turbasen = require('turbasen-auth');

app.get('/', function(req, res) {
  if (req.session && req.session.user) {
    res.json(req.session.user);
  } else {
    res.status(401);
    res.json({authenticated: false});
  }
});

app.get('/login/sherpa/signon', function(req, res) {
  throw new Error('Not Implemented');
});

app.get('/login/sherpa/callback', function(req, res) {
  throw new Error('Not Implemented');
});

app.post('/login/turbasen', turbasen.middleware, function(req, res, next) {
  if (req.turbasenAuth) {
    req.session.user = req.turbasenAuth;
    res.status(200);
    res.json(req.session.user);
  } else {
    req.session.destroy(function(err) {
      if (!err) {
        err = new Error('Invalid email or password');
        err.status = 401;
      }

      return next(err);
    });
  }
});

app.post('/logout', function(req, res) {
  req.session.destroy(function(err) {
    if (err) { throw err; }

    res.status(204).end();
  });
});


module.exports = app;

@@ -144,24 +144,238 @@
 en-auth');%0A%0A
+process.env.DNT_CONNECT_USER_AGENT = 'Hytteadmin/' + require('../package.json');%0Avar Connect = require('dnt-connect');%0Avar connect = new Connect(%0A  process.env.DNT_CONNECT_CLIENT,%0A  process.env.DNT_CONNECT_KEY%0A);%0A%0A
 app.get('/',
@@ -568,22 +568,42 @@
 gin/
-sherpa/
+dnt', connect.middleware('
 signon'
+)
 , fu
@@ -621,144 +621,568 @@
  res
-) %7B%0A  throw new Error('Not Implemented');%0A%7D);%0A%0Aapp.get('/login/sherpa/callback', function(req, res) %7B%0A  throw new Error('Not Implemented
+, next) %7B%0A  if (!req.dntConnect)  %7B%0A    var err = new Error('DNT Connect: unknown error');%0A    err.status = 500;%0A    next(err);%0A  %7D%0A%0A  if (req.dntConnect.err %7C%7C !req.dntConnect.data.er_autentisert) %7B%0A    // @TODO redierect back to login page with error message%0A    return res.redirect('/login');%0A  %7D%0A%0A  req.session.user = %7B%0A    id: 'sherpa3:' + req.dntConnect.data.sherpa_id,%0A    navn: req.dntConnect.data.fornavn + ' ' + req.dntConnect.data.etternavn,%0A    epost: req.dntConnect.data.epost,%0A    grupper: %5B%5D // @TODO get the groups%0A  %7D;%0A%0A  res.redirect('/hytter
 ');%0A

Implement /auth/login/dnt route logic