leaky-ships/leaky-ships/lib/backend/components/getTokenFromBody.ts
2023-04-08 16:56:03 +02:00

28 lines
720 B
TypeScript

import { TokenType } from "@prisma/client"
import { NextApiRequest, NextApiResponse } from "next"
import sendError from "./sendError"
async function getTokenFromBody(
req: NextApiRequest,
res: NextApiResponse,
next: (refreshToken: [string, TokenType]) => void
) {
const body = JSON.parse(req.body)
// Checking for cookie presens, because it is necessary
if (
typeof body !== "object" ||
!body ||
!("token" in body) ||
typeof body.token !== "string"
)
return sendError(req, res, {
message: "Unauthorized. No Access-Token.",
statusCode: 401,
solved: true,
})
const tokenValue = body.token
return next([tokenValue, "ACCESS"])
}
export default getTokenFromBody