tambahannnya
This commit is contained in:
108
xx.ts
108
xx.ts
@@ -1,65 +1,45 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { Elysia } from 'elysia'
|
||||
import jwt, { type JWTPayloadSpec } from '@elysiajs/jwt'
|
||||
import bearer from '@elysiajs/bearer'
|
||||
import { prisma } from '../lib/prisma'
|
||||
|
||||
// =========================================================
|
||||
// JWT Secret Validation
|
||||
// =========================================================
|
||||
const secret = process.env.JWT_SECRET
|
||||
if (!secret) throw new Error('JWT_SECRET environment variable is missing')
|
||||
|
||||
// =========================================================
|
||||
// Auth Middleware Plugin
|
||||
// =========================================================
|
||||
export default function apiAuth(app: Elysia) {
|
||||
if (!secret) throw new Error('JWT_SECRET environment variable is missing')
|
||||
return app
|
||||
// Register Bearer and JWT plugins
|
||||
.use(bearer()) // ✅ Extracts Bearer token automatically (case-insensitive)
|
||||
.use(
|
||||
jwt({
|
||||
name: 'jwt',
|
||||
secret,
|
||||
})
|
||||
)
|
||||
|
||||
// Derive user from JWT or cookie
|
||||
.derive(async ({ bearer, cookie, jwt }) => {
|
||||
// Normalize token type to string or undefined
|
||||
const token =
|
||||
(typeof bearer === 'string' ? bearer : undefined) ??
|
||||
(typeof cookie?.token?.value === 'string' ? cookie.token.value : undefined)
|
||||
|
||||
let user: Awaited<ReturnType<typeof prisma.user.findUnique>> | null = null
|
||||
|
||||
if (token) {
|
||||
try {
|
||||
const decoded = (await jwt.verify(token)) as JWTPayloadSpec
|
||||
|
||||
if (decoded?.sub && typeof decoded.sub === 'string') {
|
||||
user = await prisma.user.findUnique({
|
||||
where: { id: decoded.sub },
|
||||
})
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('[SERVER][apiAuth] Invalid token:', (err as Error).message)
|
||||
}
|
||||
}
|
||||
|
||||
return { user }
|
||||
})
|
||||
|
||||
// Protect all routes by default
|
||||
.onBeforeHandle(({ user, set, request }) => {
|
||||
// Whitelist public routes if needed
|
||||
const publicPaths = ['/auth/login', '/auth/register', '/public']
|
||||
if (publicPaths.some((path) => request.url.includes(path))) return
|
||||
|
||||
if (!user) {
|
||||
set.status = 401
|
||||
return { error: 'Unauthorized' }
|
||||
}
|
||||
})
|
||||
{
|
||||
"response": [
|
||||
{
|
||||
"type": "json",
|
||||
"data": {
|
||||
"success": true,
|
||||
"status": 200,
|
||||
"method": "GET",
|
||||
"path": "/api/pengaduan/category",
|
||||
"data": {
|
||||
"data": [
|
||||
{
|
||||
"id": "infrastruktur",
|
||||
"name": "Infrastruktur"
|
||||
},
|
||||
{
|
||||
"id": "cmhslcvcy0000mg0810l7zx8x",
|
||||
"name": "keamanan"
|
||||
},
|
||||
{
|
||||
"id": "keamanan",
|
||||
"name": "Keamanan"
|
||||
},
|
||||
{
|
||||
"id": "kebersihan",
|
||||
"name": "Kebersihan"
|
||||
},
|
||||
{
|
||||
"id": "lainnya",
|
||||
"name": "Lainnya"
|
||||
},
|
||||
{
|
||||
"id": "pelayanan",
|
||||
"name": "Pelayanan"
|
||||
},
|
||||
{
|
||||
"id": "cmhsl5ijj0000mg08pru6kom4",
|
||||
"name": "sampah"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user