Skip to content

Commit

Permalink
get the environment variables at runtime as opposed to build time
Browse files Browse the repository at this point in the history
  • Loading branch information
regulad committed Jul 25, 2024
1 parent c029af7 commit 23efba8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
9 changes: 4 additions & 5 deletions src/app/api/devices/[id]/notification/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -89,7 +86,7 @@ export async function POST(req: NextRequest): Promise<NextResponse> {
}

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";

Expand Down Expand Up @@ -150,6 +147,8 @@ export async function POST(req: NextRequest): Promise<NextResponse> {
destinationNotification.ios_badge_type = "Increase";
destinationNotification.ios_badge_count = 1;

const oneSignalClient = getOneSignalClient();

await oneSignalClient.createNotification(destinationNotification);

return NextResponse.json(
Expand Down
11 changes: 7 additions & 4 deletions src/app/api/onesignal/route.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -33,16 +32,20 @@ export async function GET(): Promise<NextResponse<OneSignalHashReturn>> {

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(
Expand Down
21 changes: 8 additions & 13 deletions src/utils/onesignal/onesignal-server.ts
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 23efba8

Please sign in to comment.