first commit
This commit is contained in:
3
server/routes/auth/login.post.ts
Normal file
3
server/routes/auth/login.post.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import loginHandler from '../../api/auth/login.post'
|
||||
|
||||
export default loginHandler
|
||||
3
server/routes/auth/refresh.post.ts
Normal file
3
server/routes/auth/refresh.post.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import refreshHandler from '../../api/auth/refresh.post'
|
||||
|
||||
export default refreshHandler
|
||||
30
server/routes/dashboard.get.ts
Normal file
30
server/routes/dashboard.get.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { createError, getRequestHeader } from 'h3'
|
||||
|
||||
import { requireAuthContext } from '../utils/require-auth'
|
||||
|
||||
/**
|
||||
* Exemplo de orquestração A -> B.
|
||||
* Reaproveita o mesmo Authorization para chamar `/profile/me`.
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = requireAuthContext(event)
|
||||
|
||||
const authorization = getRequestHeader(event, 'authorization')
|
||||
|
||||
if (!authorization) {
|
||||
throw createError({ statusCode: 401, statusMessage: 'Missing Authorization header' })
|
||||
}
|
||||
|
||||
const profileFromService = await $fetch('/profile/me', {
|
||||
headers: {
|
||||
Authorization: authorization
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
sub_from_api_a: auth.id,
|
||||
sub_from_api_b: profileFromService.id,
|
||||
same_subject: profileFromService.id === auth.id,
|
||||
profile: profileFromService
|
||||
}
|
||||
})
|
||||
32
server/routes/profile/me.get.ts
Normal file
32
server/routes/profile/me.get.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { createError } from 'h3'
|
||||
|
||||
import { prisma } from '../../utils/prisma'
|
||||
import { requireAuthContext } from '../../utils/require-auth'
|
||||
|
||||
/**
|
||||
* Retorna os dados do usuário autenticado com base no `sub` do JWT.
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const auth = requireAuthContext(event)
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: auth.id },
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
createdAt: true,
|
||||
updatedAt: true
|
||||
}
|
||||
})
|
||||
|
||||
if (!user) {
|
||||
throw createError({ statusCode: 404, statusMessage: 'Authenticated user not found' })
|
||||
}
|
||||
|
||||
return {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
created_at: user.createdAt.toISOString(),
|
||||
updated_at: user.updatedAt.toISOString()
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user