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();
          });
        }
      })
  }
};
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();
  }
};
Replace promise with async await and handle error cases
Replace promise with async await and handle error cases
