function ensureAuthenticated(req, res, next) {
  let err;
  console.log('received authentication request')
  if (!req.headers.authorization) {
    err = new Error();
    err.status = 401;
    err.message = 'Please make sure your request has an Authorization header';
    return next(err);
  }
  if (!/^Bearer .*$/.test(req.headers.authorization)) {
    err = new Error();
    err.status = 401;
    err.message = 'Authorization header does not match expected format';
    return next(err);
  }
  const token = req.headers.authorization.split(' ')[1];
  const payload = jwt.decode(token, secrets.TOKEN_SECRET);
  if (payload.exp <= moment().unix()) {
    err = new Error();
    err.status = 401;
    err.message = 'Token has expired';
    return next(err);
  }
  req.user = payload.sub;
  req.scopes = payload.scopes;
  next();
}
/**
 * Express middleware function. Ensures that the user is autheticated. This
 * method reads the authorization header where a JWT is expected to be found.
 * If no valid JWT is found a NOT_AUTHORIZED (401) response is returned.
 *
 * @param {object} req - The request object
 * @param {object} res - The response object
 * @param {function} next - Middleware function to continue the call chain
 */
Express middleware function. Ensures that the user is autheticated. This
method reads the authorization header where a JWT is expected to be found.
If no valid JWT is found a NOT_AUTHORIZED (401) response is returned.

@param {object} req - The request object
@param {object} res - The response object
@param {function} next - Middleware function to continue the call chain
Express middleware function . Ensures that the user is autheticated . This method reads the authorization header where a JWT is expected to be found . If no valid JWT is found a NOT_AUTHORIZED ( 401 ) response is returned . @param { object } req - The request object @param { object } res - The response object @param { function } next - Middleware function to continue the call chain
function ensureAuthenticated(req, res, next) {
  let err;
  console.log('received authentication request')
  if (!req.headers.authorization) {
    err = new Error();
    err.status = 401;
    err.message = 'Please make sure your request has an Authorization header';
    return next(err);
  }
  if (!/^Bearer .*$/.test(req.headers.authorization)) {
    err = new Error();
    err.status = 401;
    err.message = 'Authorization header does not match expected format';
    return next(err);
  }
  const token = req.headers.authorization.split(' ')[1];
  const payload = jwt.decode(token, secrets.TOKEN_SECRET);
  if (payload.exp <= moment().unix()) {
    err = new Error();
    err.status = 401;
    err.message = 'Token has expired';
    return next(err);
  }
  req.user = payload.sub;
  req.scopes = payload.scopes;
  next();
}
function ensureAuthenticated ( req , res , next ) { let err ; console . log ( 'received authentication request' ) if ( ! req . headers . authorization ) { err = new Error ( ) ; err . status = 401 ; err . message = 'Please make sure your request has an Authorization header' ; return next ( err ) ; } if ( ! / ^Bearer .*$ / . test ( req . headers . authorization ) ) { err = new Error ( ) ; err . status = 401 ; err . message = 'Authorization header does not match expected format' ; return next ( err ) ; } const token = req . headers . authorization . split ( ' ' ) [ 1 ] ; const payload = jwt . decode ( token , secrets . TOKEN_SECRET ) ; if ( payload . exp <= moment ( ) . unix ( ) ) { err = new Error ( ) ; err . status = 401 ; err . message = 'Token has expired' ; return next ( err ) ; } req . user = payload . sub ; req . scopes = payload . scopes ; next ( ) ; }
Express middleware function.
Express middleware function .