stage3: add piefed login

This commit is contained in:
James Xylight committed 2025-09-03 21:09:17 -07:00
1 parent d00e8b0d08
commit d236912510
6 files changed
+116 -25

No files matched your search

+10 -3
View File
@@ -71,7 +71,7 @@ export function client({
clientType = profile.current.client clientType = profile.current.client
} }
const jwt = auth ? auth : profile.current?.jwt const jwt = auth ?? profile.current?.jwt
const headers = jwt ? { authorization: `Bearer ${jwt}` } : {} const headers = jwt ? { authorization: `Bearer ${jwt}` } : {}
@@ -95,11 +95,18 @@ export function getClient(
return client({ instanceURL, func, auth }) return client({ instanceURL, func, auth })
} }
export async function validateInstance(instance: string): Promise<boolean> { export async function validateInstance(
instance: string,
type: ClientType,
): Promise<boolean> {
if (instance == '') return false if (instance == '') return false
try { try {
await getClient(instance).getSite() await client({
instanceURL: instance,
clientType: type,
auth: '',
}).getSite()
return true return true
} catch { } catch {
+17 -6
View File
@@ -129,9 +129,12 @@ export class PiefedClient implements BaseClient {
} }
} }
async likePost( async likePost(
...params: Parameters<BaseClient['likePost']> params: Parameters<BaseClient['likePost']>[0],
): ReturnType<BaseClient['likePost']> { ): ReturnType<BaseClient['likePost']> {
throw new Error('Unimplemented') // TODO remove any
return (await this.#client.POST('/post/like', {
body: { ...params },
})) as any
} }
async generateTotpSecret( async generateTotpSecret(
@@ -225,9 +228,14 @@ export class PiefedClient implements BaseClient {
throw new Error('Unimplemented') throw new Error('Unimplemented')
} }
async markPostAsRead( async markPostAsRead(
...params: Parameters<BaseClient['markPostAsRead']> params: Parameters<BaseClient['markPostAsRead']>[0],
): ReturnType<BaseClient['markPostAsRead']> { ): ReturnType<BaseClient['markPostAsRead']> {
throw new Error('Unimplemented') const response = await this.#client.POST('/post/mark_as_read', {
body: { ...params },
})
// TODO remove any
return response.data as any
} }
async hidePost( async hidePost(
...params: Parameters<BaseClient['hidePost']> ...params: Parameters<BaseClient['hidePost']>
@@ -300,9 +308,12 @@ export class PiefedClient implements BaseClient {
throw new Error('Unimplemented') throw new Error('Unimplemented')
} }
async likeComment( async likeComment(
...params: Parameters<BaseClient['likeComment']> params: Parameters<BaseClient['likeComment']>[0],
): ReturnType<BaseClient['likeComment']> { ): ReturnType<BaseClient['likeComment']> {
throw new Error('Unimplemented') // TODO remove any
return (await this.#client.POST('/comment/like', {
body: { ...params },
})) as any
} }
async listCommentLikes( async listCommentLikes(
...params: Parameters<BaseClient['listCommentLikes']> ...params: Parameters<BaseClient['listCommentLikes']>
-1
View File
@@ -121,7 +121,6 @@
class={[ class={[
'rounded-xl rounded-l-none text-slate-600 dark:text-zinc-400', 'rounded-xl rounded-l-none text-slate-600 dark:text-zinc-400',
inlineAffixes && 'bg-white dark:bg-zinc-900 pl-0', inlineAffixes && 'bg-white dark:bg-zinc-900 pl-0',
sizeClass[size],
]} ]}
> >
{@render suffix?.()} {@render suffix?.()}
+1 -1
View File
@@ -93,7 +93,7 @@
</script> </script>
{#snippet selectTarget(attachment: Attachment)} {#snippet selectTarget(attachment: Attachment)}
<Label text={label} customText={customLabel} class={['space-y-1 relative', clazz, baseClass]}> <Label text={label} customText={customLabel} class={['space-y-1 relative', baseClass]}>
<div class="relative" role="presentation"> <div class="relative" role="presentation">
<select <select
{@attach attachment} {@attach attachment}
+51 -8
View File
@@ -3,6 +3,14 @@
import { goto } from '$app/navigation' import { goto } from '$app/navigation'
import { page } from '$app/state' import { page } from '$app/state'
import { profile } from '$lib/auth.svelte'
import {
client,
DEFAULT_CLIENT_TYPE,
mayBeIncompatible,
site,
type ClientType,
} from '$lib/client/lemmy.svelte'
import ErrorContainer, { import ErrorContainer, {
clearErrorScope, clearErrorScope,
pushError, pushError,
@@ -13,18 +21,16 @@
DEFAULT_INSTANCE_URL, DEFAULT_INSTANCE_URL,
LINKED_INSTANCE_URL, LINKED_INSTANCE_URL,
} from '$lib/instance.svelte.js' } from '$lib/instance.svelte.js'
import { getClient, mayBeIncompatible, site } from '$lib/client/lemmy.svelte'
import { errorMessage } from '$lib/lemmy/error' import { errorMessage } from '$lib/lemmy/error'
import { DOMAIN_REGEX_FORMS } from '$lib/util.svelte.js' import { DOMAIN_REGEX_FORMS } from '$lib/util.svelte.js'
import { MINIMUM_VERSION } from '$lib/version.js' import { MINIMUM_VERSION } from '$lib/version.js'
import { Button, Note, TextInput, toast } from 'mono-svelte' import { Button, Note, Option, Select, TextInput, toast } from 'mono-svelte'
import { import {
Icon, Icon,
Identification, Identification,
QuestionMarkCircle, QuestionMarkCircle,
UserCircle, UserCircle,
} from 'svelte-hero-icons' } from 'svelte-hero-icons'
import { profile } from '$lib/auth.svelte'
interface Props { interface Props {
ref?: string ref?: string
@@ -34,13 +40,22 @@
let { ref = page.url.searchParams.get('redirect') ?? '/', children }: Props = let { ref = page.url.searchParams.get('redirect') ?? '/', children }: Props =
$props() $props()
let data = $state({ let data = $state<{
instance: string
username: string
password: string
totp: string
loading: boolean
attempts: number
client: ClientType
}>({
instance: DEFAULT_INSTANCE_URL, instance: DEFAULT_INSTANCE_URL,
username: '', username: '',
password: '', password: '',
totp: '', totp: '',
loading: false, loading: false,
attempts: 0, attempts: 0,
client: DEFAULT_CLIENT_TYPE,
}) })
async function logIn() { async function logIn() {
@@ -50,14 +65,22 @@
try { try {
data.instance = data.instance.trim() data.instance = data.instance.trim()
const response = await getClient(data.instance).login({ const response = await client({
instanceURL: data.instance,
clientType: data.client,
auth: '',
}).login({
username_or_email: data.username.trim(), username_or_email: data.username.trim(),
password: data.password, password: data.password,
totp_2fa_token: data.totp, totp_2fa_token: data.totp,
}) })
if (response?.jwt) { if (response?.jwt) {
const result = await profile.add(response.jwt, data.instance) const result = await profile.add(
response.jwt,
data.instance,
data.client,
)
if (result) { if (result) {
toast({ content: $t('toast.logIn'), type: 'success' }) toast({ content: $t('toast.logIn'), type: 'success' })
@@ -120,12 +143,32 @@
placeholder={DEFAULT_INSTANCE_URL} placeholder={DEFAULT_INSTANCE_URL}
disabled={LINKED_INSTANCE_URL != undefined} disabled={LINKED_INSTANCE_URL != undefined}
bind:value={data.instance} bind:value={data.instance}
class="flex-1" class="flex-1 overflow-hidden"
required required
pattern={DOMAIN_REGEX_FORMS} pattern={DOMAIN_REGEX_FORMS}
autocorrect="off" autocorrect="off"
autocapitalize="none" autocapitalize="none"
/> >
{#snippet suffix()}
<Select
bind:value={
() => {
if (data.client.name == 'lemmy') return 'lemmyv3'
else return 'piefedvalpha'
},
v => {
if (v == 'lemmyv3')
data.client = { name: 'lemmy', baseUrl: '/api/v3' }
else data.client = { name: 'piefed', baseUrl: '/api/alpha' }
}
}
class="border-0 rounded-none! border-l"
>
<Option value="lemmyv3">Lemmy</Option>
<Option value="piefedvalpha">Piefed</Option>
</Select>
{/snippet}
</TextInput>
{/if} {/if}
</div> </div>
<div class="flex flex-row gap-2"> <div class="flex flex-row gap-2">
+37 -6
View File
@@ -6,11 +6,15 @@
import Header from '$lib/components/ui/layout/pages/Header.svelte' import Header from '$lib/components/ui/layout/pages/Header.svelte'
import { t } from '$lib/i18n/translations' import { t } from '$lib/i18n/translations'
import { LINKED_INSTANCE_URL } from '$lib/instance.svelte' import { LINKED_INSTANCE_URL } from '$lib/instance.svelte'
import { mayBeIncompatible } from '$lib/client/lemmy.svelte' import {
DEFAULT_CLIENT_TYPE,
mayBeIncompatible,
type ClientType,
} from '$lib/client/lemmy.svelte'
import { site, validateInstance } from '$lib/client/lemmy.svelte' import { site, validateInstance } from '$lib/client/lemmy.svelte'
import { DOMAIN_REGEX_FORMS } from '$lib/util.svelte' import { DOMAIN_REGEX_FORMS } from '$lib/util.svelte'
import { MINIMUM_VERSION } from '$lib/version' import { MINIMUM_VERSION } from '$lib/version'
import { Button, Note, TextInput, toast } from 'mono-svelte' import { Button, Note, Option, Select, TextInput, toast } from 'mono-svelte'
import { profile } from '$lib/auth.svelte' import { profile } from '$lib/auth.svelte'
interface Props { interface Props {
@@ -21,15 +25,21 @@
let { ref = page.url.searchParams.get('redirect') ?? '/', children }: Props = let { ref = page.url.searchParams.get('redirect') ?? '/', children }: Props =
$props() $props()
let form = $state({ let form = $state<{
instance: string
username: string
loading: boolean
client: ClientType
}>({
instance: '', instance: '',
username: `${$t('account.guest')} ${profile.meta.profiles.filter(p => p.jwt == undefined).length + 1}`, username: `${$t('account.guest')} ${profile.meta.profiles.filter(p => p.jwt == undefined).length + 1}`,
loading: false, loading: false,
client: DEFAULT_CLIENT_TYPE,
}) })
async function addGuest() { async function addGuest() {
form.loading = true form.loading = true
if (!(await validateInstance(form.instance))) { if (!(await validateInstance(form.instance, form.client))) {
toast({ content: $t('toast.failInstanceURL'), type: 'error' }) toast({ content: $t('toast.failInstanceURL'), type: 'error' })
form.loading = false form.loading = false
return return
@@ -40,6 +50,7 @@
id: id, id: id,
instance: form.instance, instance: form.instance,
username: form.username, username: form.username,
client: form.client,
}) })
profile.meta.profile = id profile.meta.profile = id
@@ -80,8 +91,28 @@
bind:value={form.instance} bind:value={form.instance}
pattern={DOMAIN_REGEX_FORMS} pattern={DOMAIN_REGEX_FORMS}
placeholder="example.com" placeholder="example.com"
class="flex-1" class="flex-1 overflow-hidden"
/> >
{#snippet suffix()}
<Select
bind:value={
() => {
if (form.client.name == 'lemmy') return 'lemmyv3'
else return 'piefedvalpha'
},
v => {
if (v == 'lemmyv3')
form.client = { name: 'lemmy', baseUrl: '/api/v3' }
else form.client = { name: 'piefed', baseUrl: '/api/alpha' }
}
}
class="border-0 rounded-none! border-l"
>
<Option value="lemmyv3">Lemmy</Option>
<Option value="piefedvalpha">Piefed</Option>
</Select>
{/snippet}
</TextInput>
{/if} {/if}
</div> </div>
<Button <Button