misc: add account logging in

This commit is contained in:
Xyphyn committed 2023-07-03 14:57:53 -07:00
1 parent 7d3f29d9eb
commit f71ad4be8e
21 files changed
+686 -151

No files matched your search

+43 -5
View File
@@ -1,13 +1,51 @@
import { LemmyHttp, type LoginResponse } from 'lemmy-js-client'
import {
type GetPersonDetailsResponse,
LemmyHttp,
type LoginResponse,
} from 'lemmy-js-client'
import { writable } from 'svelte/store'
import { PUBLIC_PROXY_URL } from '$env/static/public'
export const DEFAULT_INSTANCE_URL = 'lemmy.world'
export const DEFAULT_INSTANCE_URL = 'programming.dev'
export let instance_url = writable(DEFAULT_INSTANCE_URL)
export function getClient(instance: string = `lemmy.world`): LemmyHttp {
export function getClient(instance: string = `programming.dev`): LemmyHttp {
return new LemmyHttp(`${PUBLIC_PROXY_URL}/cors/${instance}`)
}
export const token = writable<LoginResponse>()
export interface AuthData {
token: string
username: string
instance: string
}
export const authData = writable<AuthData | undefined>()
export const user = writable<GetPersonDetailsResponse>()
if (typeof localStorage != 'undefined') {
if (localStorage.getItem('user')) {
try {
authData.set(JSON.parse(localStorage.getItem('user') ?? ''))
} catch (error) {
localStorage.removeItem('user')
}
}
}
authData.subscribe(async (data) => {
if (!data) return
try {
const person = await getClient().getPersonDetails({
auth: data?.token,
username: `${data?.username}@${data?.instance}`,
})
user.set(person)
if (typeof localStorage != 'undefined') {
localStorage.setItem('user', JSON.stringify(data))
}
} catch (error) {
authData.set(undefined)
}
})