feat: Add login stuff

This commit is contained in:
Xyphyn committed 2023-07-04 13:08:58 -07:00
1 parent f71ad4be8e
commit f0523b0c3f
18 files changed
+288 -56

No files matched your search

+19 -13
View File
@@ -3,13 +3,17 @@ import {
LemmyHttp,
type LoginResponse,
} from 'lemmy-js-client'
import { writable } from 'svelte/store'
import { get, writable } from 'svelte/store'
import { PUBLIC_PROXY_URL } from '$env/static/public'
export const DEFAULT_INSTANCE_URL = 'programming.dev'
export let instance_url = writable(DEFAULT_INSTANCE_URL)
export function getClient(instance: string = `programming.dev`): LemmyHttp {
export function getClient(instance?: string): LemmyHttp {
if (!instance) {
instance = get(authData)?.instance ?? DEFAULT_INSTANCE_URL
}
return new LemmyHttp(`${PUBLIC_PROXY_URL}/cors/${instance}`)
}
@@ -19,18 +23,20 @@ export interface AuthData {
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')
export const authData = writable<AuthData | undefined>(
await (() => {
if (typeof localStorage != 'undefined') {
if (localStorage.getItem('user')) {
try {
return JSON.parse(localStorage.getItem('user') ?? '')
} catch (error) {
localStorage.removeItem('user')
}
}
}
}
}
})()
)
export const user = writable<GetPersonDetailsResponse>()
authData.subscribe(async (data) => {
if (!data) return