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

+90 -133
View File
@@ -27,7 +27,7 @@ import { t } from './translations'
const getDefaultProfile = (): Profile => ({
id: -1,
instance: get(profileData)?.defaultInstance ?? get(instance),
instance: profileData.defaultInstance ?? get(instance),
})
function getFromStorage<T>(key: string): T | undefined {
@@ -83,7 +83,7 @@ const getCookie = (key: string): string | undefined => {
?.split('=')?.[1]
}
export let profileData = writable<ProfileData>(
export let profileData = $state<ProfileData>(
getFromStorage<ProfileData>('profileData') ?? {
profiles: [
{
@@ -97,88 +97,55 @@ export let profileData = writable<ProfileData>(
},
)
let fetchUser = {
loading: false,
prevProfile: -1,
}
class CurrentProfile {
#data = $derived(
profileData.profiles.find((i) => i.id == profileData.profile) ??
getDefaultProfile(),
)
function profileStore(
data: Writable<ProfileData>,
fn: (
values: ProfileData,
set: (value: Profile) => void,
update: (fn: Updater<Profile>) => void,
) => any,
) {
const derive: Readable<Profile> = derived(data, fn)
const { subscribe } = derive
return {
subscribe,
set: (value: Profile) => {
data.update((data) => {
let index = data.profiles.findIndex((t) => t.id == value.id)
data.profiles[index] = value
return data
})
},
get data() {
return this.#data
}
set data(value) {
if (!value) return
const index = profileData.profiles.findIndex((i) => i.id == value?.id)
profileData.profiles[index] = value
}
}
export let profile: Readable<Profile> & { set: (v: Profile) => void } =
profileStore(profileData, (pd, set, update) => {
const profile =
pd.profiles.find((p) => p.id == pd.profile) ?? getDefaultProfile()
export let profile = new CurrentProfile()
instance.set(profile.instance)
set(profile)
async function fetchUserData(profile: CurrentProfile) {
instance.set(profile.data.instance)
if (profile.data.jwt) {
site.set(undefined)
notifications.set({ applications: 0, inbox: 0, reports: 0 })
if (profile?.jwt) {
if (profile.id != fetchUser.prevProfile && !fetchUser.loading) {
site.set(undefined)
const res = await userFromJwt(profile.data.jwt, profile.data.instance)
if (!res?.user)
toast({
content:
"Your account's instance did not return your user data. Your login may have expired.",
type: 'error',
})
fetchUser.loading = true
notifications.set({ applications: 0, inbox: 0, reports: 0 })
userFromJwt(profile.jwt, profile.instance)
.then((res) => {
if (!res?.user)
toast({
content:
"Your account's instance did not return your user data. Your login may have expired.",
type: 'error',
})
site.set(res?.site)
profile.user = res?.user
if (profile.user) {
profile.avatar = res?.user?.local_user_view.person.avatar
profile.username = res?.user?.local_user_view.person.name
}
fetchUser.loading = false
fetchUser.prevProfile = profile.id
checkInbox()
update(() => profile)
})
.catch((e) => {
fetchUser.loading = false
toast({ content: e, type: 'error' })
})
}
} else {
if (browser && !fetchUser.loading) {
site.set(undefined)
client({ instanceURL: profile.instance })
.getSite()
.then((res) => site.set(res))
fetchUser.loading = false
}
site.set(res?.site)
profile.data.user = res?.user
if (profile.data.user) {
profile.data.avatar = res?.user?.local_user_view.person.avatar
profile.data.username = res?.user?.local_user_view.person.name
}
})
} else {
if (browser) {
site.set(undefined)
client({ instanceURL: profile.data.instance })
.getSite()
.then((res) => site.set(res))
}
}
return profile
}
export let notifications = writable<Notifications>({
applications: 0,
@@ -186,35 +153,40 @@ export let notifications = writable<Notifications>({
reports: 0,
})
profileData.subscribe(async (pd) => {
const serialized: ProfileData = {
...pd,
profiles: pd.profiles.map((p) => serializeUser(p)),
}
$effect.root(() => {
$effect(() => {
const serialized = {
...profileData,
profiles: profileData.profiles.map((p) => serializeUser(p)),
}
setFromStorage('profileData', serialized)
setFromStorage('profileData', serialized)
if (serialized.profile == -1) {
instance?.set(get(profileData).defaultInstance ?? DEFAULT_INSTANCE_URL)
}
if (serialized.profiles.length == 0) {
profileData.update((pd) => ({
...pd,
profiles: [
if (serialized.profile == -1) {
instance?.set(profileData.defaultInstance ?? DEFAULT_INSTANCE_URL)
}
if (serialized.profiles.length == 0) {
profileData.profiles = [
{
id: 1,
instance: DEFAULT_INSTANCE_URL,
username: t.get('account.guest') || 'Guest',
},
],
profile: 1,
}))
}
]
profileData.profile = 1
}
})
$effect(() => {})
$effect(() => {
if (profile.data.id || profileData) fetchUserData(profile).then((res) => {})
})
})
if (
env.PUBLIC_MIGRATE_COOKIE &&
get(profileData).profiles.length == 0 &&
profileData.profiles.length == 0 &&
env.PUBLIC_INSTANCE_URL
) {
const jwt = getCookie('jwt')
@@ -260,22 +232,14 @@ export async function setUser(jwt: string, inst: string, username?: string) {
instance.set(inst)
profileData.update((pd) => {
// too lazy to make a decent system
const id = Math.max(...pd.profiles.map((p) => p.id)) + 1
const newProfile: Profile = {
id: id,
instance: inst,
jwt: jwt,
username: user?.user?.local_user_view.person.name,
avatar: user?.user?.local_user_view.person.avatar,
}
return {
profile: id,
profiles: [newProfile, ...pd.profiles],
}
const id = Math.max(...profileData.profiles.map((p) => p.id)) + 1
profileData.profile = id
profileData.profiles.push({
id: id,
instance: inst,
jwt: jwt,
username: user?.user?.local_user_view.person.name,
avatar: user?.user?.local_user_view.person.avatar,
})
return user
@@ -326,20 +290,15 @@ async function userFromJwt(
}
export function resetProfile() {
profileData.update((p) => ({ ...p, profile: -1 }))
profileData.profile = -1
}
export function deleteProfile(id: number) {
profileData.update((pd) => {
const index = pd.profiles.findIndex((p) => p.id == id)
if (index <= -1) return pd
pd.profiles.splice(index, 1)
if (id == pd.profile) resetProfile()
return pd
})
profileData.profiles.splice(
profileData.profiles.findIndex((p) => p.id == id),
1,
)
if (id == profileData.profile) resetProfile()
}
function serializeUser(user: Profile): Profile {
@@ -350,19 +309,19 @@ function serializeUser(user: Profile): Profile {
}
export async function setUserID(id: number) {
profileData.update((p) => ({ ...p, profile: id }))
return get(profile)
if (!profileData.profiles.find((p) => p.id == id)) return -1
profileData.profile = id
return profile
}
export function moveProfile(id: number, up: boolean) {
const pd = get(profileData)
try {
const index = pd.profiles.findIndex((i) => i.id == id)
profileData.set({
...pd,
profiles: moveItem(pd.profiles, index, index + (up ? -1 : 1)),
})
const index = profileData.profiles.findIndex((i) => i.id == id)
profileData.profiles = moveItem(
profileData.profiles,
index,
index + (up ? -1 : 1),
)
} catch (err) {
// we dont care
}
@@ -406,9 +365,7 @@ async function getNotificationCount(jwt: string, mod: boolean, admin: boolean) {
}
async function checkInbox() {
if (!get(profile)) return
const { user, jwt } = get(profile)!
const { user, jwt } = profile.data
if (!jwt || !user) return
const notifs = await getNotificationCount(