mirror of
https://github.com/jxxghp/MoviePilot-Frontend.git
synced 2026-09-05 07:28:37 +08:00
fix(auth): decouple webpush subscription from login (#510)
This commit is contained in:
+20
-19
@@ -469,26 +469,27 @@ async function switchLanguage(locale: SupportedLocale) {
|
|||||||
|
|
||||||
// 订阅推送通知
|
// 订阅推送通知
|
||||||
async function subscribeForPushNotifications() {
|
async function subscribeForPushNotifications() {
|
||||||
if ('serviceWorker' in navigator && 'PushManager' in window) {
|
if (!('serviceWorker' in navigator) || !('PushManager' in window)) return
|
||||||
|
|
||||||
|
try {
|
||||||
const registration = await navigator.serviceWorker.ready
|
const registration = await navigator.serviceWorker.ready
|
||||||
// 获取订阅信息
|
let subscription = await registration.pushManager.getSubscription()
|
||||||
const subscription = await registration.pushManager.getSubscription().then(function (subscription) {
|
|
||||||
if (subscription === null) {
|
if (!subscription) {
|
||||||
const convertedVapidKey = urlBase64ToUint8Array(import.meta.env.VITE_PUBLIC_VAPID_KEY)
|
if (typeof Notification !== 'undefined' && Notification.permission !== 'granted') return
|
||||||
return registration.pushManager.subscribe({
|
|
||||||
userVisibleOnly: true,
|
const convertedVapidKey = urlBase64ToUint8Array(import.meta.env.VITE_PUBLIC_VAPID_KEY)
|
||||||
applicationServerKey: convertedVapidKey,
|
subscription = await registration.pushManager.subscribe({
|
||||||
})
|
userVisibleOnly: true,
|
||||||
} else {
|
applicationServerKey: convertedVapidKey,
|
||||||
return subscription
|
})
|
||||||
}
|
|
||||||
})
|
|
||||||
// 发送订阅请求
|
|
||||||
try {
|
|
||||||
await api.post('/message/webpush/subscribe', subscription)
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (subscription) {
|
||||||
|
await api.post('/message/webpush/subscribe', subscription)
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('WebPush subscription failed:', error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -508,7 +509,7 @@ async function afterLogin(superuser: boolean, userPayload: userState, filteredMe
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 订阅推送通知
|
// 订阅推送通知
|
||||||
if (superuser) await subscribeForPushNotifications()
|
if (superuser) void subscribeForPushNotifications()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理登录成功
|
// 处理登录成功
|
||||||
|
|||||||
Reference in New Issue
Block a user