This commit is contained in:
Your Name committed 2026-06-12 23:33:47 -04:00
1 parent 612eb4921b
commit ee1c7820c3
1 file changed
+6 -5
+6 -5
View File
@@ -4,6 +4,11 @@ import { lineConfig, lineClient } from '../lib/lineClient';
import crypto from 'crypto'; import crypto from 'crypto';
export const lineRoutes = new Elysia({ prefix: '/line' }) export const lineRoutes = new Elysia({ prefix: '/line' })
.onParse(async ({ request, contentType }) => {
if (contentType?.includes('application/json')) {
return await request.text();
}
})
.post('/webhook', async ({ request, body, set }) => { .post('/webhook', async ({ request, body, set }) => {
// 1. Verify Signature // 1. Verify Signature
const signature = request.headers.get('x-line-signature'); const signature = request.headers.get('x-line-signature');
@@ -12,9 +17,7 @@ export const lineRoutes = new Elysia({ prefix: '/line' })
return { error: 'Missing Signature' }; return { error: 'Missing Signature' };
} }
// In Elysia, application/json is automatically parsed into an object. // In our onParse hook, we intercepted the body and returned it as a raw text string.
// We stringify it back to a string for signature validation.
// Note: This works if the original payload didn't have special formatting spaces.
const rawBody = typeof body === 'string' ? body : JSON.stringify(body); const rawBody = typeof body === 'string' ? body : JSON.stringify(body);
if (!rawBody) { if (!rawBody) {
@@ -62,6 +65,4 @@ export const lineRoutes = new Elysia({ prefix: '/line' })
set.status = 500; set.status = 500;
return { error: 'Webhook processing failed' }; return { error: 'Webhook processing failed' };
} }
}, {
type: 'text'
}); });