import { Token } from "@prisma/client"
import { NextApiRequest } from "next"

async function getTokenFromCookie<T>(payload: T & { req: NextApiRequest }) {
  const { req } = payload
  const token = req.cookies.token

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

  return { ...payload, token, tokenType: "REFRESH" as Token["type"] }
}

export default getTokenFromCookie
