drivers/src/autenticacao/_validarToken.ts
2024-08-22 16:36:22 -03:00

35 lines
967 B
TypeScript

import type { tipoResposta } from "p-respostas"
import { urlAutenticacao } from "./_urlAutenticacao"
type tipoPostValidarTokem = { token: string }
import node_fetch from "cross-fetch"
import type { z } from "zod"
import type { zAmbiente } from "../ts/ambiente"
/** faz a validação do token */
export const validarToken = async ({
ambiente,
post,
}: {
ambiente: z.infer<typeof zAmbiente>
post: tipoPostValidarTokem
}): Promise<"valido" | "erro"> => {
const url = `${urlAutenticacao(ambiente)}/api/validar_token`
try {
const resposta = await node_fetch(url, {
method: "POST",
body: JSON.stringify(post),
headers: { "Content-Type": "application/json" },
})
.then((r) => r.json())
.then((r) => r as tipoResposta<any>)
.then((resposta) =>
resposta.eCerto ? ("valido" as const) : ("erro" as const),
)
.catch(() => "erro" as const)
return resposta
} catch (_e) {
return "erro"
}
}