Make the login persisten between reloads
This commit is contained in:
parent
cb67093ce0
commit
2f91fe8572
2 changed files with 16 additions and 0 deletions
|
|
@ -19,6 +19,8 @@ export const auth = {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const PUBKEY_KEY = "nostr_pubkey";
|
||||||
|
|
||||||
export async function login() {
|
export async function login() {
|
||||||
if (!window.nostr) {
|
if (!window.nostr) {
|
||||||
alert(
|
alert(
|
||||||
|
|
@ -28,6 +30,7 @@ export async function login() {
|
||||||
}
|
}
|
||||||
const pubkey = await window.nostr.getPublicKey();
|
const pubkey = await window.nostr.getPublicKey();
|
||||||
signer = window.nostr;
|
signer = window.nostr;
|
||||||
|
localStorage.setItem(PUBKEY_KEY, pubkey);
|
||||||
const { loadNostrUser } = await import("@nostr/gadgets/metadata");
|
const { loadNostrUser } = await import("@nostr/gadgets/metadata");
|
||||||
user = await loadNostrUser(pubkey);
|
user = await loadNostrUser(pubkey);
|
||||||
}
|
}
|
||||||
|
|
@ -35,4 +38,13 @@ export async function login() {
|
||||||
export function logout() {
|
export function logout() {
|
||||||
user = null;
|
user = null;
|
||||||
signer = null;
|
signer = null;
|
||||||
|
localStorage.removeItem(PUBKEY_KEY);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function restoreSession() {
|
||||||
|
const pubkey = localStorage.getItem(PUBKEY_KEY);
|
||||||
|
if (!pubkey) return;
|
||||||
|
signer = window.nostr ?? null;
|
||||||
|
const { loadNostrUser } = await import("@nostr/gadgets/metadata");
|
||||||
|
user = await loadNostrUser(pubkey);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@
|
||||||
import LeftSidebar from "$lib/components/LeftSidebar.svelte";
|
import LeftSidebar from "$lib/components/LeftSidebar.svelte";
|
||||||
import ChatSidebar from "$lib/components/ChatSidebar.svelte";
|
import ChatSidebar from "$lib/components/ChatSidebar.svelte";
|
||||||
import { page } from "$app/state";
|
import { page } from "$app/state";
|
||||||
|
import { onMount } from "svelte";
|
||||||
|
import { restoreSession } from "$lib/auth.svelte";
|
||||||
|
|
||||||
let { children } = $props();
|
let { children } = $props();
|
||||||
|
|
||||||
|
|
@ -12,6 +14,8 @@
|
||||||
const mode: "simple" | "full" = "full";
|
const mode: "simple" | "full" = "full";
|
||||||
const chatEnabled = true;
|
const chatEnabled = true;
|
||||||
|
|
||||||
|
onMount(restoreSession);
|
||||||
|
|
||||||
let chatExpanded = $state(false);
|
let chatExpanded = $state(false);
|
||||||
|
|
||||||
const activeRoom = $derived(page.params.slug ?? "");
|
const activeRoom = $derived(page.params.slug ?? "");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue