feat: update login flow

This commit is contained in:
2024-01-10 09:22:13 +07:00
parent 73f90ebaf9
commit f2504071cd
17 changed files with 619 additions and 705 deletions

View File

@@ -108,10 +108,11 @@ export class Ark {
}
}
public async getUserProfile({ pubkey }: { pubkey: string }) {
public async getUserProfile(pubkey?: string) {
try {
// get clean pubkey without any special characters
let hexstring = pubkey.replace(/[^a-zA-Z0-9]/g, "").replace("nostr:", "");
const currentUserPubkey = this.#storage.account.pubkey;
if (
hexstring.startsWith("npub1") ||
@@ -125,7 +126,9 @@ export class Ark {
if (decoded.type === "naddr") hexstring = decoded.data.pubkey;
}
const user = this.ndk.getUser({ pubkey: hexstring });
const user = this.ndk.getUser({
pubkey: pubkey ? hexstring : currentUserPubkey,
});
const profile = await user.fetchProfile({
cacheUsage: NDKSubscriptionCacheUsage.CACHE_FIRST,
@@ -138,23 +141,33 @@ export class Ark {
}
}
public async getUserContacts({
pubkey = undefined,
outbox = undefined,
}: {
pubkey?: string;
outbox?: boolean;
}) {
public async getUserContacts(pubkey?: string) {
try {
const user = this.ndk.getUser({
pubkey: pubkey ? pubkey : this.#storage.account.pubkey,
});
const contacts = [...(await user.follows(undefined, outbox))].map(
(user) => user.pubkey,
);
// get clean pubkey without any special characters
let hexstring = pubkey.replace(/[^a-zA-Z0-9]/g, "").replace("nostr:", "");
const currentUserPubkey = this.#storage.account.pubkey;
if (pubkey === this.#storage.account.pubkey)
if (
hexstring.startsWith("npub1") ||
hexstring.startsWith("nprofile1") ||
hexstring.startsWith("naddr1")
) {
const decoded = nip19.decode(hexstring);
if (decoded.type === "nprofile") hexstring = decoded.data.pubkey;
if (decoded.type === "npub") hexstring = decoded.data;
if (decoded.type === "naddr") hexstring = decoded.data.pubkey;
}
const user = this.ndk.getUser({
pubkey: pubkey ? hexstring : currentUserPubkey,
});
const contacts = [...(await user.follows())].map((user) => user.pubkey);
if (!pubkey || pubkey === this.#storage.account.pubkey)
this.#storage.account.contacts = contacts;
return contacts;
} catch (e) {
throw new Error(e);
@@ -507,7 +520,10 @@ export class Ark {
signal,
});
if (!res.ok) throw new Error(`Failed to fetch NIP-05 service: ${nip05}`);
if (!res.ok) {
console.log(res);
throw new Error(`Failed to fetch NIP-05 service: ${nip05}`);
}
const data: NIP05 = await res.json();

View File

@@ -1,27 +1,27 @@
import { useQuery } from '@tanstack/react-query';
import { useArk } from '../provider';
import { useQuery } from "@tanstack/react-query";
import { useArk } from "../provider";
export function useProfile(pubkey: string) {
const ark = useArk();
const {
isLoading,
isError,
data: user,
} = useQuery({
queryKey: ['user', pubkey],
queryFn: async () => {
const profile = await ark.getUserProfile({ pubkey });
if (!profile)
throw new Error(
`Cannot get metadata for ${pubkey}, will be retry after 10 seconds`
);
return profile;
},
refetchOnMount: false,
refetchOnWindowFocus: false,
refetchOnReconnect: false,
retry: 2,
});
const ark = useArk();
const {
isLoading,
isError,
data: user,
} = useQuery({
queryKey: ["user", pubkey],
queryFn: async () => {
const profile = await ark.getUserProfile(pubkey);
if (!profile)
throw new Error(
`Cannot get metadata for ${pubkey}, will be retry after 10 seconds`,
);
return profile;
},
refetchOnMount: false,
refetchOnWindowFocus: false,
refetchOnReconnect: false,
retry: 2,
});
return { isLoading, isError, user };
return { isLoading, isError, user };
}

View File

@@ -366,6 +366,7 @@ export class LumeStorage {
const currentSetting = await this.checkSettingValue(key);
if (!currentSetting) {
this.settings[key] === !!parseInt(value);
return await this.#db.execute(
"INSERT OR IGNORE INTO settings (key, value) VALUES ($1, $2);",
[key, value],

View File

@@ -159,4 +159,9 @@ export interface NIP05 {
names: {
[key: string]: string;
};
nip46: {
[key: string]: {
[key: string]: string[];
};
};
}

View File

@@ -32,9 +32,7 @@ export function OnboardingProfileSettingsScreen() {
navigate("/follow");
}
const oldProfile = await ark.getUserProfile({
pubkey: storage.account.pubkey,
});
const oldProfile = await ark.getUserProfile();
const profile: NDKUserProfile = {
...data,
@@ -55,9 +53,6 @@ export function OnboardingProfileSettingsScreen() {
if (publish) {
setLoading(false);
navigate("/follow");
} else {
toast.error("Cannot publish your profile, please try again later.");
setLoading(false);
}
} catch (e) {
setLoading(false);