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

View file

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