diff --git a/src/lib/auth.ts b/src/lib/auth.ts index a19fe4e6..3287631d 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -69,11 +69,25 @@ const getCookie = (key: string): string | undefined => { // sorry i was dying when i wrote this line // ask chatgpt or something to explain this for you - return document?.cookie?.split(';').map(c => c.trim()).find(c => c.split('=')?.[0] == key)?.split('=')?.[1] + return document?.cookie + ?.split(';') + .map((c) => c.trim()) + .find((c) => c.split('=')?.[0] == key) + ?.split('=')?.[1] } export let profileData = writable( - getFromStorage('profileData') ?? { profiles: [], profile: -1 } + getFromStorage('profileData') ?? { + profiles: [ + { + id: 1, + instance: DEFAULT_INSTANCE_URL, + username: 'Guest', + color: '#505050', + }, + ], + profile: 1, + } ) // stupid hack to get dev server working @@ -88,22 +102,45 @@ profileData.subscribe(async (pd) => { initialInstance = get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL instance?.set(get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL) } + if (pd.profiles.length == 0) { + profileData.update((pd) => ({ + ...pd, + profiles: [ + { + id: 1, + instance: DEFAULT_INSTANCE_URL, + username: 'Guest', + }, + ], + profile: 1, + })) + } }) -if (env.PUBLIC_MIGRATE_COOKIE && get(profileData).profiles.length == 0 && env.PUBLIC_INSTANCE_URL) { +if ( + env.PUBLIC_MIGRATE_COOKIE && + get(profileData).profiles.length == 0 && + env.PUBLIC_INSTANCE_URL +) { const jwt = getCookie('jwt') if (jwt) { new Promise(async () => { - const user = await userFromJwt(jwt, env.PUBLIC_INSTANCE_URL ?? '') - if (!user) return + const user = await userFromJwt(jwt, env.PUBLIC_INSTANCE_URL ?? '') + if (!user) return - const result = await setUser(jwt, env.PUBLIC_INSTANCE_URL ?? '', user?.user.local_user_view.person.name) - - if (result) - toast({ content: 'Your instance migrated to Photon, and you were logged in using a leftover cookie.', type: 'success' }) - + const result = await setUser( + jwt, + env.PUBLIC_INSTANCE_URL ?? '', + user?.user.local_user_view.person.name + ) + + if (result) + toast({ + content: + 'Your instance migrated to Photon, and you were logged in using a leftover cookie.', + type: 'success', + }) }) - } } @@ -113,37 +150,36 @@ profile.subscribe(async (p) => { if (p?.id == -1) { instance?.set(get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL) } - if (!p || !p.jwt) { - profileData.update((pd) => ({ ...pd, profile: -1 })) - return - } + if (!p) return if (p.user) return instance.set(p.instance) // fetch the user because p.user is undefined - const user = await userFromJwt(p.jwt, p.instance) - .then((user) => { - if (!user?.user) - toast({ - content: 'Your login has expired. Re-login to fix this issue.', - type: 'warning', - }) - site.set(user?.site) + if (p?.jwt) { + const user = await userFromJwt(p.jwt, p.instance) + .then((user) => { + if (!user?.user) + toast({ + content: 'Your login has expired. Re-login to fix this issue.', + type: 'warning', + }) + site.set(user?.site) - return user - }) - .catch((err) => { - toast({ content: err as any, type: 'error' }) - }) + return user + }) + .catch((err) => { + toast({ content: err as any, type: 'error' }) + }) - if (!user?.user) return + if (!user?.user) return - profile.update(() => ({ - ...p, - user: user!.user, - username: user?.user.local_user_view.person.name, - avatar: user?.user.local_user_view.person.avatar, - })) + profile.update(() => ({ + ...p, + user: user!.user, + username: user?.user.local_user_view.person.name, + avatar: user?.user.local_user_view.person.avatar, + })) + } }) export async function setUser(jwt: string, inst: string, username: string) { @@ -396,8 +432,6 @@ async function checkInbox() { ...p!, user: user, })) - - } setInterval(checkInbox, 4 * 60 * 1000) @@ -408,4 +442,3 @@ profile.subscribe((p) => { prevId = p?.id if (p.id != -1) checkInbox() }) - diff --git a/src/lib/components/ui/sidebar/ProfileButton.svelte b/src/lib/components/ui/sidebar/ProfileButton.svelte index f102097f..64c883a2 100644 --- a/src/lib/components/ui/sidebar/ProfileButton.svelte +++ b/src/lib/components/ui/sidebar/ProfileButton.svelte @@ -5,6 +5,7 @@ import SidebarButton from '$lib/components/ui/sidebar/SidebarButton.svelte' import ProfileAvatar from '$lib/lemmy/ProfileAvatar.svelte' import { Button } from 'mono-svelte' + import { Icon, QuestionMarkCircle } from 'svelte-hero-icons' export let prof: Profile export let index: number @@ -46,8 +47,9 @@ slot="icon" /> {prof.username ?? prof.user?.local_user_view.person.name} {#if !guest} @@ -56,4 +58,7 @@ {/if} + {#if !prof.jwt} + + {/if} diff --git a/src/lib/components/ui/sidebar/Sidebar.svelte b/src/lib/components/ui/sidebar/Sidebar.svelte index 6807fa54..bf37719e 100644 --- a/src/lib/components/ui/sidebar/Sidebar.svelte +++ b/src/lib/components/ui/sidebar/Sidebar.svelte @@ -77,15 +77,6 @@ {/each} - Accounts diff --git a/src/routes/accounts/+page.svelte b/src/routes/accounts/+page.svelte index 43553241..aa90e990 100644 --- a/src/routes/accounts/+page.svelte +++ b/src/routes/accounts/+page.svelte @@ -293,48 +293,17 @@ {/each} -
- Guest -
- - -
-
- - https:// - - +
-
{/if} diff --git a/src/routes/login/+page.svelte b/src/routes/login/+page.svelte index e3d0f574..77f49273 100644 --- a/src/routes/login/+page.svelte +++ b/src/routes/login/+page.svelte @@ -10,7 +10,12 @@ validateInstance, } from '$lib/lemmy.js' import { Button, TextInput } from 'mono-svelte' - import { Icon, Identification, QuestionMarkCircle } from 'svelte-hero-icons' + import { + Icon, + Identification, + QuestionMarkCircle, + UserCircle, + } from 'svelte-hero-icons' import { DOMAIN_REGEX_FORMS } from '$lib/util.js' import { MINIMUM_VERSION } from '$lib/version.js' @@ -137,6 +142,10 @@ Forgot Password +
diff --git a/src/routes/login/guest/+page.svelte b/src/routes/login/guest/+page.svelte new file mode 100644 index 00000000..a7b0ee7f --- /dev/null +++ b/src/routes/login/guest/+page.svelte @@ -0,0 +1,95 @@ + + +
+
+
+

Add Guest

+ {#if $site && mayBeIncompatible(MINIMUM_VERSION, $site.version.replace('v', ''))} + + This version of Photon supports instances running + + v{MINIMUM_VERSION} + + or higher. + + {/if} +
+
+ + {#if !LINKED_INSTANCE_URL} + + {/if} +
+ +
+
diff --git a/src/routes/profile/(local_user)/blocks/+layout.svelte b/src/routes/profile/(local_user)/blocks/+layout.svelte index 0a2abc7b..bfcd546b 100644 --- a/src/routes/profile/(local_user)/blocks/+layout.svelte +++ b/src/routes/profile/(local_user)/blocks/+layout.svelte @@ -1,5 +1,5 @@