58 lines
1.3 KiB
JavaScript
58 lines
1.3 KiB
JavaScript
|
|
const requestAndRespondSignUpChallenge = async () => {
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
|
||
|
|
return verifyResponse;
|
||
|
|
};
|
||
|
|
|
||
|
|
module.exports = {
|
||
|
|
requestAndRespondSignUpChallenge,
|
||
|
|
};
|