feat: add user authentication UI to header component

Add login and register dialogs, user dropdown menu with profile link and logout option, and integrate with session management hook. Prevent hydration mismatches by conditionally rendering auth elements after mount. Update package-lock.json with new @esbuild dependencies for build tooling.
This commit is contained in:
Evandro Fonseca Junior committed 2025-11-04 02:33:29 -03:00
1 parent e641138887
commit 55eebde0ba
55 files changed
+7353 -7

No files matched your search

+205
View File
@@ -0,0 +1,205 @@
import { NextRequest, NextResponse } from "next/server";
import { secureLogger } from "@/lib/secure-logger";
import { requireAuth } from "@/lib/auth/middleware";
import {
updateCharacter,
deleteCharacter,
getCharacterById,
} from "@/lib/db/queries/characters";
import { characterSchema } from "@/lib/validation-schemas";
export async function PUT(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
// Middleware de autenticação
const authResult = await requireAuth(request);
if (authResult instanceof NextResponse) {
return authResult;
}
const auth = authResult;
const { id } = await params;
const characterId = parseInt(id, 10);
if (isNaN(characterId)) {
return NextResponse.json(
{
success: false,
error: "ID de personagem inválido",
},
{ status: 400 }
);
}
const body = await request.json();
// Validação com Zod
const validationResult = characterSchema.safeParse(body);
if (!validationResult.success) {
const errors = validationResult.error.issues.map((err) => ({
field: err.path.join("."),
message: err.message,
}));
return NextResponse.json(
{
success: false,
error: "Dados de entrada inválidos",
details: errors,
},
{ status: 400 }
);
}
const validatedData = validationResult.data;
// Atualizar personagem (verifica ownership internamente)
const updatedCharacter = await updateCharacter(
characterId,
auth.userId,
{
character_name: validatedData.characterName,
server_name: validatedData.serverName,
is_main: validatedData.isMain,
}
);
secureLogger.info("✅ Personagem atualizado com sucesso", {
userId: auth.userId,
characterId: updatedCharacter.character_id,
timestamp: new Date().toISOString(),
});
return NextResponse.json(
{
success: true,
character: {
characterId: updatedCharacter.character_id,
characterName: updatedCharacter.character_name,
serverName: updatedCharacter.server_name,
isMain: updatedCharacter.is_main,
verified: updatedCharacter.verified,
createdAt: updatedCharacter.created_at,
},
},
{ status: 200 }
);
} catch (error) {
// Se for NextResponse (erro de autenticação), re-lançar
if (error instanceof NextResponse) {
return error;
}
// Verificar se é erro de permissão
if (
error instanceof Error &&
error.message.includes("não tem permissão")
) {
return NextResponse.json(
{
success: false,
error: error.message,
},
{ status: 403 }
);
}
secureLogger.error("❌ Erro ao atualizar personagem", {
error: error instanceof Error ? error.message : "Erro desconhecido",
stack: error instanceof Error ? error.stack : undefined,
timestamp: new Date().toISOString(),
});
return NextResponse.json(
{
success: false,
error: error instanceof Error ? error.message : "Erro interno",
},
{ status: 500 }
);
}
}
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
// Middleware de autenticação
const authResult = await requireAuth(request);
if (authResult instanceof NextResponse) {
return authResult;
}
const auth = authResult;
const { id } = await params;
const characterId = parseInt(id, 10);
if (isNaN(characterId)) {
return NextResponse.json(
{
success: false,
error: "ID de personagem inválido",
},
{ status: 400 }
);
}
// Remover personagem (verifica ownership internamente)
await deleteCharacter(characterId, auth.userId);
secureLogger.info("✅ Personagem removido com sucesso", {
userId: auth.userId,
characterId,
timestamp: new Date().toISOString(),
});
return NextResponse.json(
{
success: true,
},
{ status: 200 }
);
} catch (error) {
// Se for NextResponse (erro de autenticação), re-lançar
if (error instanceof NextResponse) {
return error;
}
// Verificar se é erro de permissão
if (
error instanceof Error &&
error.message.includes("não tem permissão")
) {
return NextResponse.json(
{
success: false,
error: error.message,
},
{ status: 403 }
);
}
secureLogger.error("❌ Erro ao remover personagem", {
error: error instanceof Error ? error.message : "Erro desconhecido",
stack: error instanceof Error ? error.stack : undefined,
timestamp: new Date().toISOString(),
});
return NextResponse.json(
{
success: false,
error: error instanceof Error ? error.message : "Erro interno",
},
{ status: 500 }
);
}
}