flotilla/src/app/components/LogIn.svelte

175 lines
5.3 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"
import {makeSecret, getNip07, Nip46Broker, getNip55, Nip55Signer} from "@welshman/signer"
2024-08-30 23:25:41 +00:00
import {addSession, loadHandle, nip46Perms, type Session} from "@welshman/app"
2024-08-16 17:50:38 +00:00
import Icon from "@lib/components/Icon.svelte"
2024-10-14 19:35:08 +00:00
import Tippy from "@lib/components/Tippy.svelte"
2024-08-16 17:50:38 +00:00
import Button from "@lib/components/Button.svelte"
2024-10-14 19:35:08 +00:00
import Divider from "@lib/components/Divider.svelte"
2024-08-16 17:50:38 +00:00
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"
import LogInInfoRemoteSigner from "@app/components/LogInInfoRemoteSigner.svelte"
import LogInKey from "@app/components/LogInKey.svelte"
2024-10-15 22:52:30 +00:00
import {pushModal, clearModals} from "@app/modal"
2024-10-22 18:04:40 +00:00
import {PLATFORM_NAME} from "@app/state"
2024-08-16 17:50:38 +00:00
import {pushToast} from "@app/toast"
2024-08-16 19:42:00 +00:00
import {loadUserData} from "@app/commands"
2024-08-09 23:22:09 +00:00
2024-08-16 22:17:30 +00:00
const signUp = () => pushModal(SignUp)
2024-08-09 23:22:09 +00:00
2024-08-22 20:17:21 +00:00
const withLoading = (cb: () => void) => async () => {
loading = true
try {
await cb()
} finally {
loading = false
}
}
2024-08-26 21:43:43 +00:00
const onSuccess = async (session: Session, relays: string[] = []) => {
2024-08-22 20:17:21 +00:00
addSession(session)
2024-09-11 18:09:25 +00:00
await loadUserData(session.pubkey, {relays})
2024-08-22 20:17:21 +00:00
pushToast({message: "Successfully logged in!"})
2024-10-15 22:52:30 +00:00
clearModals()
2024-08-22 20:17:21 +00:00
}
const loginWithNip46 = withLoading(async () => {
2024-10-14 19:35:08 +00:00
const rootHandle = await loadHandle(`_@${domain}`)
if (!rootHandle?.pubkey) {
return pushToast({
theme: "error",
message: "Sorry, we couldn't find that remote signer.",
})
}
2024-08-09 23:22:09 +00:00
const secret = makeSecret()
2024-10-15 19:02:11 +00:00
const {pubkey, nip46, relays = []} = (await loadHandle(`${username}@${domain}`)) || {}
2024-08-09 23:22:09 +00:00
2024-10-14 19:35:08 +00:00
if (!pubkey) {
2024-08-09 23:22:09 +00:00
return pushToast({
theme: "error",
2024-08-16 17:50:38 +00:00
message: "Sorry, it looks like you don't have an account yet. Try signing up instead.",
2024-08-09 23:22:09 +00:00
})
}
2024-10-14 19:35:08 +00:00
const handler = {
domain,
pubkey: rootHandle.pubkey,
relays: rootHandle.nip46 || rootHandle.relays || nip46 || relays,
}
2024-08-09 23:22:09 +00:00
const broker = Nip46Broker.get(pubkey, secret, handler)
2024-08-16 22:17:30 +00:00
if (await broker.connect("", nip46Perms)) {
2024-08-26 21:43:43 +00:00
await onSuccess({method: "nip46", pubkey, secret, handler}, relays)
2024-08-09 23:22:09 +00:00
} else {
pushToast({
theme: "error",
2024-08-16 17:50:38 +00:00
message: "Something went wrong! Please try again.",
2024-08-09 23:22:09 +00:00
})
}
2024-08-22 20:17:21 +00:00
})
2024-08-09 23:22:09 +00:00
2024-08-22 20:17:21 +00:00
const loginWithNip07 = withLoading(async () => {
const pubkey = await getNip07()?.getPublicKey()
2024-08-09 23:22:09 +00:00
2024-08-22 20:17:21 +00:00
if (pubkey) {
await onSuccess({method: "nip07", pubkey})
2024-08-22 20:17:21 +00:00
} else {
pushToast({
theme: "error",
message: "Something went wrong! Please try again.",
})
2024-08-09 23:22:09 +00:00
}
2024-08-22 20:17:21 +00:00
})
2024-08-09 23:22:09 +00:00
2024-10-21 21:09:57 +00:00
const loginWithSigner = async (app: any) => {
const signer = new Nip55Signer(app.packageName)
const pubkey = await signer.getPubkey()
if (pubkey) {
await onSuccess({method: "nip55", pubkey, signer: app.packageName})
} else {
pushToast({
theme: "error",
message: "Something went wrong! Please try again.",
})
}
}
const loginWithKey = () => pushModal(LogInKey)
2024-08-09 23:22:09 +00:00
let username = ""
2024-10-15 19:02:11 +00:00
let domain = "nsec.app"
2024-08-09 23:22:09 +00:00
let loading = false
2024-10-21 21:09:57 +00:00
let signers: any[] = []
onMount(async () => {
signers = await getNip55()
})
2024-08-09 23:22:09 +00:00
</script>
2024-08-22 20:17:21 +00:00
<form class="column gap-4" on:submit|preventDefault={loginWithNip46}>
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
2024-08-16 22:17:30 +00:00
<Button class="link" on:click={() => pushModal(InfoNostr)}>nostr protocol</Button>, which allows
you to own your social identity.
</p>
2024-10-15 19:02:11 +00:00
<div class="grid grid-cols-3 items-center gap-3">
2024-10-14 19:35:08 +00:00
<p class="font-bold">Username</p>
2024-10-15 19:02:11 +00:00
<label class="input input-bordered col-span-2 flex w-full items-center gap-2">
2024-10-14 19:35:08 +00:00
<Icon icon="user-circle" />
2024-10-15 19:02:11 +00:00
<input
bind:value={username}
disabled={loading}
class="grow"
type="text"
placeholder="username" />
2024-10-14 19:35:08 +00:00
</label>
<Tippy component={LogInInfoRemoteSigner} params={{interactive: true}}>
2024-10-15 19:02:11 +00:00
<p class="flex cursor-pointer items-center gap-2 font-bold">
2024-10-14 19:35:08 +00:00
Remote Signer
<Icon icon="info-circle" class="opacity-50" />
</p>
</Tippy>
2024-10-15 19:02:11 +00:00
<label class="input input-bordered col-span-2 flex w-full items-center gap-2">
2024-10-14 19:35:08 +00:00
<Icon icon="key-minimalistic-square-3" />
<input bind:value={domain} disabled={loading} class="grow" type="text" />
</label>
</div>
<Button type="submit" class="btn btn-primary flex-grow" disabled={!username || loading}>
<Spinner {loading}>Log In</Spinner>
<Icon icon="alt-arrow-right" />
</Button>
<Divider>Or</Divider>
{#if getNip07()}
2024-10-15 19:02:11 +00:00
<Button
disabled={loading}
on:click={loginWithNip07}
class="btn {username ? 'btn-neutral' : 'btn-primary'}">
2024-10-14 19:35:08 +00:00
<Icon icon="widget" />
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={loading} class="btn btn-neutral" on:click={() => loginWithSigner(app)}>
<img src={app.iconUrl} alt={app.name} width="48" height="48" />
Log in with {app.name}
</Button>
{/each}
<Button disabled={loading} on:click={loginWithKey} class="btn btn-neutral">
2024-10-14 19:35:08 +00:00
<Icon icon="key" />
Log in with Key
</Button>
<div class="text-sm">
Need an account?
<Button class="link" on:click={signUp}>Register instead</Button>
2024-08-09 23:22:09 +00:00
</div>
</form>