feat: add backwards compatibility for POST requests

This commit is contained in:
Xyphyn committed 2023-10-02 16:03:18 -07:00
1 parent c843d8bb45
commit 93e144a837
2 files changed
+74 -30

No files matched your search

+6 -1
View File
@@ -33,8 +33,13 @@ async function customFetch(
): Promise<Response> { ): Promise<Response> {
const f = func ? func : fetch const f = func ? func : fetch
const url = toURL(input) if (init?.body && auth) {
const json = JSON.parse(init.body.toString())
json.auth = auth
init.body = JSON.stringify(json)
}
const url = toURL(input)
if (auth && url) url.searchParams.set('auth', auth) if (auth && url) url.searchParams.set('auth', auth)
const res = await f(url ?? input, init) const res = await f(url ?? input, init)
@@ -1,12 +1,13 @@
<script lang="ts"> <script lang="ts">
import { deleteProfile, profile, setUser } from '$lib/auth.js' import { deleteProfile, profile, setUser } from '$lib/auth.js'
import { getClient } from '$lib/lemmy.js' import { getClient, site } from '$lib/lemmy.js'
import { trycatch } from '$lib/util.js' import { trycatch } from '$lib/util.js'
import { Button, Snippet, TextInput, toast } from 'mono-svelte' import { Button, Snippet, TextInput, toast } from 'mono-svelte'
import { instance as currentInstance } from '$lib/instance.js' import { instance as currentInstance } from '$lib/instance.js'
import { ClipboardDocument, Icon } from 'svelte-hero-icons' import { ClipboardDocument, Icon } from 'svelte-hero-icons'
import { goto } from '$app/navigation' import { goto } from '$app/navigation'
import { page } from '$app/stores' import { page } from '$app/stores'
import { feature } from '$lib/version.js'
export let data export let data
@@ -15,8 +16,12 @@
newPasswordVerify = '' newPasswordVerify = ''
let loading = false let loading = false
let verify_totp = ''
// @ts-ignore // @ts-ignore
$: totpLink = data.my_user?.local_user_view?.local_user?.totp_2fa_url let totpLink: string | undefined = undefined
$: totpEnabled =
data.my_user?.local_user_view.local_user.totp_2fa_enabled ?? totpLink
async function changePassword() { async function changePassword() {
try { try {
@@ -49,16 +54,28 @@
loading = false loading = false
} }
async function twofa(enabled: boolean) { async function twofa(enabled: boolean, update: boolean = false) {
if (!$profile?.jwt) return if (!$profile?.jwt) return
try { try {
await getClient().saveUserSettings({ if (feature('newTotp', $site?.version)) {
...data.my_user?.local_user_view.local_user, if (update) {
...data.my_user?.local_user_view.person, await getClient().updateTotp({
// @ts-ignore enabled: enabled,
generate_totp_2fa: enabled, totp_token: verify_totp,
}) })
} else {
const res = await getClient().generateTotpSecret()
totpLink = res.totp_secret_url
}
} else {
const res = await getClient().saveUserSettings({
...data.my_user?.local_user_view.local_user,
...data.my_user?.local_user_view.person,
// @ts-ignore
generate_totp_2fa: enabled,
})
}
goto($page.url, { invalidateAll: true }) goto($page.url, { invalidateAll: true })
toast({ toast({
content: `Successfully ${enabled ? 'enabled' : 'disabled'} 2FA.`, content: `Successfully ${enabled ? 'enabled' : 'disabled'} 2FA.`,
@@ -105,27 +122,49 @@
<h1 class="font-bold text-2xl">2FA</h1> <h1 class="font-bold text-2xl">2FA</h1>
<p> <p>
2FA on Lemmy is unstable and you may lock yourself out of your account. 2FA on Lemmy is unstable and you may lock yourself out of your account.
{#if feature('newTotp', $site?.version)}
<strong>
You cannot disable 2FA if you lose access to your authenticator.
</strong>
{/if}
</p> </p>
{#if totpLink} {#if totpEnabled}
<TextInput disabled type="password" value={totpLink} label="TOTP Link"> <!--v0.18.0 2FA: Shows a TOTP code which you copy-->
<button {#if totpLink}
slot="suffix" <TextInput disabled type="password" value={totpLink} label="TOTP Link">
class="contents" <button
on:click={() => { slot="suffix"
if (!totpLink) return class="contents"
navigator.clipboard?.writeText(totpLink) on:click={() => {
toast({ content: 'Copied to clipboard.' }) if (!totpLink) return
}} navigator.clipboard?.writeText(totpLink)
> toast({ content: 'Copied to clipboard.' })
<Icon src={ClipboardDocument} size="20" mini /> }}
</button> >
<span class="font-normal text-xs"> <Icon src={ClipboardDocument} size="20" mini />
Paste this in your authenticator app. </button>
</span> <span class="font-normal text-xs">
</TextInput> Paste this in your authenticator app.
<Button on:click={() => twofa(false)} size="lg" color="primary"> </span>
Disable </TextInput>
</Button> {/if}
<p>To change your 2FA state, enter your code here.</p>
<form class="flex flex-col gap-2" on:submit|preventDefault={() => {}}>
<TextInput
bind:value={verify_totp}
placeholder="012345"
label="2FA Code"
/>
{#if totpEnabled}
<Button on:click={() => twofa(false, true)} size="lg" color="primary">
Disable
</Button>
{:else}
<Button on:click={() => twofa(true, true)} size="lg" color="primary">
Enable
</Button>
{/if}
</form>
{:else} {:else}
<Button on:click={() => twofa(true)} size="lg" color="primary"> <Button on:click={() => twofa(true)} size="lg" color="primary">
Setup Setup