feat: use state based auth instead of store based (#488)

* misc: switch to state for auth

* fix: class based auth not working
This commit is contained in:
Xylight authored and GitHub committed 2025-02-08 13:49:09 -08:00
1 parent 8c6ff77fac
commit 5651ad475d
83 files changed
+395 -469

No files matched your search

@@ -35,7 +35,7 @@
let data = $state(pageData)
async function unblockUser(item: PersonBlockView) {
if (!$profile?.jwt) return
if (!profile.data?.jwt) return
data.person_blocks.splice(
data.person_blocks.findIndex((i) => i.target.id == item.target.id),
@@ -51,7 +51,7 @@
}
async function unblockCommunity(item: CommunityBlockView) {
if (!$profile?.jwt) return
if (!profile.data?.jwt) return
data.community_blocks.splice(
data.community_blocks.findIndex(
@@ -70,7 +70,7 @@
}
async function unblockInstances(item: InstanceBlockView) {
if (!$profile?.jwt) return
if (!profile.data?.jwt) return
if (!data.my_user?.instance_blocks) return
data.my_user.instance_blocks.splice(
@@ -19,7 +19,7 @@
url: '/profile/password',
},
{
name: $t('form.profile.2fa.2fa'),
name: $t('form.profile.data.2fa.2fa'),
url: '/profile/password/2fa',
},
{
@@ -50,7 +50,7 @@
>
<Icon src={Key} size="24" />
</Material>
<span class="font-medium text-lg">{$t('form.profile.2fa.2fa')}</span>
<span class="font-medium text-lg">{$t('form.profile.data.2fa.2fa')}</span>
<span class="text-sm">
{data.my_user?.local_user_view.local_user.totp_2fa_enabled
? $t('common.enabled')
@@ -58,7 +58,7 @@
disabled
type="password"
value={totpLink}
label={$t('form.profile.2fa.totp')}
label={$t('form.profile.data.2fa.totp')}
>
{#snippet suffix()}
<button
@@ -73,7 +73,7 @@
</button>
{/snippet}
<span class="font-normal text-xs">
{$t('form.profile.2fa.paste')}
{$t('form.profile.data.2fa.paste')}
</span>
</TextInput>
{/if}
@@ -22,8 +22,8 @@
old_password: oldPassword,
})
if (res?.jwt) {
const { instance, username } = $profile
deleteProfile($profile.id)
const { instance, username } = profile.data
deleteProfile(profile.data.id)
await setUser(res.jwt, instance, username!)
$currentInstance = instance
@@ -47,21 +47,21 @@
<form onsubmit={preventDefault(changePassword)} class="flex flex-col gap-4">
<TextInput
bind:value={oldPassword}
label={$t('form.profile.currentPassword')}
label={$t('form.profile.data.currentPassword')}
type="password"
minlength={10}
required
/>
<TextInput
bind:value={newPassword}
label={$t('form.profile.newPassword')}
label={$t('form.profile.data.newPassword')}
type="password"
minlength={10}
required
/>
<TextInput
bind:value={newPasswordVerify}
label={$t('form.profile.verifyNewPassword')}
label={$t('form.profile.data.verifyNewPassword')}
type="password"
minlength={10}
required
@@ -70,14 +70,9 @@
delete_content: deletion.deleteContent,
})
profileData.update((pd) => {
pd.profiles.splice(
pd.profiles.findIndex((p) => pd.profile == p.id),
1,
)
return pd
})
profileData.profiles.splice(
profileData.profiles.findIndex((p) => profileData.profile == p.id),
)
setUserID(-1)
toast({
@@ -102,7 +97,7 @@
onaction={() => deleteAccount(4)}
>
{#snippet customTitle()}
<span>{$t('form.profile.deleteAccount.label')}</span>
<span>{$t('form.profile.data.deleteAccount.label')}</span>
{/snippet}
<TextInput
label={$t('form.password')}
@@ -110,10 +105,10 @@
bind:value={deletion.password}
/>
<Checkbox bind:checked={deletion.deleteContent}>
{$t('form.profile.deleteAccount.deleteContent')}
{$t('form.profile.data.deleteAccount.deleteContent')}
{#snippet description()}
<span>
{$t('form.profile.deleteAccount.warning')}
{$t('form.profile.data.deleteAccount.warning')}
</span>
{/snippet}
</Checkbox>
@@ -133,10 +128,10 @@
{$t('routes.profile.delete.warning')}
</p>
<Switch bind:checked={deletion.deleteContent}>
{$t('form.profile.deleteAccount.deleteContent')}
{$t('form.profile.data.deleteAccount.deleteContent')}
{#snippet description()}
<span>
{$t('form.profile.deleteAccount.warning')}
{$t('form.profile.data.deleteAccount.warning')}
</span>
{/snippet}
</Switch>
@@ -40,17 +40,25 @@
let bannerImage: FileList | undefined = $state()
async function save() {
if (!formData || !$profile?.jwt) return
if (!formData || !profile.data?.jwt) return
loading = true
try {
let pfp = profileImage
? await uploadImage(profileImage[0], $profile.instance, $profile.jwt)
? await uploadImage(
profileImage[0],
profile.data.instance,
profile.data.jwt,
)
: undefined
let banner = bannerImage
? await uploadImage(bannerImage[0], $profile.instance, $profile.jwt)
? await uploadImage(
bannerImage[0],
profile.data.instance,
profile.data.jwt,
)
: undefined
const res = await getClient().saveUserSettings({
@@ -81,45 +89,54 @@
{/if}
{@render children?.()}
{#if data.my_user?.local_user_view?.local_user && formData}
<TextInput label={$t('form.profile.email')} bind:value={formData.email} />
<TextInput
label={$t('form.profile.displayName')}
label={$t('form.profile.data.email')}
bind:value={formData.email}
/>
<TextInput
label={$t('form.profile.data.displayName')}
bind:value={formData.display_name}
placeholder="Optional"
/>
<MarkdownEditor
images={false}
bind:value={() => formData.bio ?? '', (v) => (formData.bio = v)}
label={$t('form.profile.bio')}
label={$t('form.profile.data.bio')}
previewButton
/>
<div class="flex gap-2 items-center *:flex-1">
<ImageInput label={$t('form.profile.avatar')} bind:files={profileImage} />
<ImageInput label={$t('form.profile.banner')} bind:files={bannerImage} />
<ImageInput
label={$t('form.profile.data.avatar')}
bind:files={profileImage}
/>
<ImageInput
label={$t('form.profile.data.banner')}
bind:files={bannerImage}
/>
</div>
<TextInput
label={$t('form.profile.matrix')}
label={$t('form.profile.data.matrix')}
bind:value={formData.matrix_user_id}
placeholder="@user:example.com"
/>
<Switch bind:checked={formData.show_nsfw}>
{$t('form.profile.showNSFW')}
{$t('form.profile.data.showNSFW')}
</Switch>
<Switch bind:checked={formData.show_scores}>
{$t('form.profile.scores')}
{$t('form.profile.data.scores')}
</Switch>
<Switch bind:checked={formData.bot_account}>
{$t('form.profile.bot')}
{$t('form.profile.data.bot')}
</Switch>
<Switch bind:checked={formData.show_bot_accounts}>
{$t('form.profile.showBots')}
{$t('form.profile.data.showBots')}
</Switch>
<Switch bind:checked={formData.show_read_posts}>
{$t('form.profile.showRead')}
{$t('form.profile.data.showRead')}
</Switch>
<div class="space-y-1">
<SectionTitle>{$t('form.profile.languages.title')}</SectionTitle>
<p>{$t('form.profile.languages.description')}</p>
<SectionTitle>{$t('form.profile.data.languages.title')}</SectionTitle>
<p>{$t('form.profile.data.languages.description')}</p>
<Material rounding="xl" color="uniform" class="dark:bg-zinc-950">
{#if $site && formData.discussion_languages}
<div class="flex gap-2 flex-wrap flex-row">
+2 -2
View File
@@ -9,8 +9,8 @@ export let ssr = false
export async function load({ params, url, fetch }) {
const my_user =
// get(profile)?.user ??
(await client({ auth: get(profile)?.jwt, func: fetch }).getSite()).my_user
// profile.data?.user ??
(await client({ auth: profile.data?.jwt, func: fetch }).getSite()).my_user
return {
my_user: my_user,
+1 -1
View File
@@ -4,7 +4,7 @@ import { error } from '@sveltejs/kit'
import { get } from 'svelte/store'
export async function load({ fetch, url }) {
const { jwt } = get(profile)
const { jwt } = profile.data
let page = Number(url.searchParams.get('page')) || 1
+1 -1
View File
@@ -14,7 +14,7 @@
let loading = $state(false)
async function deleteImage(image: LocalImage) {
if (!$profile?.jwt) return
if (!profile.data?.jwt) return
try {
loading = true