Fix: Ensure VAPID keys are initialized inside the request handler for reliability
This commit is contained in:
parent
95f0d94411
commit
0eab4b36e7
@ -2,13 +2,17 @@ import { NextRequest, NextResponse } from 'next/server';
|
||||
import webPush from 'web-push';
|
||||
import { getUsersForRemindersD1, updateLastNotifiedD1 } from '@/lib/d1';
|
||||
|
||||
// Configure web-push
|
||||
if (process.env.NEXT_PUBLIC_VAPID_PUBLIC_KEY && process.env.VAPID_PRIVATE_KEY) {
|
||||
// Configure web-push - Helper called inside handler to ensure env is ready
|
||||
function ensureVapidConfig() {
|
||||
if (process.env.NEXT_PUBLIC_VAPID_PUBLIC_KEY && process.env.VAPID_PRIVATE_KEY) {
|
||||
webPush.setVapidDetails(
|
||||
process.env.VAPID_SUBJECT || 'mailto:example@yourdomain.org',
|
||||
process.env.NEXT_PUBLIC_VAPID_PUBLIC_KEY,
|
||||
process.env.VAPID_PRIVATE_KEY
|
||||
);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const MESSAGES = {
|
||||
@ -76,6 +80,16 @@ function getNotificationData(timeStr: string) {
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const isVapidReady = ensureVapidConfig();
|
||||
if (!isVapidReady) {
|
||||
console.error('VAPID Configuration missing! Keys:', {
|
||||
public: !!process.env.NEXT_PUBLIC_VAPID_PUBLIC_KEY,
|
||||
private: !!process.env.VAPID_PRIVATE_KEY
|
||||
});
|
||||
// We don't return early here so we can see which users fail below,
|
||||
// but generateRequestDetails will likely throw.
|
||||
}
|
||||
|
||||
// Protect with a secret if configured
|
||||
const authHeader = request.headers.get('authorization');
|
||||
if (process.env.CRON_SECRET && authHeader !== `Bearer ${process.env.CRON_SECRET}`) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user