import fetch from 'node-fetch';

import { userModel } from '../models/index';

export const authenticatedWithToken = async (req, res, next) => {
  if (req && req.cookies && req.cookies.jwt && req.headers && req.headers.csrf) {
    let csrf = req.headers.csrf;
    let jwt = req.cookies.jwt;

    const opts = {
      headers: {
        csrf,
        jwt
      }
    };

    let res = await fetch('http://localhost:3002/signcheck', opts);

    if (res.status === 401) {
      return null;
    }

    let resJson = await res.json();

    if (!resJson) {
      next();
    } else {
      try {
        let user = await userModel.findOne({name: resJson.name}).exec();

        if (user) {
          req.user = user;
          next();
        }
      } catch (error) {
        console.log(error)
      }
    }
  } else {
    req.err = 'Either the cookie or the token is missing';

    next();
  }
};
import fetch from 'node-fetch';

import { userModel } from '../models/index';

export const authenticatedWithToken = (req, res, next) => {
  let csrf;
  let jwt;

  if (req && req.cookies && req.headers){
    csrf = req.headers.csrf;
    jwt = req.cookies.jwt;

    const opts = {
      headers: {
        csrf,
        jwt
      }
    };

    fetch('http://localhost:3002/signcheck', opts)
      .then(res => {
        if (res.status === 401) {
          return null;
        }

        return res.json();
      })
      .then(user => {
        if (!user) {
          return next();
        } else {
          return userModel.findOne({name: user.name})
            .then(user => {
              req.user = user;
              
              return next();
          });
        }
      })
  }
};
--- 
+++ 
@@ -2,10 +2,13 @@
 
 import { userModel } from '../models/index';
 
-export const authenticatedWithToken = async (req, res, next) => {
-  if (req && req.cookies && req.cookies.jwt && req.headers && req.headers.csrf) {
-    let csrf = req.headers.csrf;
-    let jwt = req.cookies.jwt;
+export const authenticatedWithToken = (req, res, next) => {
+  let csrf;
+  let jwt;
+
+  if (req && req.cookies && req.headers){
+    csrf = req.headers.csrf;
+    jwt = req.cookies.jwt;
 
     const opts = {
       headers: {
@@ -14,31 +17,25 @@
       }
     };
 
-    let res = await fetch('http://localhost:3002/signcheck', opts);
+    fetch('http://localhost:3002/signcheck', opts)
+      .then(res => {
+        if (res.status === 401) {
+          return null;
+        }
 
-    if (res.status === 401) {
-      return null;
-    }
-
-    let resJson = await res.json();
-
-    if (!resJson) {
-      next();
-    } else {
-      try {
-        let user = await userModel.findOne({name: resJson.name}).exec();
-
-        if (user) {
-          req.user = user;
-          next();
+        return res.json();
+      })
+      .then(user => {
+        if (!user) {
+          return next();
+        } else {
+          return userModel.findOne({name: user.name})
+            .then(user => {
+              req.user = user;
+              
+              return next();
+          });
         }
-      } catch (error) {
-        console.log(error)
-      }
-    }
-  } else {
-    req.err = 'Either the cookie or the token is missing';
-
-    next();
+      })
   }
 };
Replace promise with async await and handle error cases
