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.
93 lines
2.6 KiB
TypeScript
93 lines
2.6 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { secureLogger } from "@/lib/secure-logger";
|
|
import { requireAuth } from "@/lib/auth/middleware";
|
|
import { getUserById } from "@/lib/db/queries/users";
|
|
import { getUserCharacters } from "@/lib/db/queries/characters";
|
|
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
// Middleware de autenticação
|
|
const authResult = await requireAuth(request);
|
|
|
|
// Se retornou NextResponse, é um erro de autenticação
|
|
if (authResult instanceof NextResponse) {
|
|
return authResult;
|
|
}
|
|
|
|
const auth = authResult;
|
|
|
|
// Buscar dados do usuário
|
|
const user = await getUserById(auth.userId);
|
|
|
|
if (!user) {
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
error: "Usuário não encontrado",
|
|
},
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
// Buscar personagens do usuário
|
|
const characters = await getUserCharacters(auth.userId);
|
|
|
|
secureLogger.info("✅ Perfil do usuário recuperado", {
|
|
userId: auth.userId,
|
|
timestamp: new Date().toISOString(),
|
|
});
|
|
|
|
return NextResponse.json(
|
|
{
|
|
success: true,
|
|
user: {
|
|
userId: user.user_id,
|
|
username: user.username,
|
|
email: user.email,
|
|
firstName: user.first_name,
|
|
lastName: user.last_name,
|
|
phone: user.phone,
|
|
cpf: user.cpf,
|
|
accountLevel: user.account_level,
|
|
hasDonated: user.has_donated,
|
|
whatsappNotifications: user.whatsapp_notifications,
|
|
createdAt: user.created_at,
|
|
updatedAt: user.updated_at,
|
|
lastLogin: user.last_login,
|
|
isActive: user.is_active,
|
|
isVerified: user.is_verified,
|
|
},
|
|
characters: characters.map((char) => ({
|
|
characterId: char.character_id,
|
|
characterName: char.character_name,
|
|
serverName: char.server_name,
|
|
isMain: char.is_main,
|
|
verified: char.verified,
|
|
createdAt: char.created_at,
|
|
})),
|
|
},
|
|
{ status: 200 }
|
|
);
|
|
} catch (error) {
|
|
// Se for NextResponse (erro de autenticação), re-lançar
|
|
if (error instanceof NextResponse) {
|
|
return error;
|
|
}
|
|
|
|
secureLogger.error("❌ Erro ao buscar perfil do usuário", {
|
|
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 }
|
|
);
|
|
}
|
|
}
|
|
|