diff --git a/javascript/src/iframe/viamapi-iframe.js b/javascript/src/iframe/viamapi-iframe.js index 6f4b6aab037482751e9e7822f157a0dcdcd216b0..56b2a605c5ea25a7d3a8cf8efc356ae7812b40ef 100644 --- a/javascript/src/iframe/viamapi-iframe.js +++ b/javascript/src/iframe/viamapi-iframe.js @@ -327,36 +327,6 @@ function loadIdentityInternal(identityKey, pinCode) { }); } -function changeIdentityPinCodeInternal(key, oldPinCode, newPinCode) { - - return new Penpal.Promise(result => { - getIdentityFromLocalStorage(key, oldPinCode, false).then((identity) => { - - identity.pinCode = newPinCode; - - setIdentityInLocalStorage(identity).then(() => { - result({ - "data": "", - "code": "200", - "status": "Successfully changed pincode" - }); - }).catch((e) => { - result({ - "data": "", - "code": "400", - "status": "Cannot store identity " + e - }); - }); - }).catch((e) => { - result({ - "data": "", - "code": "400", - "status": "Cannot get identity " + e - }); - }); - }); -} - function getCertificateForPassport(passportUUID, internal) { return new Penpal.Promise(certificateResult => { @@ -501,8 +471,34 @@ const connection = Penpal.connectToParent({ loadIdentity(identityKey, pinCode) { return loadIdentityInternal(identityKey, pinCode) }, - changeIdentityPinCode(key, oldPinCode, newPinCode) { - return changeIdentityPinCodeInternal(key, oldPinCode, newPinCode) + checkIdentityPinCode: async (key, pinCode) => { + try { + const identity = await getIdentityFromLocalStorage(key, pinCode, false); + + if (identity) { + return encodeResponse("200", null, "Pincode check successful"); + } else { + return encodeResponse("400", null, "Pincode check failed"); + } + } catch (e) { + return encodeResponse("400", e, "Pincode check error"); + } + }, + changeIdentityPinCode: async (key, oldPinCode, newPinCode) => { + try { + const identity = await getIdentityFromLocalStorage(key, oldPinCode, false); + + if (identity) { + identity.pinCode = newPinCode; + await setIdentityInLocalStorage(identity); + + encodeResponse("200", null, "Successfully changed pincode"); + } else { + encodeResponse("400", null, "Identity not found"); + } + } catch (e) { + encodeResponse("400", e.message, "Change pincode error"); + } }, getIdentityProfile(identityKey) { return new Penpal.Promise(result => { @@ -1285,14 +1281,8 @@ connection.promise.then(parent => { for (let i = 0; i < eventsLen; i++) { const event = executeResult.data[i]; switch (event.type) { - case "Authenticated" : { - const uuid = event.payloads[0]; - const token = event.payloads[1]; - handleIdentityLogin(window.currentlyLoadedIdentity, uuid, token); - const identityToStore = window.currentlyAuthenticatedIdentity; - event.payloads = [{fromQRCode: true}]; - await setIdentityInLocalStorage(identityToStore); - await getProfileData(identityToStore); + case "DeviceConfirmed" : { + await setIdentityInLocalStorage(window.currentlyLoadedIdentity); parent.onEvent(event); break; }