feat: multiple guest account support
This commit is contained in:
1 parent
e90d484755
commit
c172327923
7 files changed
+192
-90
No files matched your search
+71
-38
@@ -69,11 +69,25 @@ const getCookie = (key: string): string | undefined => {
|
|||||||
|
|
||||||
// sorry i was dying when i wrote this line
|
// sorry i was dying when i wrote this line
|
||||||
// ask chatgpt or something to explain this for you
|
// 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>(
|
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
|
// stupid hack to get dev server working
|
||||||
@@ -88,22 +102,45 @@ profileData.subscribe(async (pd) => {
|
|||||||
initialInstance = get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL
|
initialInstance = get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL
|
||||||
instance?.set(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')
|
const jwt = getCookie('jwt')
|
||||||
if (jwt) {
|
if (jwt) {
|
||||||
new Promise(async () => {
|
new Promise(async () => {
|
||||||
const user = await userFromJwt(jwt, env.PUBLIC_INSTANCE_URL ?? '')
|
const user = await userFromJwt(jwt, env.PUBLIC_INSTANCE_URL ?? '')
|
||||||
if (!user) return
|
if (!user) return
|
||||||
|
|
||||||
const result = await setUser(jwt, env.PUBLIC_INSTANCE_URL ?? '', user?.user.local_user_view.person.name)
|
const result = await setUser(
|
||||||
|
jwt,
|
||||||
if (result)
|
env.PUBLIC_INSTANCE_URL ?? '',
|
||||||
toast({ content: 'Your instance migrated to Photon, and you were logged in using a leftover cookie.', type: 'success' })
|
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) {
|
if (p?.id == -1) {
|
||||||
instance?.set(get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL)
|
instance?.set(get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL)
|
||||||
}
|
}
|
||||||
if (!p || !p.jwt) {
|
if (!p) return
|
||||||
profileData.update((pd) => ({ ...pd, profile: -1 }))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (p.user) return
|
if (p.user) return
|
||||||
|
|
||||||
instance.set(p.instance)
|
instance.set(p.instance)
|
||||||
// fetch the user because p.user is undefined
|
// fetch the user because p.user is undefined
|
||||||
const user = await userFromJwt(p.jwt, p.instance)
|
if (p?.jwt) {
|
||||||
.then((user) => {
|
const user = await userFromJwt(p.jwt, p.instance)
|
||||||
if (!user?.user)
|
.then((user) => {
|
||||||
toast({
|
if (!user?.user)
|
||||||
content: 'Your login has expired. Re-login to fix this issue.',
|
toast({
|
||||||
type: 'warning',
|
content: 'Your login has expired. Re-login to fix this issue.',
|
||||||
})
|
type: 'warning',
|
||||||
site.set(user?.site)
|
})
|
||||||
|
site.set(user?.site)
|
||||||
|
|
||||||
return user
|
return user
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
toast({ content: err as any, type: 'error' })
|
toast({ content: err as any, type: 'error' })
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!user?.user) return
|
if (!user?.user) return
|
||||||
|
|
||||||
profile.update(() => ({
|
profile.update(() => ({
|
||||||
...p,
|
...p,
|
||||||
user: user!.user,
|
user: user!.user,
|
||||||
username: user?.user.local_user_view.person.name,
|
username: user?.user.local_user_view.person.name,
|
||||||
avatar: user?.user.local_user_view.person.avatar,
|
avatar: user?.user.local_user_view.person.avatar,
|
||||||
}))
|
}))
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
export async function setUser(jwt: string, inst: string, username: string) {
|
export async function setUser(jwt: string, inst: string, username: string) {
|
||||||
@@ -396,8 +432,6 @@ async function checkInbox() {
|
|||||||
...p!,
|
...p!,
|
||||||
user: user,
|
user: user,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setInterval(checkInbox, 4 * 60 * 1000)
|
setInterval(checkInbox, 4 * 60 * 1000)
|
||||||
@@ -408,4 +442,3 @@ profile.subscribe((p) => {
|
|||||||
prevId = p?.id
|
prevId = p?.id
|
||||||
if (p.id != -1) checkInbox()
|
if (p.id != -1) checkInbox()
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
import SidebarButton from '$lib/components/ui/sidebar/SidebarButton.svelte'
|
import SidebarButton from '$lib/components/ui/sidebar/SidebarButton.svelte'
|
||||||
import ProfileAvatar from '$lib/lemmy/ProfileAvatar.svelte'
|
import ProfileAvatar from '$lib/lemmy/ProfileAvatar.svelte'
|
||||||
import { Button } from 'mono-svelte'
|
import { Button } from 'mono-svelte'
|
||||||
|
import { Icon, QuestionMarkCircle } from 'svelte-hero-icons'
|
||||||
|
|
||||||
export let prof: Profile
|
export let prof: Profile
|
||||||
export let index: number
|
export let index: number
|
||||||
@@ -46,8 +47,9 @@
|
|||||||
slot="icon"
|
slot="icon"
|
||||||
/>
|
/>
|
||||||
<span
|
<span
|
||||||
class="flex flex-col gap-0 {$profile?.id == prof.id ? 'font-semibold' : ''}"
|
class="inline-flex flex-col gap-0 {$profile?.id == prof.id
|
||||||
slot="label"
|
? 'font-semibold'
|
||||||
|
: ''}"
|
||||||
>
|
>
|
||||||
{prof.username ?? prof.user?.local_user_view.person.name}
|
{prof.username ?? prof.user?.local_user_view.person.name}
|
||||||
{#if !guest}
|
{#if !guest}
|
||||||
@@ -56,4 +58,7 @@
|
|||||||
</span>
|
</span>
|
||||||
{/if}
|
{/if}
|
||||||
</span>
|
</span>
|
||||||
|
{#if !prof.jwt}
|
||||||
|
<Icon src={QuestionMarkCircle} size="14" micro class="ml-auto opacity-50" />
|
||||||
|
{/if}
|
||||||
</SidebarButton>
|
</SidebarButton>
|
||||||
@@ -77,15 +77,6 @@
|
|||||||
<ProfileButton {index} {prof} />
|
<ProfileButton {index} {prof} />
|
||||||
</div>
|
</div>
|
||||||
{/each}
|
{/each}
|
||||||
<ProfileButton
|
|
||||||
index={0}
|
|
||||||
prof={{
|
|
||||||
id: -1,
|
|
||||||
instance: $profileData?.defaultInstance ?? DEFAULT_INSTANCE_URL,
|
|
||||||
username: 'Guest',
|
|
||||||
}}
|
|
||||||
guest
|
|
||||||
/>
|
|
||||||
<SidebarButton href="/accounts" icon={UserGroup}>
|
<SidebarButton href="/accounts" icon={UserGroup}>
|
||||||
<span slot="label">Accounts</span>
|
<span slot="label">Accounts</span>
|
||||||
</SidebarButton>
|
</SidebarButton>
|
||||||
|
|||||||
@@ -293,48 +293,17 @@
|
|||||||
</Menu>
|
</Menu>
|
||||||
</div>
|
</div>
|
||||||
{/each}
|
{/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>
|
</EditableList>
|
||||||
<div class="mt-auto" />
|
<div class="mt-auto" />
|
||||||
<div class="flex flex-row font-normal gap-2">
|
<div class="flex items-center gap-2">
|
||||||
<TextInput
|
<Button href="/login" size="lg" class="flex-1">
|
||||||
on:change={changeGuestInstance}
|
<Icon slot="prefix" src={ArrowLeftOnRectangle} size="16" mini />
|
||||||
placeholder="Instance URL"
|
Log In
|
||||||
label="Guest instance"
|
</Button>
|
||||||
bind:value={newInstance}
|
<Button href="/login/guest" size="lg">
|
||||||
disabled={LINKED_INSTANCE_URL != undefined}
|
<Icon slot="prefix" src={Plus} size="16" mini />
|
||||||
size="sm"
|
Add Guest
|
||||||
>
|
|
||||||
<span slot="prefix">https://</span>
|
|
||||||
</TextInput>
|
|
||||||
<Button
|
|
||||||
color="primary"
|
|
||||||
{loading}
|
|
||||||
disabled={loading || LINKED_INSTANCE_URL != undefined}
|
|
||||||
class="self-end"
|
|
||||||
>
|
|
||||||
Change
|
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
<Button href="/login" size="lg">
|
|
||||||
<Icon slot="prefix" src={ArrowLeftOnRectangle} size="16" mini />
|
|
||||||
Log In
|
|
||||||
</Button>
|
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
@@ -10,7 +10,12 @@
|
|||||||
validateInstance,
|
validateInstance,
|
||||||
} from '$lib/lemmy.js'
|
} from '$lib/lemmy.js'
|
||||||
import { Button, TextInput } from 'mono-svelte'
|
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 { DOMAIN_REGEX_FORMS } from '$lib/util.js'
|
||||||
import { MINIMUM_VERSION } from '$lib/version.js'
|
import { MINIMUM_VERSION } from '$lib/version.js'
|
||||||
|
|
||||||
@@ -137,6 +142,10 @@
|
|||||||
<Icon src={QuestionMarkCircle} mini size="16" />
|
<Icon src={QuestionMarkCircle} mini size="16" />
|
||||||
Forgot Password
|
Forgot Password
|
||||||
</Button>
|
</Button>
|
||||||
|
<Button color="tertiary" href="/login/guest">
|
||||||
|
<Icon src={UserCircle} mini size="16" />
|
||||||
|
Guest
|
||||||
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
@@ -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
|
<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 />
|
<slot />
|
||||||
</div>
|
</div>
|
||||||
Reference in new issue
Block a user