const config = require('../../config')
const authorisedRequest = require('../lib/authorised-request')

module.exports = (req, res, next) => {
  const token = req.session.token
  const user = req.session.user

  if (token && !user) {
    authorisedRequest(token, `${config.apiRoot}/whoami/`)
      .then((userInfo) => {
        req.session.user = {
          id: userInfo.id, // DIT Adviser id
          name: userInfo.name,
          team: userInfo.dit_team,
        }

        res.locals.user = req.session.user
        next()
      })
      .catch(next)
  } else {
    if (user) {
      res.locals.user = user
    }
    next()
  }
}

@@ -212,18 +212,8 @@
 if (
-token && !
 user
@@ -218,16 +218,103 @@
 er) %7B%0A  
+  res.locals.user = user%0A    return next()%0A  %7D%0A%0A  if (!token) %7B%0A    return next()%0A  %7D%0A%0A
   author
@@ -365,18 +365,16 @@
 /%60)%0A    
-  
 .then((u
@@ -376,20 +376,16 @@
 en((user
-Info
 ) =%3E %7B%0A 
@@ -389,18 +389,16 @@
 %7B%0A      
- 
- 
 req.sess
@@ -411,140 +411,8 @@
 er =
- %7B%0A          id: userInfo.id, // DIT Adviser id%0A          name: userInfo.name,%0A          team: userInfo.dit_team,%0A        %7D%0A%0A       
  res
@@ -430,27 +430,13 @@
 r = 
-req.session.
 user%0A
-  
     
@@ -452,15 +452,11 @@
     
-  
 %7D)%0A
-  
     
@@ -472,83 +472,6 @@
 xt)%0A
-  %7D else %7B%0A    if (user) %7B%0A      res.locals.user = user%0A    %7D%0A    next()%0A  %7D%0A
 %7D%0A

Refactor user middleware