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
}
const jwt = auth ? auth : profile.current?.jwt
const jwt = auth ?? profile.current?.jwt
const headers = jwt ? { authorization: `Bearer ${jwt}` } : {}
@@ -95,11 +95,18 @@ export function getClient(
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
try {
await getClient(instance).getSite()
await client({
instanceURL: instance,
clientType: type,
auth: '',
}).getSite()
return true
} catch {
+17 -6
View File
@@ -129,9 +129,12 @@ export class PiefedClient implements BaseClient {
}
}
async likePost(
...params: Parameters<BaseClient['likePost']>
params: Parameters<BaseClient['likePost']>[0],
): ReturnType<BaseClient['likePost']> {
throw new Error('Unimplemented')
// TODO remove any
return (await this.#client.POST('/post/like', {
body: { ...params },
})) as any
}
async generateTotpSecret(
@@ -225,9 +228,14 @@ export class PiefedClient implements BaseClient {
throw new Error('Unimplemented')
}
async markPostAsRead(
...params: Parameters<BaseClient['markPostAsRead']>
params: Parameters<BaseClient['markPostAsRead']>[0],
): 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(
...params: Parameters<BaseClient['hidePost']>
@@ -300,9 +308,12 @@ export class PiefedClient implements BaseClient {
throw new Error('Unimplemented')
}
async likeComment(
...params: Parameters<BaseClient['likeComment']>
params: Parameters<BaseClient['likeComment']>[0],
): ReturnType<BaseClient['likeComment']> {
throw new Error('Unimplemented')
// TODO remove any
return (await this.#client.POST('/comment/like', {
body: { ...params },
})) as any
}
async listCommentLikes(
...params: Parameters<BaseClient['listCommentLikes']>
-1
View File
@@ -121,7 +121,6 @@
class={[
'rounded-xl rounded-l-none text-slate-600 dark:text-zinc-400',
inlineAffixes && 'bg-white dark:bg-zinc-900 pl-0',
sizeClass[size],
]}
>
{@render suffix?.()}
+1 -1
View File
@@ -93,7 +93,7 @@
</script>
{#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">
<select
{@attach attachment}
+51 -8
View File
@@ -3,6 +3,14 @@
import { goto } from '$app/navigation'
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, {
clearErrorScope,
pushError,
@@ -13,18 +21,16 @@
DEFAULT_INSTANCE_URL,
LINKED_INSTANCE_URL,
} from '$lib/instance.svelte.js'
import { getClient, mayBeIncompatible, site } from '$lib/client/lemmy.svelte'
import { errorMessage } from '$lib/lemmy/error'
import { DOMAIN_REGEX_FORMS } from '$lib/util.svelte.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 {
Icon,
Identification,
QuestionMarkCircle,
UserCircle,
} from 'svelte-hero-icons'
import { profile } from '$lib/auth.svelte'
interface Props {
ref?: string
@@ -34,13 +40,22 @@
let { ref = page.url.searchParams.get('redirect') ?? '/', children }: 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,
username: '',
password: '',
totp: '',
loading: false,
attempts: 0,
client: DEFAULT_CLIENT_TYPE,
})
async function logIn() {
@@ -50,14 +65,22 @@
try {
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(),
password: data.password,
totp_2fa_token: data.totp,
})
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) {
toast({ content: $t('toast.logIn'), type: 'success' })
@@ -120,12 +143,32 @@
placeholder={DEFAULT_INSTANCE_URL}
disabled={LINKED_INSTANCE_URL != undefined}
bind:value={data.instance}
class="flex-1"
class="flex-1 overflow-hidden"
required
pattern={DOMAIN_REGEX_FORMS}
autocorrect="off"
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}
</div>
<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 { t } from '$lib/i18n/translations'
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 { DOMAIN_REGEX_FORMS } from '$lib/util.svelte'
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'
interface Props {
@@ -21,15 +25,21 @@
let { ref = page.url.searchParams.get('redirect') ?? '/', children }: Props =
$props()
let form = $state({
let form = $state<{
instance: string
username: string
loading: boolean
client: ClientType
}>({
instance: '',
username: `${$t('account.guest')} ${profile.meta.profiles.filter(p => p.jwt == undefined).length + 1}`,
loading: false,
client: DEFAULT_CLIENT_TYPE,
})
async function addGuest() {
form.loading = true
if (!(await validateInstance(form.instance))) {
if (!(await validateInstance(form.instance, form.client))) {
toast({ content: $t('toast.failInstanceURL'), type: 'error' })
form.loading = false
return
@@ -40,6 +50,7 @@
id: id,
instance: form.instance,
username: form.username,
client: form.client,
})
profile.meta.profile = id
@@ -80,8 +91,28 @@
bind:value={form.instance}
pattern={DOMAIN_REGEX_FORMS}
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}
</div>
<Button