flotilla/src/app/components/LogIn.svelte

168 lines
5.1 KiB
Svelte
Raw Normal View History

2024-08-09 23:22:09 +00:00
<script lang="ts">
import cx from "classnames"
2024-10-21 21:09:57 +00:00
import {onMount} from "svelte"
2024-11-12 22:03:21 +00:00
import {Capacitor} from "@capacitor/core"
import {getNip07, getNip55, Nip55Signer} from "@welshman/signer"
2026-07-28 16:16:26 +00:00
import {nip07, nip55, toSession} from "@welshman/app"
import type {Session} from "@welshman/app"
import Widget from "@assets/icons/widget-4.svg?dataurl"
2025-12-18 02:43:13 +00:00
import Letter from "@assets/icons/letter.svg?dataurl"
2025-09-10 22:26:39 +00:00
import Cpu from "@assets/icons/cpu-bolt.svg?dataurl"
import Compass from "@assets/icons/compass-big.svg?dataurl"
2026-01-16 22:17:38 +00:00
import Key from "@assets/icons/key.svg?dataurl"
2024-08-16 17:50:38 +00:00
import Icon from "@lib/components/Icon.svelte"
import Link from "@lib/components/Link.svelte"
2026-01-30 22:14:12 +00:00
import Modal from "@lib/components/Modal.svelte"
import ModalBody from "@lib/components/ModalBody.svelte"
2024-08-16 17:50:38 +00:00
import Button from "@lib/components/Button.svelte"
import Spinner from "@lib/components/Spinner.svelte"
2024-08-16 22:17:30 +00:00
import SignUp from "@app/components/SignUp.svelte"
2024-10-14 19:35:08 +00:00
import InfoNostr from "@app/components/InfoNostr.svelte"
2024-10-29 21:40:37 +00:00
import LogInBunker from "@app/components/LogInBunker.svelte"
2025-12-18 02:43:13 +00:00
import LogInEmail from "@app/components/LogInEmail.svelte"
2026-01-16 22:17:38 +00:00
import LogInKey from "@app/components/LogInKey.svelte"
2026-06-08 21:14:40 +00:00
import {pushModal, clearModals} from "@app/modal"
import {PLATFORM_NAME, POMADE_SIGNERS} from "@app/env"
2026-06-08 21:14:40 +00:00
import {pushToast} from "@app/toast"
import {setChecked} from "@app/notifications"
2026-07-28 16:16:26 +00:00
import {login} from "@app/core"
2024-08-09 23:22:09 +00:00
let signers: any[] = $state([])
let loading: string | undefined = $state()
2025-12-18 02:43:13 +00:00
const hasPomade = POMADE_SIGNERS.length >= 3
const disabled = $derived(loading ? true : undefined)
2024-08-16 22:17:30 +00:00
const signUp = () => pushModal(SignUp)
2024-08-09 23:22:09 +00:00
2025-10-06 18:23:19 +00:00
const onSuccess = async (session: Session) => {
2026-07-28 16:16:26 +00:00
await login(session)
2024-11-14 18:53:13 +00:00
setChecked("*")
2024-10-15 22:52:30 +00:00
clearModals()
2024-08-22 20:17:21 +00:00
}
2025-02-03 23:40:00 +00:00
const loginWithNip07 = async () => {
2025-02-03 23:50:19 +00:00
loading = "nip07"
2025-02-03 23:40:00 +00:00
try {
const pubkey = await getNip07()?.getPublicKey()
2024-08-09 23:22:09 +00:00
2025-02-03 23:40:00 +00:00
if (pubkey) {
2026-07-28 16:16:26 +00:00
await onSuccess(toSession(nip07, {}))
2025-02-03 23:40:00 +00:00
} else {
pushToast({
theme: "error",
message: "Something went wrong! Please try again.",
})
}
} finally {
loading = undefined
2024-08-09 23:22:09 +00:00
}
2025-02-03 23:40:00 +00:00
}
2026-07-28 16:16:26 +00:00
const loginWithNip55 = async (signerApp: any) => {
2025-02-03 23:50:19 +00:00
loading = "nip55"
2024-08-09 23:22:09 +00:00
2025-02-03 23:40:00 +00:00
try {
2026-07-28 16:16:26 +00:00
const signer = new Nip55Signer(signerApp.packageName)
2025-02-03 23:40:00 +00:00
const pubkey = await signer.getPubkey()
2024-10-21 21:09:57 +00:00
2025-02-03 23:40:00 +00:00
if (pubkey) {
2026-07-28 16:16:26 +00:00
await onSuccess(toSession(nip55, {pubkey, signer: signerApp.packageName}))
2025-02-03 23:40:00 +00:00
} else {
pushToast({
theme: "error",
message: "Something went wrong! Please try again.",
})
}
} finally {
loading = undefined
2024-10-21 21:09:57 +00:00
}
2025-02-03 23:40:00 +00:00
}
2024-10-21 21:09:57 +00:00
2025-12-18 02:43:13 +00:00
const loginWithEmail = () => pushModal(LogInEmail)
2024-11-27 00:39:49 +00:00
2024-10-29 21:40:37 +00:00
const loginWithBunker = () => pushModal(LogInBunker)
2026-01-16 22:17:38 +00:00
const loginWithKey = () => pushModal(LogInKey)
const hasSigner = $derived(getNip07() || signers.length > 0)
2024-10-21 21:09:57 +00:00
onMount(async () => {
if (Capacitor.isNativePlatform()) {
signers = await getNip55()
}
2024-10-21 21:09:57 +00:00
})
2024-08-09 23:22:09 +00:00
</script>
2026-01-30 22:14:12 +00:00
<Modal>
<ModalBody>
<h1 class="heading">Log in with Nostr</h1>
<p class="m-auto max-w-sm text-center">
{PLATFORM_NAME} is built using the
<Button class="link" onclick={() => pushModal(InfoNostr)}>nostr protocol</Button>, which
allows you to own your social identity.
</p>
{#if getNip07()}
<Button {disabled} onclick={loginWithNip07} class="button button-primary">
2026-01-30 22:14:12 +00:00
{#if loading === "nip07"}
<Spinner size="sm" class="mr-3" />
2026-01-30 22:14:12 +00:00
{:else}
<Icon icon={Widget} />
{/if}
Log in with Extension
</Button>
{/if}
2026-07-28 16:16:26 +00:00
{#each signers as signerApp}
<Button {disabled} class="button button-primary" onclick={() => loginWithNip55(signerApp)}>
2026-01-30 22:14:12 +00:00
{#if loading === "nip55"}
<Spinner size="sm" class="mr-3" />
2026-01-30 22:14:12 +00:00
{:else}
2026-07-28 16:16:26 +00:00
<img src={signerApp.iconUrl} alt={signerApp.name} width="20" height="20" />
2026-01-30 22:14:12 +00:00
{/if}
2026-07-28 16:16:26 +00:00
Log in with {signerApp.name}
2026-01-30 22:14:12 +00:00
</Button>
{/each}
{#if hasPomade && !hasSigner}
<Button {disabled} onclick={loginWithEmail} class="button button-primary">
2026-01-30 22:14:12 +00:00
<Icon icon={Letter} />
Log in with Email
</Button>
{/if}
<Button
onclick={loginWithBunker}
{disabled}
class={cx(`button button-${hasSigner || hasPomade ? "neutral" : "primary"}`)}>
2026-01-30 22:14:12 +00:00
<Icon icon={Cpu} />
Log in with Remote Signer
</Button>
{#if hasPomade && hasSigner}
2026-06-30 00:20:55 +00:00
<Button {disabled} onclick={loginWithEmail} class="button button-neutral">
2026-01-30 22:14:12 +00:00
<Icon icon={Letter} />
Log in with Email
</Button>
{/if}
{#if !hasSigner}
<Button {disabled} onclick={loginWithKey} class="button button-neutral">
2026-01-30 22:14:12 +00:00
<Icon icon={Key} />
Log in with Key
</Button>
{/if}
{#if !hasSigner || !hasPomade}
<Link
external
{disabled}
href="https://nostrapps.com#signers"
class="button {hasSigner || hasPomade ? '' : 'button-neutral'}">
2026-01-30 22:14:12 +00:00
<Icon icon={Compass} />
Browse Signer Apps
</Link>
{/if}
<div class="text-sm">
Need an account?
<Button class="link" onclick={signUp}>Register instead</Button>
</div>
</ModalBody>
</Modal>