nostr challenge step much more robust
This commit is contained in:
parent
805ad5fad9
commit
564dcb8083
7 changed files with 99 additions and 50 deletions
|
|
@ -10,40 +10,51 @@ window.onload = function () {
|
|||
|
||||
async function acceptInvite() {
|
||||
|
||||
if (!window.nostr) {
|
||||
console.log("No Nostr extension found.");
|
||||
return { success: false, error: "No Nostr extension detected." };
|
||||
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 {
|
||||
const challengeResponse = await fetch("/api/signup/nostr-challenge");
|
||||
if (!challengeResponse.ok) throw new Error("Failed to fetch challenge");
|
||||
const { challenge } = await challengeResponse.json();
|
||||
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
|
||||
};
|
||||
|
||||
const pubkey = await window.nostr.getPublicKey();
|
||||
let signedEvent;
|
||||
try {
|
||||
signedEvent = await window.nostr.signEvent(event);
|
||||
} 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
|
||||
};
|
||||
const verifyResponse = await fetch("/api/signup/nostr-verify", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(signedEvent),
|
||||
});
|
||||
|
||||
const signedEvent = await window.nostr.signEvent(event);
|
||||
if (verifyResponse.status === 200) {
|
||||
|
||||
const verifyResponse = await fetch("/api/signup/nostr-verify", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(signedEvent),
|
||||
});
|
||||
|
||||
if (verifyResponse.status === 200) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
} catch (error) { }
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue