nostr challenge creation works

This commit is contained in:
counterweight 2025-02-11 18:15:06 +01:00
parent 7e4adf052c
commit 9ed583ecdb
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
5 changed files with 103 additions and 14 deletions

View file

@ -1,3 +1,50 @@
async function login() {
if (!window.nostr) {
console.log("No Nostr extension found.");
return { success: false, error: "No Nostr extension detected." };
}
try {
const challengeResponse = await fetch("/api/nostr-challenge");
if (!challengeResponse.ok) throw new Error("Failed to fetch challenge");
const { challenge } = await challengeResponse.json();
console.log(`Received challenge: ${challenge}`);
} catch (error) { }
/* const pubkey = await window.nostr.getPublicKey();
const event = {
kind: 22242,
created_at: Math.floor(Date.now() / 1000),
tags: [["challenge", challenge]],
content: "Sign this challenge to authenticate",
pubkey: pubkey
};
const signedEvent = await window.nostr.signEvent(event);
const verifyResponse = await fetch("/api/nostr-verify", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(signedEvent),
});
if (!verifyResponse.ok) throw new Error("Verification failed");
const verifyResult = await verifyResponse.json();
if (verifyResult.success) {
console.log("Authentication successful!");
return { success: true, pubkey };
} else {
throw new Error("Invalid signature");
}
} catch (error) {
console.error("Error during Nostr authentication:", error.message);
return { success: false, error: error.message };
} */
}
/*
window.onload = function () {
setTimeout(function () {
console.log("This code runs 2 seconds after the window has loaded.");
@ -35,4 +82,4 @@ window.onload = function () {
)
}, 2000);
}
} */