Skip to content

Instantly share code, notes, and snippets.

@hieptl
Last active November 2, 2021 16:14
Show Gist options
  • Save hieptl/6291e2a46f258ed5d4b5fd4a021cad01 to your computer and use it in GitHub Desktop.
Save hieptl/6291e2a46f258ed5d4b5fd4a021cad01 to your computer and use it in GitHub Desktop.
login.js - init EThree - Encrypted Chat App
...
const registerEThree = async (eThree, user) => {
if (eThree) {
try {
await eThree.register();
// generate the backup key.
if (user && user.keyPassword) {
await eThree.backupPrivateKey(user.keyPassword);
}
} catch (err) {
if (err.name === 'IdentityAlreadyExistsError') {
console.log('EThree id already existed');
// restore the private key
const hasLocalPrivateKey = await eThree.hasLocalPrivateKey();
console.log(`${user.fullname} has local private key: ${hasLocalPrivateKey}`);
if (!hasLocalPrivateKey) {
await eThree.restorePrivateKey(user.keyPassword);
}
}
}
}
};
const initEthree = async (user) => {
async function getVirgilToken() {
const response = await fetch('http://localhost:8080/virgil-jwt', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
identity: user.id
})
})
return response.json().then(data => {
localStorage.setItem('virgilToken', JSON.stringify(data.virgilToken));
return data.virgilToken;
});
}
const eThree = await window.E3kit.EThree.initialize(getVirgilToken);
if (eThree) {
console.log('Initialized EThree successfully');
console.log(eThree);
setEThree(eThree);
await registerEThree(eThree, user);
localStorage.setItem('auth', JSON.stringify(user));
setUser(user);
setIsLoading(false);
history.push('/');
}
};
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment