aboutsummaryrefslogtreecommitdiff
path: root/src/lib/components/Login.svelte
blob: 0ceefe75effbd662c85117ca1315ad7931b79919 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<script lang='ts'>
  import { Avatar, Button, Popover } from 'flowbite-svelte';
  import NDK, { NDKNip07Signer, type NDKUserProfile } from '@nostr-dev-kit/ndk';
  import { signedIn, ndk } from '$lib/ndk';

  let profile: NDKUserProfile | null = null;

  const signInWithExtension = async () => {
    const signer = new NDKNip07Signer();
    const user = await signer.user();
    
    user.ndk = $ndk;
    $ndk.signer = signer;
    $ndk.activeUser = user;

    await $ndk.connect();
    profile = await user.fetchProfile();

    console.log('NDK signed in with extension and reconnected.');

    $signedIn = true;
  };

  const signInWithBunker = () => {
    console.warn('Bunker sign-in not yet implemented.');
  };
</script>

{#if $signedIn}
  <Avatar
    rounded
    class='h-6 w-6 m-4 cursor-pointer'
    src={profile?.image}
    alt={profile?.displayName}
  />
  <Popover
    class='popover-leather w-fit'
    placement='bottom'
    target='avatar'
  >
    <h3 class='text-lg font-bold'>{profile?.displayName}</h3>
    <h4 class='text-base'>@{profile?.name}</h4>
</Popover>
{:else}
  <Avatar rounded class='h-6 w-6 m-4 cursor-pointer' id='avatar' />
  <Popover
    class='popover-leather w-fit'
    placement='bottom'
    target='avatar'
  >
    <div class='w-full flex space-x-2'>
      <Button
        on:click={signInWithExtension}
      >
        Extension Sign-In
      </Button>
      <Button
        color='alternative'
        on:click={signInWithBunker}
      >
        Bunker Sign-In
      </Button>
    </div>
  </Popover>
{/if}