Add GET_PAIRING_ID event listener

This commit is contained in:
Sosthene 2025-06-11 15:31:38 +02:00 committed by NicolasCantu
parent d6e06f3594
commit b52ff937f0
2 changed files with 37 additions and 0 deletions

View File

@ -24,6 +24,7 @@ export interface INotification {
export enum MessageType {
LISTENING = 'LISTENING',
GET_PAIRING_ID = 'GET_PAIRING_ID',
REQUEST_LINK = 'REQUEST_LINK',
LINK_ACCEPTED = 'LINK_ACCEPTED',
CREATE_PROFILE = 'CREATE_PROFILE',

View File

@ -446,6 +446,40 @@ export async function registerAllListeners() {
}
}
const handleGetPairingId = async (event: MessageEvent) => {
if (event.data.type !== MessageType.GET_PAIRING_ID) return;
const tokenService = await TokenService.getInstance();
if (!services.isPaired()) {
const errorMsg = 'Device not paired';
errorResponse(errorMsg, event.origin);
return;
}
try {
const { accessToken } = event.data;
// Validate the session token
if (!accessToken || !tokenService.validateToken(accessToken, event.origin)) {
throw new Error('Invalid or expired session token');
}
const userPairingId = services.getPairingProcessId();
window.parent.postMessage(
{
type: MessageType.GET_PAIRING_ID,
userPairingId,
},
event.origin
);
} catch (e) {
const errorMsg = `Failed to get pairing id: ${e}`;
errorResponse(errorMsg, event.origin);
}
}
// Remove before adding to be sure there's no duplicate
window.removeEventListener('message', handleRequestLink);
window.removeEventListener('message', handleAddProfile);
@ -454,6 +488,7 @@ export async function registerAllListeners() {
window.removeEventListener('message', handleDecryptState);
window.removeEventListener('message', handleValidateToken);
window.removeEventListener('message', handleRenewToken);
window.removeEventListener('message', handleGetPairingId);
window.addEventListener('message', handleRequestLink);
window.addEventListener('message', handleAddProfile);
@ -462,6 +497,7 @@ export async function registerAllListeners() {
window.addEventListener('message', handleDecryptState);
window.addEventListener('message', handleValidateToken);
window.addEventListener('message', handleRenewToken);
window.addEventListener('message', handleGetPairingId);
console.log('Initialized event handlers');