feat: integra microsserive de reviews
This commit is contained in:
29
server/api/profile/[id].get.ts
Normal file
29
server/api/profile/[id].get.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { prisma } from '../../utils/prisma'
|
||||
import { requireAuthContext } from '../../utils/require-auth'
|
||||
|
||||
/**
|
||||
* Retorna o nome de exibição de um usuário pelo ID (sub do JWT).
|
||||
* Usado para resolver nomes de autores de reviews.
|
||||
* Responde com { displayName: string | null } — null quando o ID
|
||||
* pertence a outro sistema (usuário externo).
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
requireAuthContext(event)
|
||||
|
||||
const id = getRouterParam(event, 'id')
|
||||
|
||||
if (!id) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'ID obrigatório' })
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id },
|
||||
select: { email: true }
|
||||
})
|
||||
|
||||
if (!user) {
|
||||
return { displayName: null }
|
||||
}
|
||||
|
||||
return { displayName: user.email.split('@')[0] }
|
||||
})
|
||||
@@ -14,4 +14,6 @@ export interface AccessTokenClaims {
|
||||
export interface AuthRouteRequirement {
|
||||
method: string
|
||||
path: string
|
||||
/** Quando true, protege qualquer rota cujo caminho comece com `path` */
|
||||
prefix?: boolean
|
||||
}
|
||||
|
||||
@@ -8,6 +8,11 @@ const PROTECTED_ROUTES: AuthRouteRequirement[] = [
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/dashboard'
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
path: '/api/profile/',
|
||||
prefix: true
|
||||
}
|
||||
]
|
||||
|
||||
@@ -37,8 +42,10 @@ export function getRouteRequirement(method: string, path: string): AuthRouteRequ
|
||||
const normalizedPath = normalizePath(path)
|
||||
|
||||
return (
|
||||
PROTECTED_ROUTES.find(
|
||||
(route) => route.method === normalizedMethod && route.path === normalizedPath
|
||||
) ?? null
|
||||
PROTECTED_ROUTES.find((route) => {
|
||||
if (route.method !== normalizedMethod) return false
|
||||
if (route.prefix) return normalizedPath.startsWith(route.path)
|
||||
return route.path === normalizedPath
|
||||
}) ?? null
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user