refactor invite page to pickup webpacked code
This commit is contained in:
parent
5b6ab8779e
commit
ae64424f54
3 changed files with 86 additions and 77 deletions
|
|
@ -1,74 +0,0 @@
|
||||||
window.onload = function () {
|
|
||||||
if (!window.nostr) {
|
|
||||||
console.log('Nostr extension not present');
|
|
||||||
document.querySelector('#nostr-signup-button').disabled = true;
|
|
||||||
document.querySelector('#no-extension-nudges').style.display = 'block';
|
|
||||||
} else {
|
|
||||||
console.log('Nostr extension present');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const signUpConfirmation = document.querySelector('#sign-up-success');
|
|
||||||
|
|
||||||
function showConfirmationAndRedirect() {
|
|
||||||
signUpConfirmation.classList.add('revealed');
|
|
||||||
setTimeout(() => {
|
|
||||||
window.location.href = '/createProfile';
|
|
||||||
}, 5000);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function acceptInvite() {
|
|
||||||
let challengeResponse;
|
|
||||||
try {
|
|
||||||
challengeResponse = await fetch('/api/signup/nostr-challenge', {
|
|
||||||
method: 'GET',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.log(`Something went wrong: ${error}`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const { challenge } = await challengeResponse.json();
|
|
||||||
|
|
||||||
let pubkey;
|
|
||||||
try {
|
|
||||||
pubkey = await window.nostr.getPublicKey();
|
|
||||||
} catch (error) {
|
|
||||||
document.querySelector('#rejected-nostr-nudges').style.display = 'block';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const event = {
|
|
||||||
kind: 22242,
|
|
||||||
created_at: Math.floor(Date.now() / 1000),
|
|
||||||
tags: [['challenge', challenge]],
|
|
||||||
content: 'Sign this challenge to authenticate',
|
|
||||||
pubkey: pubkey,
|
|
||||||
};
|
|
||||||
|
|
||||||
let signedEvent;
|
|
||||||
try {
|
|
||||||
signedEvent = await window.nostr.signEvent(event);
|
|
||||||
} catch (error) {
|
|
||||||
document.querySelector('#rejected-nostr-nudges').style.display = 'block';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let verifyResponse;
|
|
||||||
try {
|
|
||||||
verifyResponse = await fetch('/api/signup/nostr-verify', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify(signedEvent),
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.log(`Something went wrong: ${error}`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (verifyResponse.ok) {
|
|
||||||
showConfirmationAndRedirect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
83
src/front/pages/invite.js
Normal file
83
src/front/pages/invite.js
Normal file
|
|
@ -0,0 +1,83 @@
|
||||||
|
const invitesFunction = () => {
|
||||||
|
window.onload = function () {
|
||||||
|
if (!window.nostr) {
|
||||||
|
console.log('Nostr extension not present');
|
||||||
|
document.querySelector('#nostr-signup-button').disabled = true;
|
||||||
|
document.querySelector('#no-extension-nudges').style.display = 'block';
|
||||||
|
} else {
|
||||||
|
console.log('Nostr extension present');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const signUpConfirmation = document.querySelector('#sign-up-success');
|
||||||
|
|
||||||
|
function showConfirmationAndRedirect() {
|
||||||
|
signUpConfirmation.classList.add('revealed');
|
||||||
|
setTimeout(() => {
|
||||||
|
window.location.href = '/createProfile';
|
||||||
|
}, 5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function acceptInvite() {
|
||||||
|
let challengeResponse;
|
||||||
|
try {
|
||||||
|
challengeResponse = await fetch('/api/signup/nostr-challenge', {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.log(`Something went wrong: ${error}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { challenge } = await challengeResponse.json();
|
||||||
|
|
||||||
|
let pubkey;
|
||||||
|
try {
|
||||||
|
pubkey = await window.nostr.getPublicKey();
|
||||||
|
} catch (error) {
|
||||||
|
document.querySelector('#rejected-nostr-nudges').style.display = 'block';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const event = {
|
||||||
|
kind: 22242,
|
||||||
|
created_at: Math.floor(Date.now() / 1000),
|
||||||
|
tags: [['challenge', challenge]],
|
||||||
|
content: 'Sign this challenge to authenticate',
|
||||||
|
pubkey: pubkey,
|
||||||
|
};
|
||||||
|
|
||||||
|
let signedEvent;
|
||||||
|
try {
|
||||||
|
signedEvent = await window.nostr.signEvent(event);
|
||||||
|
} catch (error) {
|
||||||
|
document.querySelector('#rejected-nostr-nudges').style.display = 'block';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let verifyResponse;
|
||||||
|
try {
|
||||||
|
verifyResponse = await fetch('/api/signup/nostr-verify', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(signedEvent),
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.log(`Something went wrong: ${error}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (verifyResponse.ok) {
|
||||||
|
showConfirmationAndRedirect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const signUpButton = document.getElementById('nostr-signup-button');
|
||||||
|
signUpButton.addEventListener('click', () => {
|
||||||
|
acceptInvite();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
invitesFunction();
|
||||||
|
|
@ -21,7 +21,7 @@
|
||||||
/>
|
/>
|
||||||
</figure>
|
</figure>
|
||||||
<p>Usa tu extensión de Nostr para darte de alta:</p>
|
<p>Usa tu extensión de Nostr para darte de alta:</p>
|
||||||
<form onsubmit="acceptInvite();return false">
|
<div>
|
||||||
<button
|
<button
|
||||||
id="nostr-signup-button"
|
id="nostr-signup-button"
|
||||||
type="submit"
|
type="submit"
|
||||||
|
|
@ -36,7 +36,7 @@
|
||||||
</figure>
|
</figure>
|
||||||
<p>Alta con Nostr</p>
|
<p>Alta con Nostr</p>
|
||||||
</button>
|
</button>
|
||||||
</form>
|
</div>
|
||||||
<div
|
<div
|
||||||
id="rejected-nostr-nudges"
|
id="rejected-nostr-nudges"
|
||||||
class="card-secondary"
|
class="card-secondary"
|
||||||
|
|
@ -110,6 +110,6 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
<script src="/javascript/invite.js"></script>
|
<script src="/invite.bundle.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue