|
|
|
|
@ -45,6 +45,9 @@ function persistRelays(
@@ -45,6 +45,9 @@ function persistRelays(
|
|
|
|
|
inboxes: Set<NDKRelay>, |
|
|
|
|
outboxes: Set<NDKRelay>, |
|
|
|
|
): void { |
|
|
|
|
// Only access localStorage on client-side
|
|
|
|
|
if (typeof window === 'undefined') return; |
|
|
|
|
|
|
|
|
|
localStorage.setItem( |
|
|
|
|
getRelayStorageKey(user, "inbox"), |
|
|
|
|
JSON.stringify(Array.from(inboxes).map((relay) => relay.url)), |
|
|
|
|
@ -56,6 +59,11 @@ function persistRelays(
@@ -56,6 +59,11 @@ function persistRelays(
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function getPersistedRelays(user: NDKUser): [Set<string>, Set<string>] { |
|
|
|
|
// Only access localStorage on client-side
|
|
|
|
|
if (typeof window === 'undefined') { |
|
|
|
|
return [new Set<string>(), new Set<string>()]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const inboxes = new Set<string>( |
|
|
|
|
JSON.parse(localStorage.getItem(getRelayStorageKey(user, "inbox")) ?? "[]"), |
|
|
|
|
); |
|
|
|
|
@ -135,6 +143,9 @@ async function getUserPreferredRelays(
@@ -135,6 +143,9 @@ async function getUserPreferredRelays(
|
|
|
|
|
export const loginMethodStorageKey = "alexandria/login/method"; |
|
|
|
|
|
|
|
|
|
function persistLogin(user: NDKUser, method: "extension" | "amber" | "npub") { |
|
|
|
|
// Only access localStorage on client-side
|
|
|
|
|
if (typeof window === 'undefined') return; |
|
|
|
|
|
|
|
|
|
localStorage.setItem(loginStorageKey, user.pubkey); |
|
|
|
|
localStorage.setItem(loginMethodStorageKey, method); |
|
|
|
|
} |
|
|
|
|
@ -212,7 +223,10 @@ export async function loginWithExtension() {
@@ -212,7 +223,10 @@ export async function loginWithExtension() {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
clearLogin(); |
|
|
|
|
localStorage.removeItem("alexandria/logout/flag"); |
|
|
|
|
// Only access localStorage on client-side
|
|
|
|
|
if (typeof window !== 'undefined') { |
|
|
|
|
localStorage.removeItem("alexandria/logout/flag"); |
|
|
|
|
} |
|
|
|
|
persistLogin(user, "extension"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -279,7 +293,10 @@ export async function loginWithAmber(amberSigner: NDKSigner, user: NDKUser) {
@@ -279,7 +293,10 @@ export async function loginWithAmber(amberSigner: NDKSigner, user: NDKUser) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
clearLogin(); |
|
|
|
|
localStorage.removeItem("alexandria/logout/flag"); |
|
|
|
|
// Only access localStorage on client-side
|
|
|
|
|
if (typeof window !== 'undefined') { |
|
|
|
|
localStorage.removeItem("alexandria/logout/flag"); |
|
|
|
|
} |
|
|
|
|
persistLogin(user, "amber"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -363,7 +380,10 @@ export async function loginWithNpub(pubkeyOrNpub: string) {
@@ -363,7 +380,10 @@ export async function loginWithNpub(pubkeyOrNpub: string) {
|
|
|
|
|
userPubkey.set(user.pubkey); |
|
|
|
|
|
|
|
|
|
clearLogin(); |
|
|
|
|
localStorage.removeItem("alexandria/logout/flag"); |
|
|
|
|
// Only access localStorage on client-side
|
|
|
|
|
if (typeof window !== 'undefined') { |
|
|
|
|
localStorage.removeItem("alexandria/logout/flag"); |
|
|
|
|
} |
|
|
|
|
persistLogin(user, "npub"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -373,47 +393,51 @@ export async function loginWithNpub(pubkeyOrNpub: string) {
@@ -373,47 +393,51 @@ export async function loginWithNpub(pubkeyOrNpub: string) {
|
|
|
|
|
export function logoutUser() { |
|
|
|
|
console.log("Logging out user..."); |
|
|
|
|
const currentUser = get(userStore); |
|
|
|
|
if (currentUser.ndkUser) { |
|
|
|
|
// Clear persisted relays for the user
|
|
|
|
|
localStorage.removeItem(getRelayStorageKey(currentUser.ndkUser, "inbox")); |
|
|
|
|
localStorage.removeItem(getRelayStorageKey(currentUser.ndkUser, "outbox")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Only access localStorage on client-side
|
|
|
|
|
if (typeof window !== 'undefined') { |
|
|
|
|
if (currentUser.ndkUser) { |
|
|
|
|
// Clear persisted relays for the user
|
|
|
|
|
localStorage.removeItem(getRelayStorageKey(currentUser.ndkUser, "inbox")); |
|
|
|
|
localStorage.removeItem(getRelayStorageKey(currentUser.ndkUser, "outbox")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Clear all possible login states from localStorage
|
|
|
|
|
clearLogin(); |
|
|
|
|
// Clear all possible login states from localStorage
|
|
|
|
|
clearLogin(); |
|
|
|
|
|
|
|
|
|
// Also clear any other potential login keys that might exist
|
|
|
|
|
const keysToRemove = []; |
|
|
|
|
for (let i = 0; i < localStorage.length; i++) { |
|
|
|
|
const key = localStorage.key(i); |
|
|
|
|
if ( |
|
|
|
|
key && |
|
|
|
|
(key.includes("login") || |
|
|
|
|
key.includes("nostr") || |
|
|
|
|
key.includes("user") || |
|
|
|
|
key.includes("alexandria") || |
|
|
|
|
key === "pubkey") |
|
|
|
|
) { |
|
|
|
|
keysToRemove.push(key); |
|
|
|
|
// Also clear any other potential login keys that might exist
|
|
|
|
|
const keysToRemove = []; |
|
|
|
|
for (let i = 0; i < localStorage.length; i++) { |
|
|
|
|
const key = localStorage.key(i); |
|
|
|
|
if ( |
|
|
|
|
key && |
|
|
|
|
(key.includes("login") || |
|
|
|
|
key.includes("nostr") || |
|
|
|
|
key.includes("user") || |
|
|
|
|
key.includes("alexandria") || |
|
|
|
|
key === "pubkey") |
|
|
|
|
) { |
|
|
|
|
keysToRemove.push(key); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Specifically target the login storage key
|
|
|
|
|
keysToRemove.push("alexandria/login/pubkey"); |
|
|
|
|
keysToRemove.push("alexandria/login/method"); |
|
|
|
|
// Specifically target the login storage key
|
|
|
|
|
keysToRemove.push("alexandria/login/pubkey"); |
|
|
|
|
keysToRemove.push("alexandria/login/method"); |
|
|
|
|
|
|
|
|
|
keysToRemove.forEach((key) => { |
|
|
|
|
console.log("Removing localStorage key:", key); |
|
|
|
|
localStorage.removeItem(key); |
|
|
|
|
}); |
|
|
|
|
keysToRemove.forEach((key) => { |
|
|
|
|
console.log("Removing localStorage key:", key); |
|
|
|
|
localStorage.removeItem(key); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// Clear Amber-specific flags
|
|
|
|
|
localStorage.removeItem("alexandria/amber/fallback"); |
|
|
|
|
// Clear Amber-specific flags
|
|
|
|
|
localStorage.removeItem("alexandria/amber/fallback"); |
|
|
|
|
|
|
|
|
|
// Set a flag to prevent auto-login on next page load
|
|
|
|
|
localStorage.setItem("alexandria/logout/flag", "true"); |
|
|
|
|
// Set a flag to prevent auto-login on next page load
|
|
|
|
|
localStorage.setItem("alexandria/logout/flag", "true"); |
|
|
|
|
|
|
|
|
|
console.log("Cleared all login data from localStorage"); |
|
|
|
|
console.log("Cleared all login data from localStorage"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
userStore.set({ |
|
|
|
|
pubkey: null, |
|
|
|
|
|