fix(profile): corrige persistencia da remocao de avatar
O Better Auth usa cookie cache de 5 minutos para a sessao. Quando o avatar era removido via Prisma, o cache ainda tinha o avatar antigo. Agora usamos auth.api.updateUser para atualizar o usuario e invalidar o cache da sessao. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
92954b45c7
commit
a6af4aa580
1 changed files with 28 additions and 8 deletions
|
|
@ -5,8 +5,10 @@
|
|||
*/
|
||||
|
||||
import { NextRequest, NextResponse } from "next/server"
|
||||
import { headers } from "next/headers"
|
||||
|
||||
import { getServerSession } from "@/lib/auth-server"
|
||||
import { auth } from "@/lib/auth"
|
||||
import { prisma } from "@/lib/prisma"
|
||||
import { DEFAULT_TENANT_ID } from "@/lib/constants"
|
||||
import { createConvexClient } from "@/server/convex-client"
|
||||
|
|
@ -73,12 +75,21 @@ export async function POST(request: NextRequest) {
|
|||
return NextResponse.json({ error: "Erro ao obter URL do avatar" }, { status: 500 })
|
||||
}
|
||||
|
||||
// Atualiza o usuário no banco
|
||||
await prisma.authUser.update({
|
||||
where: { id: session.user.id },
|
||||
data: { avatarUrl },
|
||||
// Usa updateUser do Better Auth para invalidar o cache da sessao
|
||||
const headerList = await headers()
|
||||
const updateResult = await auth.api.updateUser({
|
||||
headers: headerList,
|
||||
body: { avatarUrl },
|
||||
})
|
||||
|
||||
if (!updateResult) {
|
||||
// Fallback: atualiza diretamente no Prisma
|
||||
await prisma.authUser.update({
|
||||
where: { id: session.user.id },
|
||||
data: { avatarUrl },
|
||||
})
|
||||
}
|
||||
|
||||
// Sincroniza com o Convex
|
||||
try {
|
||||
const tenantId = session.user.tenantId ?? DEFAULT_TENANT_ID
|
||||
|
|
@ -109,12 +120,21 @@ export async function DELETE() {
|
|||
return NextResponse.json({ error: "Não autorizado" }, { status: 401 })
|
||||
}
|
||||
|
||||
// Remove a imagem do usuário (volta ao padrão)
|
||||
await prisma.authUser.update({
|
||||
where: { id: session.user.id },
|
||||
data: { avatarUrl: null },
|
||||
// Usa updateUser do Better Auth para invalidar o cache da sessao
|
||||
const headerList = await headers()
|
||||
const updateResult = await auth.api.updateUser({
|
||||
headers: headerList,
|
||||
body: { avatarUrl: null },
|
||||
})
|
||||
|
||||
if (!updateResult) {
|
||||
// Fallback: atualiza diretamente no Prisma
|
||||
await prisma.authUser.update({
|
||||
where: { id: session.user.id },
|
||||
data: { avatarUrl: null },
|
||||
})
|
||||
}
|
||||
|
||||
// Sincroniza com o Convex
|
||||
try {
|
||||
const convex = createConvexClient()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue