diff options
| author | Pinapelz <yukais@pinapelz.com> | 2025-10-03 13:18:47 -0700 |
|---|---|---|
| committer | Pinapelz <yukais@pinapelz.com> | 2025-10-03 13:18:47 -0700 |
| commit | 4da231aeb5bf3ee0296fb8626dae3ac99893a158 (patch) | |
| tree | e952d79fe1c6f2ef6b7f65ac43b409ec4a7474b9 /middleware | |
| parent | 78aa1512446072fdec94c86360c9aee3ea932db1 (diff) | |
implement api call to save topic for given fcm token
Diffstat (limited to 'middleware')
| -rw-r--r-- | middleware/src/app/api/notifications/get/route.ts | 2 | ||||
| -rw-r--r-- | middleware/src/app/api/notifications/set/route.ts | 30 |
2 files changed, 27 insertions, 5 deletions
diff --git a/middleware/src/app/api/notifications/get/route.ts b/middleware/src/app/api/notifications/get/route.ts index 344e41b..6150822 100644 --- a/middleware/src/app/api/notifications/get/route.ts +++ b/middleware/src/app/api/notifications/get/route.ts @@ -7,7 +7,7 @@ const redis = new Redis({ token: process.env.KV_REST_API_TOKEN!, }); -// /api/fcm/list?topic=<topic> +// /api/notification/get?topic=<topic> export async function GET(req: Request) { const { searchParams } = new URL(req.url); const topic = searchParams.get("topic"); diff --git a/middleware/src/app/api/notifications/set/route.ts b/middleware/src/app/api/notifications/set/route.ts index 3a584fb..c87159f 100644 --- a/middleware/src/app/api/notifications/set/route.ts +++ b/middleware/src/app/api/notifications/set/route.ts @@ -6,7 +6,19 @@ const redis = new Redis({ token: process.env.KV_REST_API_TOKEN!, }); -// /api/fcm?topic=<topic>&token=<fcm_token>&action=subscribe|unsubscribe +const corsHeaders = { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Methods": "GET, POST, OPTIONS", + "Access-Control-Allow-Headers": "Content-Type, Authorization", +}; +export async function OPTIONS() { + return new Response(null, { + status: 204, + headers: corsHeaders, + }); +} + +// /api/notification/set?topic=<topic>&token=<fcm_token>&action=subscribe|unsubscribe export async function GET(req: Request) { const { searchParams } = new URL(req.url); const topic = searchParams.get("topic"); @@ -14,8 +26,12 @@ export async function GET(req: Request) { const action = searchParams.get("action"); // "subscribe" | "unsubscribe" if (!topic || !token || !action) { - return new Response(JSON.stringify({ error: "Missing params" }), { status: 400 }); + return new Response(JSON.stringify({ error: "Missing params" }), { + status: 400, + headers: corsHeaders, + }); } + const key = `fcm-${topic}`; if (action === "subscribe") { @@ -23,8 +39,14 @@ export async function GET(req: Request) { } else if (action === "unsubscribe") { await redis.srem(key, token); } else { - return new Response(JSON.stringify({ error: "Invalid action" }), { status: 400 }); + return new Response(JSON.stringify({ error: "Invalid action" }), { + status: 400, + headers: corsHeaders, + }); } - return new Response(JSON.stringify({ success: true }), { status: 200 }); + return new Response(JSON.stringify({ success: true }), { + status: 200, + headers: corsHeaders, + }); } |
