var express = require('express');
var passport = require('passport');
var router = express.Router();
var querystring = require('querystring');

var tenant = require('../lib/tenant');

var env = {
  AUTH0_CLIENT_ID: process.env.AUTH0_CLIENT_ID,
  AUTH0_DOMAIN: process.env.AUTH0_DOMAIN,
  AUTH0_CALLBACK_URL: process.env.AUTH0_CALLBACK_URL || 'http://localhost:3000/callback'
};

/* GET home page. */
router.get('/', 
  function(req, res, next) {
    if (req.user) {
      res.redirect('/user');
    }

    res.render('index', { title: 'Multi-tenant App!', env: env });
  });

router.get('/login',
  tenant.setCurrent(),
  function(req, res) {
    // use session to pass login tenant (if one exists) through login flow
    req.session.loginTenant = req.tenant;

    var query = querystring.stringify({
      client_id: process.env.AUTH0_CLIENT_ID,
      response_type: 'code',
      scopes: 'openid profile bar',
      redirect_uri: process.env.AUTH0_CALLBACK_URL
    });

    res.redirect(`https://${process.env.AUTH0_DOMAIN}/authorize?${query}`);
  });

router.get('/logout', 
  function(req, res){
    req.logout();
    res.redirect('/');
  });

router.get('/callback',
  passport.authenticate('auth0', { failWithError: true }),
  tenant.setCurrent(req => req.session.loginTenant),
  tenant.ensureCurrent(),
  function(req, res) {
    var path = req.session.returnTo || '/user';
    var url = `http://${req.tenant}.${process.env.ROOT_DOMAIN}:${process.env.PORT}${path}`;

    // clear session values used to complete login flow
    delete req.session.loginTenant;
    delete req.session.returnTo;

    res.redirect(url);
  },
  function(err, req, res, next) {
    if (err.constructor.name === 'AuthenticationError') {
      err.description = req.query.error_description;
      return res.json(err);
    }

    next(err);
  });

module.exports = router;

@@ -181,203 +181,8 @@
 );%0A%0A
-var env = %7B%0A  AUTH0_CLIENT_ID: process.env.AUTH0_CLIENT_ID,%0A  AUTH0_DOMAIN: process.env.AUTH0_DOMAIN,%0A  AUTH0_CALLBACK_URL: process.env.AUTH0_CALLBACK_URL %7C%7C 'http://localhost:3000/callback'%0A%7D;%0A%0A
 /* G
@@ -356,18 +356,8 @@
 pp!'
-, env: env
  %7D);
@@ -720,16 +720,26 @@
 ct_uri: 
+%60http://$%7B
 process.
@@ -746,26 +746,50 @@
 env.
-AUTH0_CALLBACK_URL
+ROOT_DOMAIN%7D:$%7Bprocess.env.PORT%7D/callback%60
 %0A   

Remove unused 'env' variable