import { NextApiRequest } from "next"

async function getTokenFromBody<T>(payload: T & { req: NextApiRequest }) {
  const { req } = payload
  const token: string = req.body.token

  // Checking for cookie presens, because it is necessary
  if (!token) {
    return Promise.reject({
      message: "Unauthorized. No Access-Token.",
      statusCode: 401,
      solved: true,
    })
  }

  return { ...payload, token }
}

export default getTokenFromBody
