Handle token validation messages

This commit is contained in:
NicolasCantu 2025-05-06 16:50:17 +02:00
parent bf06b6634a
commit 86393e6cfa

View File

@ -353,17 +353,46 @@ export async function registerAllListeners() {
}
}
const handleGetProfile = async (event: MessageEvent) => {
if (event.data.type !== MessageType.RETRIEVE_DATA) {
const handleValidateToken = async (event: MessageEvent) => {
if (event.data.type !== MessageType.VALIDATE_TOKEN) {
return;
}
const tokenService = await TokenService.getInstance();
const services = await Services.getInstance();
if (!services.isPaired()) {
const errorMsg = 'Device not paired';
errorResponse(errorMsg, event.origin);
return;
console.log('Received validate token msg');
const tokenService = await TokenService.getInstance();
const accessToken = event.data.accessToken;
const refreshToken = event.data.refreshToken;
try {
if (!accessToken || !refreshToken) {
// That's actually a different error
throw new Error('Missing access token or refresh token');
}
} catch (e) {
console.error('❌ Erreur:', e);
errorResponse(`Failed to get profile: ${e}`, event.origin);
}
let isValid = false;
if (await tokenService.validateToken(accessToken, event.origin)) {
isValid = true;
}
window.parent.postMessage(
{
type: MessageType.VALIDATE_TOKEN,
accessToken: accessToken,
refreshToken: refreshToken,
isValid: isValid
},
event.origin
);
};
const handleRenewToken = async (event: MessageEvent) => {
if (event.data.type !== MessageType.RENEW_TOKEN) {
return;
}
try {