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
+50 -6

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)) {
if (update) {
await getClient().updateTotp({
enabled: 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.local_user,
...data.my_user?.local_user_view.person, ...data.my_user?.local_user_view.person,
// @ts-ignore // @ts-ignore
generate_totp_2fa: enabled, 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,7 +122,14 @@
<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 totpEnabled}
<!--v0.18.0 2FA: Shows a TOTP code which you copy-->
{#if totpLink} {#if totpLink}
<TextInput disabled type="password" value={totpLink} label="TOTP Link"> <TextInput disabled type="password" value={totpLink} label="TOTP Link">
<button <button
@@ -123,9 +147,24 @@
Paste this in your authenticator app. Paste this in your authenticator app.
</span> </span>
</TextInput> </TextInput>
<Button on:click={() => twofa(false)} size="lg" color="primary"> {/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 Disable
</Button> </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