Add bunker login

This commit is contained in:
Jon Staab
2024-10-29 14:40:37 -07:00
parent 16c942c917
commit 12fab67961
10 changed files with 108 additions and 46 deletions
+32
View File
@@ -0,0 +1,32 @@
<script lang="ts">
import Link from "@lib/components/Link.svelte"
import Button from "@lib/components/Button.svelte"
import ModalHeader from "@lib/components/ModalHeader.svelte"
import {PLATFORM_NAME} from "@app/state"
</script>
<div class="column gap-4">
<ModalHeader>
<div slot="title">What is a bunker link?</div>
</ModalHeader>
<p>
<Link external class="link" href="https://nostr.com/">Nostr</Link> uses "keys" instead of
passwords to identify users. This allows users to own their social identity instead of
renting it from a tech company, and can bring it with them from app to app.
</p>
<p>
A good way to manage your keys is to use a remote signing application. These apps can hold
your keys and log you in remotely to as many applications as you like, without increasing
the risk of your keys being stolen.
</p>
<p>
One way to log in with a remote signer is using a "bunker link" which is more secure and
decentralized than other solutions. Check your signer for a link beginning with "bunker://",
copy it into {PLATFORM_NAME}, and you should be good to go!
</p>
<p>
If you don't have a signer yet, <Link external class="link" href="https://nsec.app/">nsec.app</Link>
is an easy way to get started.
</p>
<Button class="btn btn-primary" on:click={() => history.back()}>Got it</Button>
</div>
+1 -1
View File
@@ -17,7 +17,7 @@
<div class="column gap-4">
<div class="py-2">
<h1 class="heading">Welcome to {PLATFORM_NAME}!</h1>
<p class="text-center">The chat app built for sovereign communities.</p>
<p class="text-center">The chat app built for self-hosted communities.</p>
</div>
<Button on:click={logIn}>
<CardButton>
+5 -5
View File
@@ -10,7 +10,7 @@
import SignUp from "@app/components/SignUp.svelte"
import InfoNostr from "@app/components/InfoNostr.svelte"
import LogInInfoRemoteSigner from "@app/components/LogInInfoRemoteSigner.svelte"
import LogInKey from "@app/components/LogInKey.svelte"
import LogInBunker from "@app/components/LogInBunker.svelte"
import {pushModal, clearModals} from "@app/modal"
import {PLATFORM_NAME} from "@app/state"
import {pushToast} from "@app/toast"
@@ -103,7 +103,7 @@
}
}
const loginWithKey = () => pushModal(LogInKey)
const loginWithBunker = () => pushModal(LogInBunker)
let username = ""
let domain = "nsec.app"
@@ -164,9 +164,9 @@
Log in with {app.name}
</Button>
{/each}
<Button disabled={loading} on:click={loginWithKey} class="btn btn-neutral">
<Icon icon="key" />
Log in with Key
<Button disabled={loading} on:click={loginWithBunker} class="btn btn-neutral">
<Icon icon="cpu" />
Log in with Bunker Link
</Button>
<div class="text-sm">
Need an account?
@@ -1,6 +1,5 @@
<script lang="ts">
import {nip19} from "nostr-tools"
import {getPubkey} from "@welshman/signer"
import {Nip46Broker} from "@welshman/signer"
import {addSession} from "@welshman/app"
import Spinner from "@lib/components/Spinner.svelte"
import Button from "@lib/components/Button.svelte"
@@ -8,68 +7,60 @@
import Icon from "@lib/components/Icon.svelte"
import ModalHeader from "@lib/components/ModalHeader.svelte"
import ModalFooter from "@lib/components/ModalFooter.svelte"
import InfoNostr from "@app/components/InfoNostr.svelte"
import {loadUserData} from "@app/commands"
import InfoBunker from "@app/components/InfoBunker.svelte"
import {loginWithNip46, loadUserData} from "@app/commands"
import {pushModal, clearModals} from "@app/modal"
import {pushToast} from "@app/toast"
import {PLATFORM_NAME} from "@app/state"
const back = () => history.back()
const onSubmit = async () => {
let secret = key
const {pubkey, token, relays} = Nip46Broker.parseBunkerLink(bunker)
if (secret.startsWith("nsec")) {
secret = nip19.decode(secret).data as string
}
if (!isKeyValid(secret)) {
if (!pubkey || relays.length === 0) {
return pushToast({
theme: "error",
message: "Sorry, it looks like that's an invalid private key.",
message: "Sorry, it looks like that's an invalid bunker link.",
})
}
const pubkey = getPubkey(secret)
addSession({method: "nip01", pubkey, secret})
loading = true
await loadUserData(pubkey)
try {
if (!await loginWithNip46(token, {pubkey, relays})) {
return pushToast({
theme: "error",
message: "Something went wrong, please try again!",
})
}
await loadUserData(pubkey)
} finally {
loading = false
}
clearModals()
}
const isKeyValid = (key: string) => {
// Validate the key before setting it to state by encoding it using bech32.
// This will error if invalid (this works whether it's a public or a private key)
try {
getPubkey(key)
} catch (e) {
return false
}
return true
}
let key = ""
let bunker = ""
let loading = false
</script>
<form class="column gap-4" on:submit|preventDefault={onSubmit}>
<ModalHeader>
<div slot="title">Log In</div>
<div slot="info">Already have a nostr key?</div>
<div slot="info">Connect your signer app with {PLATFORM_NAME} using a bunker link.</div>
</ModalHeader>
<Field>
<p slot="label">Private Key*</p>
<p slot="label">Bunker Link*</p>
<label class="input input-bordered flex w-full items-center gap-2" slot="input">
<Icon icon="key" />
<input bind:value={key} class="grow" type="password" />
<Icon icon="cpu" />
<input bind:value={bunker} class="grow" placeholder="bunker://" />
</label>
<p slot="info">
A nostr nsec or private key. Note that this log in method is not recommended.
<Button class="link" on:click={() => pushModal(InfoNostr)}>What is nostr?</Button>
A login link provided by a nostr signing app.
<Button class="link" on:click={() => pushModal(InfoBunker)}>What is a bunker link?</Button>
</p>
</Field>
<ModalFooter>
@@ -79,7 +70,7 @@
</Button>
<Button type="submit" class="btn btn-primary" disabled={loading}>
<Spinner {loading}>Next</Spinner>
<Icon icon="alt-arrow-right" class="!bg-base-300" />
<Icon icon="alt-arrow-right" />
</Button>
</ModalFooter>
</form>
+1 -1
View File
@@ -55,7 +55,7 @@
</Button>
<Button type="submit" class="btn btn-primary">
Next
<Icon icon="alt-arrow-right" class="!bg-base-300" />
<Icon icon="alt-arrow-right" />
</Button>
</ModalFooter>
</form>
+1 -1
View File
@@ -51,7 +51,7 @@
</Button>
<Button type="submit" class="btn btn-primary">
Next
<Icon icon="alt-arrow-right" class="!bg-base-300" />
<Icon icon="alt-arrow-right" />
</Button>
</ModalFooter>
</form>
@@ -37,7 +37,7 @@
</Button>
<Button type="submit" class="btn btn-primary">
Let's go
<Icon icon="alt-arrow-right" class="!bg-base-300" />
<Icon icon="alt-arrow-right" />
</Button>
</ModalFooter>
</form>