initial better auth config
This commit is contained in:
@@ -3,6 +3,7 @@ import { httpAction } from "./_generated/server";
|
||||
import { internal } from "./_generated/api";
|
||||
import { getClientIP } from "./utils/getClientIP";
|
||||
import { v } from "convex/values";
|
||||
import { authComponent, createAuth } from "./auth";
|
||||
|
||||
const http = httpRouter();
|
||||
|
||||
@@ -18,9 +19,9 @@ http.route({
|
||||
request.headers.forEach((value, key) => {
|
||||
headers[key] = value;
|
||||
});
|
||||
|
||||
|
||||
const ip = getClientIP(request);
|
||||
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
headers,
|
||||
@@ -53,11 +54,11 @@ http.route({
|
||||
});
|
||||
console.log("Headers:", headersEntries.join(", "));
|
||||
console.log("Request URL:", request.url);
|
||||
|
||||
|
||||
// Extrair IP do cliente do request
|
||||
let clientIP = getClientIP(request);
|
||||
console.log("IP extraído:", clientIP);
|
||||
|
||||
|
||||
// Se não encontrou IP, tentar obter do URL ou usar valor padrão
|
||||
if (!clientIP) {
|
||||
try {
|
||||
@@ -77,13 +78,13 @@ http.route({
|
||||
console.warn("Erro ao processar URL para IP");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Extrair User-Agent
|
||||
const userAgent = request.headers.get("user-agent") || undefined;
|
||||
|
||||
|
||||
// Ler body da requisição
|
||||
const body = await request.json();
|
||||
|
||||
|
||||
if (!body.matriculaOuEmail || !body.senha) {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
@@ -96,15 +97,18 @@ http.route({
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// Chamar a mutation de login interna com IP e userAgent
|
||||
const resultado = await ctx.runMutation(internal.autenticacao.loginComIP, {
|
||||
matriculaOuEmail: body.matriculaOuEmail,
|
||||
senha: body.senha,
|
||||
ipAddress: clientIP,
|
||||
userAgent: userAgent,
|
||||
});
|
||||
|
||||
const resultado = await ctx.runMutation(
|
||||
internal.autenticacao.loginComIP,
|
||||
{
|
||||
matriculaOuEmail: body.matriculaOuEmail,
|
||||
senha: body.senha,
|
||||
ipAddress: clientIP,
|
||||
userAgent: userAgent,
|
||||
}
|
||||
);
|
||||
|
||||
return new Response(JSON.stringify(resultado), {
|
||||
status: 200,
|
||||
headers: {
|
||||
@@ -118,7 +122,8 @@ http.route({
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
sucesso: false,
|
||||
erro: error instanceof Error ? error.message : "Erro ao processar login",
|
||||
erro:
|
||||
error instanceof Error ? error.message : "Erro ao processar login",
|
||||
}),
|
||||
{
|
||||
status: 500,
|
||||
@@ -147,4 +152,6 @@ http.route({
|
||||
}),
|
||||
});
|
||||
|
||||
authComponent.registerRoutes(http, createAuth);
|
||||
|
||||
export default http;
|
||||
|
||||
Reference in New Issue
Block a user