flotilla/src/app/components/LogInBunker.svelte

107 lines
3.3 KiB
Svelte
Raw Normal View History

<script lang="ts">
2025-03-20 19:55:12 +00:00
import type {Nip46ResponseWithResult} from "@welshman/signer"
2025-04-11 18:51:15 +00:00
import {Nip46Broker, makeSecret} from "@welshman/signer"
import {loginWithNip01, loginWithNip46} from "@welshman/app"
2025-02-04 03:28:29 +00:00
import {preventDefault} from "@lib/html"
import Spinner from "@lib/components/Spinner.svelte"
import Button from "@lib/components/Button.svelte"
import Icon from "@lib/components/Icon.svelte"
import ModalHeader from "@lib/components/ModalHeader.svelte"
import ModalFooter from "@lib/components/ModalFooter.svelte"
2025-03-20 19:55:12 +00:00
import BunkerConnect, {BunkerConnectController} from "@app/components/BunkerConnect.svelte"
import BunkerUrl from "@app/components/BunkerUrl.svelte"
2025-02-14 19:12:19 +00:00
import {loadUserData} from "@app/requests"
2025-03-20 19:55:12 +00:00
import {clearModals} from "@app/modal"
2024-11-14 18:53:13 +00:00
import {setChecked} from "@app/notifications"
import {pushToast} from "@app/toast"
2025-04-11 18:51:15 +00:00
import {SIGNER_RELAYS, NIP46_PERMS} from "@app/state"
2025-03-20 19:55:12 +00:00
const back = () => history.back()
2025-03-20 19:55:12 +00:00
const controller = new BunkerConnectController({
onNostrConnect: async (response: Nip46ResponseWithResult) => {
2025-04-11 18:51:15 +00:00
const pubkey = await controller.broker.getPublicKey()
2025-04-11 18:51:15 +00:00
await loadUserData(pubkey)
2025-03-20 19:55:12 +00:00
2025-04-11 18:51:15 +00:00
loginWithNip46(pubkey, controller.clientSecret, response.event.pubkey, SIGNER_RELAYS)
2025-03-20 19:55:12 +00:00
setChecked("*")
clearModals()
},
})
const onSubmit = async () => {
2025-03-20 19:55:12 +00:00
if (controller.loading) return
2025-03-20 19:55:12 +00:00
const {signerPubkey, connectSecret, relays} = Nip46Broker.parseBunkerUrl(controller.bunker)
2024-12-03 00:21:54 +00:00
if (!signerPubkey || relays.length === 0) {
return pushToast({
theme: "error",
2024-10-29 21:40:37 +00:00
message: "Sorry, it looks like that's an invalid bunker link.",
})
}
2025-03-20 19:55:12 +00:00
controller.loading = true
try {
2025-03-20 19:55:12 +00:00
const {clientSecret} = controller
2025-05-07 20:53:58 +00:00
const broker = new Nip46Broker({relays, clientSecret, signerPubkey})
2025-04-11 18:51:15 +00:00
const result = await broker.connect(connectSecret, NIP46_PERMS)
const pubkey = await broker.getPublicKey()
2024-12-03 00:21:54 +00:00
2025-04-11 18:51:15 +00:00
// TODO: remove ack result
if (pubkey && ["ack", connectSecret].includes(result)) {
2025-05-07 20:53:58 +00:00
broker.cleanup()
2025-03-20 19:55:12 +00:00
controller.stop()
2025-04-11 18:51:15 +00:00
await loadUserData(pubkey)
loginWithNip46(pubkey, clientSecret, signerPubkey, relays)
2024-12-03 00:21:54 +00:00
} else {
2024-10-29 21:40:37 +00:00
return pushToast({
theme: "error",
message: "Something went wrong, please try again!",
})
}
} finally {
2025-03-20 19:55:12 +00:00
controller.loading = false
}
2024-10-29 21:40:37 +00:00
clearModals()
}
2025-02-04 03:28:29 +00:00
$effect(() => {
// For testing and for play store reviewers
2025-03-20 19:55:12 +00:00
if (controller.bunker === "reviewkey") {
2025-04-11 18:51:15 +00:00
loginWithNip01(makeSecret())
}
2025-02-04 00:37:14 +00:00
})
</script>
2025-02-04 00:37:14 +00:00
<form class="column gap-4" onsubmit={preventDefault(onSubmit)}>
<ModalHeader>
2025-02-04 00:37:14 +00:00
{#snippet title()}
<div>Log In</div>
{/snippet}
{#snippet info()}
<div>Connect your signer by scanning the QR code below or pasting a bunker link.</div>
{/snippet}
</ModalHeader>
2025-03-20 19:55:12 +00:00
<BunkerConnect {controller} />
<BunkerUrl loading={controller.loading} bind:bunker={controller.bunker} />
<ModalFooter>
2025-03-20 19:55:12 +00:00
<Button class="btn btn-link" onclick={back} disabled={controller.loading}>
<Icon icon="alt-arrow-left" />
Go back
</Button>
2025-03-20 19:55:12 +00:00
<Button
type="submit"
class="btn btn-primary"
disabled={controller.loading || !controller.bunker}>
<Spinner loading={controller.loading}>Next</Spinner>
2024-10-29 21:40:37 +00:00
<Icon icon="alt-arrow-right" />
</Button>
</ModalFooter>
</form>