Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions backend/app/Http/Controllers/Api/PushSubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public function __construct(WebPushService $webPushService)
*/
public function getVapidPublicKey(): JsonResponse
{
$publicKey = config('services.webpush.vapid_public_key', env('VAPID_PUBLIC_KEY'));
$rawKey = config('services.webpush.vapid_public_key', env('VAPID_PUBLIC_KEY'));
$publicKey = $rawKey ? trim($rawKey, " \"'") : null;

return response()->json([
'vapid_public_key' => $publicKey,
Expand All @@ -43,22 +44,32 @@ public function store(Request $request): JsonResponse
]);

$user = $request->user();
if (! $user) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}

$subscription = PushSubscription::updateOrCreate(
['endpoint' => $request->input('endpoint')],
[
'user_id' => $user->id,
'public_key' => $request->input('keys.p256dh'),
'auth_token' => $request->input('keys.auth'),
'content_encoding' => $request->input('content_encoding', 'aes128gcm'),
'locale' => $request->input('locale', 'bo'),
]
);
try {
$subscription = PushSubscription::updateOrCreate(
['endpoint' => $request->input('endpoint')],
[
'user_id' => $user->id,
'public_key' => $request->input('keys.p256dh'),
'auth_token' => $request->input('keys.auth'),
'content_encoding' => $request->input('content_encoding', 'aes128gcm'),
'locale' => $request->input('locale', 'bo'),
]
);

return response()->json([
'message' => 'Push subscription saved successfully.',
'subscription' => $subscription,
]);
return response()->json([
'message' => 'Push subscription saved successfully.',
'subscription' => $subscription,
]);
} catch (\Throwable $e) {
\Illuminate\Support\Facades\Log::error('[PushSubscription] Store error: ' . $e->getMessage());
return response()->json([
'error' => 'Failed to save push subscription: ' . $e->getMessage(),
], 500);
}
}

/**
Expand Down
23 changes: 14 additions & 9 deletions backend/app/Services/WebPushService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@ public function __construct()
$subject = config('services.webpush.vapid_subject', env('VAPID_SUBJECT', 'mailto:admin@monlam.ai'));

if ($publicKey && $privateKey) {
$auth = [
'VAPID' => [
'subject' => $subject,
'publicKey' => $publicKey,
'privateKey' => $privateKey,
],
];
$this->webPush = new WebPush($auth);
$this->webPush->setReuseVAPIDHeaders(true);
try {
$auth = [
'VAPID' => [
'subject' => trim($subject),
'publicKey' => trim($publicKey),
'privateKey' => trim($privateKey),
],
];
$this->webPush = new WebPush($auth);
$this->webPush->setReuseVAPIDHeaders(true);
} catch (\Throwable $e) {
Log::error('[WebPush] Initialization failed: ' . $e->getMessage());
$this->webPush = null;
}
}
}

Expand Down
Loading