update ask access token login in service worker

This commit is contained in:
Ajay Bura 2024-09-11 18:32:12 +05:30
parent 042cbc4453
commit c38151c237
2 changed files with 40 additions and 25 deletions

View file

@ -21,25 +21,30 @@ import './app/i18n';
document.body.classList.add(configClass, varsClass);
settings.applyTheme();
// Register Service Worker
if ('serviceWorker' in navigator) {
const swUrl =
import.meta.env.MODE === 'production'
? `${trimTrailingSlash(import.meta.env.BASE_URL)}/sw.js`
: `/dev-sw.js?dev-sw`;
const registerServiceWorker = async () => {
if ('serviceWorker' in navigator) {
const swUrl =
import.meta.env.MODE === 'production'
? `${trimTrailingSlash(import.meta.env.BASE_URL)}/sw.js`
: `/dev-sw.js?dev-sw`;
navigator.serviceWorker.register(swUrl);
navigator.serviceWorker.addEventListener('message', (event) => {
if (event.data?.type === 'token' && event.data?.responseKey) {
// Get the token for SW.
const token = localStorage.getItem('cinny_access_token') ?? undefined;
event.source!.postMessage({
responseKey: event.data.responseKey,
token,
await navigator.serviceWorker.register(swUrl);
navigator.serviceWorker.ready.then((registration) => {
navigator.serviceWorker.addEventListener('message', (event) => {
if (event.data?.type === 'token' && event.data?.messageId) {
const token = localStorage.getItem('cinny_access_token') ?? undefined;
registration.active?.postMessage({
messageId: event.data.messageId,
token,
});
}
});
}
});
}
});
}
};
window.addEventListener('load', registerServiceWorker);
const mountApp = () => {
const rootContainer = document.getElementById('root');

View file

@ -3,16 +3,26 @@
export type {};
declare const self: ServiceWorkerGlobalScope;
type Message = { messageId: string };
type MessageListener = (message: Message) => void;
const messageListeners = new Map<string, MessageListener>();
self.addEventListener('message', (event) => {
const { messageId } = event.data;
if (typeof messageId === 'string') {
messageListeners.get(messageId)?.(event.data);
messageListeners.delete(messageId);
}
});
type TokenMessage = Message & {
token?: string;
};
async function askForAccessToken(client: Client): Promise<string | undefined> {
return new Promise((resolve) => {
const responseKey = Math.random().toString(36);
const listener = (event: ExtendableMessageEvent) => {
if (event.data.responseKey !== responseKey) return;
resolve(event.data.token);
self.removeEventListener('message', listener);
};
self.addEventListener('message', listener);
client.postMessage({ responseKey, type: 'token' });
const messageId = Math.random().toString(36);
messageListeners.set(messageId, (message: TokenMessage) => resolve(message.token));
client.postMessage({ messageId, type: 'token' });
});
}