feat: multiple guest account support

This commit is contained in:
Xylight committed 2024-06-01 12:41:43 -07:00
1 parent e90d484755
commit c172327923
7 files changed
+192 -90

No files matched your search

+71 -38
View File
@@ -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<ProfileData>(
getFromStorage<ProfileData>('profileData') ?? { profiles: [], profile: -1 }
getFromStorage<ProfileData>('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()
})
@@ -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"
/>
<span
class="flex flex-col gap-0 {$profile?.id == prof.id ? 'font-semibold' : ''}"
slot="label"
class="inline-flex flex-col gap-0 {$profile?.id == prof.id
? 'font-semibold'
: ''}"
>
{prof.username ?? prof.user?.local_user_view.person.name}
{#if !guest}
@@ -56,4 +58,7 @@
</span>
{/if}
</span>
{#if !prof.jwt}
<Icon src={QuestionMarkCircle} size="14" micro class="ml-auto opacity-50" />
{/if}
</SidebarButton>
@@ -77,15 +77,6 @@
<ProfileButton {index} {prof} />
</div>
{/each}
<ProfileButton
index={0}
prof={{
id: -1,
instance: $profileData?.defaultInstance ?? DEFAULT_INSTANCE_URL,
username: 'Guest',
}}
guest
/>
<SidebarButton href="/accounts" icon={UserGroup}>
<span slot="label">Accounts</span>
</SidebarButton>
+8 -39
View File
@@ -293,48 +293,17 @@
</Menu>
</div>
{/each}
<div class="flex flex-row gap-4 items-center py-4">
<span class="font-bold flex flex-col">Guest</span>
<div class="ml-auto" />
<Button
aria-label={$currentProfile?.id != -1 ? 'Switch' : 'Current'}
on:click={async () => {
if ($currentProfile?.id != -1) setUserID(-1)
}}
size="square-md"
color={$currentProfile?.id == -1 ? 'primary' : 'secondary'}
>
{#if $currentProfile?.id == -1}
<Icon src={Check} mini size="16" />
{/if}
</Button>
</div>
</EditableList>
<div class="mt-auto" />
<div class="flex flex-row font-normal gap-2">
<TextInput
on:change={changeGuestInstance}
placeholder="Instance URL"
label="Guest instance"
bind:value={newInstance}
disabled={LINKED_INSTANCE_URL != undefined}
size="sm"
>
<span slot="prefix">https://</span>
</TextInput>
<Button
color="primary"
{loading}
disabled={loading || LINKED_INSTANCE_URL != undefined}
class="self-end"
>
Change
<div class="flex items-center gap-2">
<Button href="/login" size="lg" class="flex-1">
<Icon slot="prefix" src={ArrowLeftOnRectangle} size="16" mini />
Log In
</Button>
<Button href="/login/guest" size="lg">
<Icon slot="prefix" src={Plus} size="16" mini />
Add Guest
</Button>
</div>
<Button href="/login" size="lg">
<Icon slot="prefix" src={ArrowLeftOnRectangle} size="16" mini />
Log In
</Button>
</div>
{/if}
+10 -1
View File
@@ -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 @@
<Icon src={QuestionMarkCircle} mini size="16" />
Forgot Password
</Button>
<Button color="tertiary" href="/login/guest">
<Icon src={UserCircle} mini size="16" />
Guest
</Button>
</div>
</form>
</div>
+95
View File
@@ -0,0 +1,95 @@
<script lang="ts">
import { site, validateInstance } from '$lib/lemmy.js'
import { Button, Note, TextInput, toast } from 'mono-svelte'
import { MINIMUM_VERSION } from '$lib/version'
import { mayBeIncompatible } from '$lib/lemmy'
import { DOMAIN_REGEX_FORMS } from '$lib/util'
import { profile, profileData, type Profile } from '$lib/auth'
import { LINKED_INSTANCE_URL } from '$lib/instance'
let form = {
instance: '',
username: 'Guest',
loading: false,
}
async function addGuest() {
form.loading = true
if (!(await validateInstance(form.instance))) {
toast({ content: 'Failed to contact that instance URL.', type: 'error' })
form.loading = false
return
}
profileData.update((pd) => {
// too lazy to make a decent system
const id = Math.floor(Math.random() * 100000)
const newProfile: Profile = {
id: id,
instance: form.instance,
username: form.username,
}
profile.set({
...newProfile,
})
return {
profile: id,
profiles: [newProfile, ...pd.profiles],
}
})
toast({ content: 'Added that account.', type: 'success' })
form.loading = false
}
</script>
<div class="max-w-xl w-full mx-auto h-max my-auto">
<form on:submit|preventDefault={addGuest} class="flex flex-col gap-5">
<div class="flex flex-col gap-2">
<h1 class="font-bold text-3xl">Add Guest</h1>
{#if $site && mayBeIncompatible(MINIMUM_VERSION, $site.version.replace('v', ''))}
<Note>
This version of Photon supports instances running
<span style="font-family: monospace;">
v{MINIMUM_VERSION}
</span>
or higher.
</Note>
{/if}
</div>
<div class="inline-flex items-center gap-2">
<TextInput
required
label="Name"
bind:value={form.username}
placeholder="Guest 2"
minlength={1}
class="flex-1"
></TextInput>
{#if !LINKED_INSTANCE_URL}
<TextInput
required
label="Instance URL"
bind:value={form.instance}
pattern={DOMAIN_REGEX_FORMS}
placeholder="example.com"
class="flex-1"
/>
{/if}
</div>
<Button
submit
class="w-full"
color="primary"
size="lg"
loading={form.loading}
disabled={form.loading}
>
Submit
</Button>
</form>
</div>
@@ -1,5 +1,5 @@
<div
class="flex flex-col divide-y divide-zinc-800 [&>*]:py-3 first:p-0 last:py-0"
class="flex flex-col divide-y divide-slate-200 dark:divide-zinc-800 [&>*]:py-3 first:p-0 last:py-0"
>
<slot />
</div>