This commit is contained in:
@@ -38,6 +38,36 @@ namespace Aberwyn.Controllers
|
||||
return Ok(new { message = $"Skickade pizzanotiser till {count} användare." });
|
||||
}
|
||||
|
||||
[HttpPost("subscribe-user")]
|
||||
public async Task<IActionResult> SubscribeUser([FromBody] PushSubscription subscription)
|
||||
{
|
||||
var user = await _userManager.GetUserAsync(User);
|
||||
if (user == null) return Unauthorized();
|
||||
|
||||
var existing = await _context.PushSubscribers
|
||||
.FirstOrDefaultAsync(s => s.Endpoint == subscription.Endpoint);
|
||||
|
||||
if (existing == null)
|
||||
{
|
||||
var newSub = new PushSubscriber
|
||||
{
|
||||
Endpoint = subscription.Endpoint,
|
||||
P256DH = subscription.Keys["p256dh"],
|
||||
Auth = subscription.Keys["auth"],
|
||||
UserId = user.Id
|
||||
};
|
||||
_context.PushSubscribers.Add(newSub);
|
||||
}
|
||||
else
|
||||
{
|
||||
existing.P256DH = subscription.Keys["p256dh"];
|
||||
existing.Auth = subscription.Keys["auth"];
|
||||
existing.UserId = user.Id;
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpPost("subscribe")]
|
||||
public async Task<IActionResult> Subscribe([FromBody] PushSubscriptionWithOrder dto)
|
||||
|
||||
@@ -98,7 +98,7 @@
|
||||
applicationServerKey: urlBase64ToUint8Array(vapidPublicKey.trim())
|
||||
});
|
||||
|
||||
await fetch("/api/push/subscribe", {
|
||||
await fetch("/api/push/subscribe-user", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(sub),
|
||||
headers: { "Content-Type": "application/json" }
|
||||
|
||||
Reference in New Issue
Block a user