flotilla/src/app/components/LogIn.svelte

157 lines
4.6 KiB
Svelte
Raw Normal View History

2024-08-09 23:22:09 +00:00
<script lang="ts">
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"
2025-04-11 18:51:15 +00:00
import {addSession, type Session, makeNip07Session, makeNip55Session} from "@welshman/app"
2025-09-10 22:26:39 +00:00
import Widget from "@assets/icons/widget-2.svg?dataurl"
import Key from "@assets/icons/key-minimalistic.svg?dataurl"
import Cpu from "@assets/icons/cpu-bolt.svg?dataurl"
import Compass from "@assets/icons/compass-big.svg?dataurl"
2024-08-16 17:50:38 +00:00
import Icon from "@lib/components/Icon.svelte"
import Link from "@lib/components/Link.svelte"
2024-08-16 17:50:38 +00:00
import Button from "@lib/components/Button.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"
2024-11-27 17:12:27 +00:00
import LogInPassword from "@app/components/LogInPassword.svelte"
import {pushModal, clearModals} from "@app/util/modal"
import {PLATFORM_NAME, BURROW_URL} from "@app/core/state"
import {pushToast} from "@app/util/toast"
import {setChecked} from "@app/util/notifications"
2024-08-09 23:22:09 +00:00
let signers: any[] = $state([])
let loading: string | undefined = $state()
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) => {
2024-12-04 00:18:36 +00:00
addSession(session)
2024-08-22 20:17:21 +00:00
pushToast({message: "Successfully logged in!"})
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) {
2025-04-11 18:51:15 +00:00
await onSuccess(makeNip07Session(pubkey))
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
}
const loginWithNip55 = async (app: 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 {
const signer = new Nip55Signer(app.packageName)
const pubkey = await signer.getPubkey()
2024-10-21 21:09:57 +00:00
2025-02-03 23:40:00 +00:00
if (pubkey) {
2025-04-11 18:51:15 +00:00
await onSuccess(makeNip55Session(pubkey, app.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
2024-11-27 17:12:27 +00:00
const loginWithPassword = () => pushModal(LogInPassword)
2024-11-27 00:39:49 +00:00
2024-10-29 21:40:37 +00:00
const loginWithBunker = () => pushModal(LogInBunker)
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>
<div class="column gap-4">
2024-08-16 22:17:30 +00:00
<h1 class="heading">Log in with Nostr</h1>
<p class="m-auto max-w-sm text-center">
2024-10-22 18:04:40 +00:00
{PLATFORM_NAME} is built using the
<Button class="link" onclick={() => pushModal(InfoNostr)}>nostr protocol</Button>, which allows
2024-08-16 22:17:30 +00:00
you to own your social identity.
</p>
2024-10-14 19:35:08 +00:00
{#if getNip07()}
<Button {disabled} onclick={loginWithNip07} class="btn btn-primary">
2024-12-04 00:18:36 +00:00
{#if loading === "nip07"}
2025-02-03 23:40:00 +00:00
<span class="loading loading-spinner mr-3"></span>
2024-11-14 00:14:05 +00:00
{:else}
2025-09-10 22:26:39 +00:00
<Icon icon={Widget} />
2024-11-14 00:14:05 +00:00
{/if}
2024-10-14 19:35:08 +00:00
Log in with Extension
2024-08-09 23:22:09 +00:00
</Button>
2024-10-14 19:35:08 +00:00
{/if}
2024-10-21 21:09:57 +00:00
{#each signers as app}
<Button {disabled} class="btn btn-primary" onclick={() => loginWithNip55(app)}>
2024-12-04 00:18:36 +00:00
{#if loading === "nip55"}
2025-02-03 23:40:00 +00:00
<span class="loading loading-spinner mr-3"></span>
2024-11-14 00:14:05 +00:00
{:else}
2024-11-15 23:16:06 +00:00
<img src={app.iconUrl} alt={app.name} width="20" height="20" />
2024-11-14 00:14:05 +00:00
{/if}
2024-10-21 21:09:57 +00:00
Log in with {app.name}
</Button>
{/each}
2024-12-04 23:11:32 +00:00
{#if BURROW_URL && !hasSigner}
<Button {disabled} onclick={loginWithPassword} class="btn btn-primary">
2024-12-04 23:11:32 +00:00
{#if loading === "password"}
2025-02-03 23:40:00 +00:00
<span class="loading loading-spinner mr-3"></span>
2024-12-04 23:11:32 +00:00
{:else}
2025-09-10 22:26:39 +00:00
<Icon icon={Key} />
2024-12-04 23:11:32 +00:00
{/if}
Log in with Password
</Button>
{/if}
<Button
onclick={loginWithBunker}
{disabled}
2024-12-04 23:11:32 +00:00
class="btn {hasSigner || BURROW_URL ? 'btn-neutral' : 'btn-primary'}">
2025-09-10 22:26:39 +00:00
<Icon icon={Cpu} />
Log in with Remote Signer
2024-10-14 19:35:08 +00:00
</Button>
2024-12-04 23:11:32 +00:00
{#if BURROW_URL && hasSigner}
<Button {disabled} onclick={loginWithPassword} class="btn">
2024-12-04 23:11:32 +00:00
{#if loading === "password"}
2025-02-03 23:40:00 +00:00
<span class="loading loading-spinner mr-3"></span>
2024-12-04 23:11:32 +00:00
{:else}
2025-09-10 22:26:39 +00:00
<Icon icon={Key} />
2024-12-04 23:11:32 +00:00
{/if}
Log in with Password
</Button>
{/if}
{#if !hasSigner || !BURROW_URL}
<Link
external
{disabled}
2024-12-04 23:11:32 +00:00
href="https://nostrapps.com#signers"
class="btn {hasSigner || BURROW_URL ? '' : 'btn-neutral'}">
2025-09-10 22:26:39 +00:00
<Icon icon={Compass} />
2024-12-04 23:11:32 +00:00
Browse Signer Apps
</Link>
{/if}
2024-10-14 19:35:08 +00:00
<div class="text-sm">
Need an account?
<Button class="link" onclick={signUp}>Register instead</Button>
2024-08-09 23:22:09 +00:00
</div>
</div>