From 23efba8004e06ed3ad90d1cb9fc668ccb60bdc49 Mon Sep 17 00:00:00 2001 From: Parker Wahle Date: Thu, 25 Jul 2024 00:40:44 -0400 Subject: [PATCH] get the environment variables at runtime as opposed to build time --- .../api/devices/[id]/notification/route.ts | 9 ++++---- src/app/api/onesignal/route.ts | 11 ++++++---- src/utils/onesignal/onesignal-server.ts | 21 +++++++------------ 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/app/api/devices/[id]/notification/route.ts b/src/app/api/devices/[id]/notification/route.ts index 776b62b..0e8c713 100644 --- a/src/app/api/devices/[id]/notification/route.ts +++ b/src/app/api/devices/[id]/notification/route.ts @@ -2,10 +2,7 @@ import { getDeviceForId } from "%/device-info/device-info.ts"; import { deviceApiEndpoint } from "%/endpoint-regex.ts"; import getStaticUrlForImageDataUrl from "@/app/api/render/path.ts"; import { BASE_ORIGIN } from "@/constants.ts"; -import { - ONESIGNAL_APP_ID, - oneSignalClient, -} from "@/utils/onesignal/onesignal-server.ts"; +import getOneSignalClient from "@/utils/onesignal/onesignal-server.ts"; import * as OneSignal from "@onesignal/node-onesignal"; import { NextRequest, NextResponse } from "next/server"; import { z } from "zod"; @@ -89,7 +86,7 @@ export async function POST(req: NextRequest): Promise { } const destinationNotification = new OneSignal.Notification(); - destinationNotification.app_id = ONESIGNAL_APP_ID; + destinationNotification.app_id = process.env.ONESIGNAL_APP_ID!; destinationNotification.include_aliases = { external_id: [device.ownerId] }; destinationNotification.target_channel = "push"; @@ -150,6 +147,8 @@ export async function POST(req: NextRequest): Promise { destinationNotification.ios_badge_type = "Increase"; destinationNotification.ios_badge_count = 1; + const oneSignalClient = getOneSignalClient(); + await oneSignalClient.createNotification(destinationNotification); return NextResponse.json( diff --git a/src/app/api/onesignal/route.ts b/src/app/api/onesignal/route.ts index 562f041..ba03264 100644 --- a/src/app/api/onesignal/route.ts +++ b/src/app/api/onesignal/route.ts @@ -1,4 +1,3 @@ -import { ONESIGNAL_API_KEY } from "@/utils/onesignal/onesignal-server.ts"; import { createClient } from "@/utils/supabase/server.ts"; import { NextResponse } from "next/server"; import { createHmac } from "node:crypto"; @@ -33,16 +32,20 @@ export async function GET(): Promise> { const { id, email, phone } = user; - const externalIdHash = createHmac("sha256", ONESIGNAL_API_KEY) + const externalIdHash = createHmac("sha256", process.env.ONESIGNAL_API_KEY!) .update(id) .digest("hex"); const emailHash = email - ? createHmac("sha256", ONESIGNAL_API_KEY).update(email).digest("hex") + ? createHmac("sha256", process.env.ONESIGNAL_API_KEY!) + .update(email) + .digest("hex") : undefined; const phoneHash = phone - ? createHmac("sha256", ONESIGNAL_API_KEY).update(phone).digest("hex") + ? createHmac("sha256", process.env.ONESIGNAL_API_KEY!) + .update(phone) + .digest("hex") : undefined; return NextResponse.json( diff --git a/src/utils/onesignal/onesignal-server.ts b/src/utils/onesignal/onesignal-server.ts index cd7fe8b..b035e16 100644 --- a/src/utils/onesignal/onesignal-server.ts +++ b/src/utils/onesignal/onesignal-server.ts @@ -1,16 +1,11 @@ import * as OneSignal from "@onesignal/node-onesignal"; -export const ONESIGNAL_APP_ID = process.env.ONESIGNAL_APP_ID!; -const ONESIGNAL_USER_AUTH_KEY = process.env.ONESIGNAL_USER_AUTH_KEY!; -export const ONESIGNAL_API_KEY = process.env.ONESIGNAL_API_KEY!; - -if (!ONESIGNAL_API_KEY || !ONESIGNAL_APP_ID || !ONESIGNAL_USER_AUTH_KEY) { - throw new Error("OneSignal environment variables not found"); +// get the environment variables at runtime as opposed to build time +export default function getOneSignalClient() { + const oneSignalConfiguration = OneSignal.createConfiguration({ + userAuthKey: process.env.ONESIGNAL_USER_AUTH_KEY!, + restApiKey: process.env.ONESIGNAL_API_KEY!, + }); + + return new OneSignal.DefaultApi(oneSignalConfiguration); } - -const oneSignalConfiguration = OneSignal.createConfiguration({ - userAuthKey: ONESIGNAL_USER_AUTH_KEY, - restApiKey: ONESIGNAL_API_KEY, -}); - -export const oneSignalClient = new OneSignal.DefaultApi(oneSignalConfiguration);