Skip to content

Commit

Permalink
Basic error handling during onboarding.
Browse files Browse the repository at this point in the history
  • Loading branch information
chmac committed Aug 11, 2024
1 parent 7fabf29 commit 3ce87b2
Showing 1 changed file with 68 additions and 64 deletions.
132 changes: 68 additions & 64 deletions src/onboarding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,79 +9,83 @@ import { getTrustrootsUsernameFromLocation } from "./router";
import { alert, confirmYesNo, prompt } from "./utils";

export const startUserOnboarding = async () => {
const username = getTrustrootsUsernameFromLocation();
if (username.length < 3) {
alert(`Sorry, you need to click to this page from trustroots.org.`);
return;
}

if (await confirmYesNo("Have you used trustroots notes before?")) {
const key = await prompt({
text: `Great. What's your private key?`,
inputLabel: "Your private key (starts nsec)",
});
if (typeof key === "string" && key.length > 0 && key.startsWith("nsec")) {
await setNsecPrivateKey({ nsecPrivateKey: key });
alert(`Saved. Please right click again to add a note.`);
globalThis.location.reload();
return;
} else {
await alert(`Private key failed.`);
try {
const username = getTrustrootsUsernameFromLocation();
if (username.length < 3) {
alert(`Sorry, you need to click to this page from trustroots.org.`);
return;
}
}

if (
!(await confirmYesNo(
`Notes shared here are public and accessible to other services, sites and maps. I agree?`
))
) {
await alert(
`This will become more user friendly in the future. Feel free to come back anytime. Now we'll take you back to the main trustroots site.`
);
globalThis.location.href = "https://www.trustroots.org/search";
return;
}
if (await confirmYesNo("Have you used trustroots notes before?")) {
const key = await prompt({
text: `Great. What's your private key?`,
inputLabel: "Your private key (starts nsec)",
});
if (typeof key === "string" && key.length > 0 && key.startsWith("nsec")) {
await setNsecPrivateKey({ nsecPrivateKey: key });
await alert(`Saved. Please right click again to add a note.`);
globalThis.location.reload();
return;
} else {
await alert(`Private key failed.`);
return;
}
}

if (
!(await confirmYesNo(
`Notes cannot me edited or deleted. Are you ok with this?`
))
) {
await alert(
`This will become more user friendly in the future. Feel free to come back anytime. Now we'll take you back to the main trustroots site.`
);
return;
}
if (
!(await confirmYesNo(
`Notes shared here are public and accessible to other services, sites and maps. I agree?`
))
) {
await alert(
`This will become more user friendly in the future. Feel free to come back anytime. Now we'll take you back to the main trustroots site.`
);
globalThis.location.href = "https://www.trustroots.org/search";
return;
}

const newKey = await generatePrivateKey();
const newKeyNsec = nip19.nsecEncode(newKey);
await prompt({
text: `This is your NOSTR private key. It is important that you save it somewhere safe. THIS KEY IS PRIVATE: DO NOT SHARE IT WITH ANYONE ELSE. `,
inputValue: newKeyNsec,
});
const confirmedKey = await prompt({
text: `You saved it, right. Please re-enter your NOSTR private key.`,
});
if (
!(await confirmYesNo(
`Notes cannot me edited or deleted. Are you ok with this?`
))
) {
await alert(
`This will become more user friendly in the future. Feel free to come back anytime. Now we'll take you back to the main trustroots site.`
);
return;
}

if (newKeyNsec !== confirmedKey) {
await alert(
`The private key that you entered is not the same one as the one issued to you. Right click (or long press) to start the process again.`
);
return;
}
const newKey = await generatePrivateKey();
const newKeyNsec = nip19.nsecEncode(newKey);
await prompt({
text: `This is your NOSTR private key. It is important that you save it somewhere safe. THIS KEY IS PRIVATE: DO NOT SHARE IT WITH ANYONE ELSE. `,
inputValue: newKeyNsec,
});
const confirmedKey = await prompt({
text: `You saved it, right. Please re-enter your NOSTR private key.`,
});

await setPrivateKey({ privateKey: newKey });
if (newKeyNsec !== confirmedKey) {
await alert(
`The private key that you entered is not the same one as the one issued to you. Right click (or long press) to start the process again.`
);
return;
}

await setPrivateKey({ privateKey: newKey });

const newPublicKey = getPublicKey(newKey);
const newNpub = nip19.npubEncode(newPublicKey);
await putNpubOnTrustroots({ npub: newNpub });
const newPublicKey = getPublicKey(newKey);
const newNpub = nip19.npubEncode(newPublicKey);
await putNpubOnTrustroots({ npub: newNpub });

await setProfile({ name: "", about: "", trustrootsUsername: username });
await setProfile({ name: "", about: "", trustrootsUsername: username });

await alert(
`Nice job. You're ready to create points now. Right click again to get started.`
);
await alert(
`Nice job. You're ready to create points now. Right click again to get started.`
);

globalThis.window.location.reload();
globalThis.window.location.reload();
} catch (error) {
alert(`Sorry, there was an unexpected error. ${error}`);
}
};

0 comments on commit 3ce87b2

Please sign in to comment.