feat: add 2fa

This commit is contained in:
Xyphyn committed 2023-09-27 16:05:57 -07:00
1 parent 29fa924cb3
commit 1214b35b29
4 files changed
+97 -28

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "photon-lemmy", "name": "photon-lemmy",
"version": "1.13.5", "version": "1.14.0",
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "vite dev", "dev": "vite dev",
@@ -2,15 +2,22 @@
import { deleteProfile, profile, setUser } from '$lib/auth.js' import { deleteProfile, profile, setUser } from '$lib/auth.js'
import { getClient } from '$lib/lemmy.js' import { getClient } from '$lib/lemmy.js'
import { trycatch } from '$lib/util.js' import { trycatch } from '$lib/util.js'
import { Button, 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 { goto } from '$app/navigation'
import { page } from '$app/stores'
export let data
let oldPassword = '', let oldPassword = '',
newPassword = '', newPassword = '',
newPasswordVerify = '' newPasswordVerify = ''
let loading = false let loading = false
async function submit() { $: totpLink = data.my_user?.local_user_view?.local_user?.totp_2fa_url
async function changePassword() {
try { try {
if (!$profile?.jwt) return if (!$profile?.jwt) return
loading = true loading = true
@@ -41,29 +48,88 @@
loading = false loading = false
} }
async function twofa(enabled: boolean) {
if (!$profile?.jwt) return
try {
await getClient().saveUserSettings({
...data.my_user?.local_user_view.local_user,
...data.my_user?.local_user_view.person,
generate_totp_2fa: enabled,
auth: $profile?.jwt,
})
goto($page.url, { invalidateAll: true })
toast({
content: `Successfully ${enabled ? 'enabled' : 'disabled'} 2FA.`,
type: 'success',
})
} catch (e) {
toast({ content: e as any, type: 'error' })
}
}
</script> </script>
<h1 class="font-bold text-3xl">Change Password</h1> <h1 class="font-bold text-3xl">Credentials</h1>
<form on:submit|preventDefault={submit} class="max-w-sm flex flex-col gap-4"> <div class="flex flex-row flex-wrap gap-8">
<TextInput <div class="flex flex-col gap-2 max-w-sm w-full">
bind:value={oldPassword} <h1 class="font-bold text-2xl">Password</h1>
label="Current Password" <form
type="password" on:submit|preventDefault={changePassword}
required class="max-w-sm flex flex-col gap-4"
/> >
<TextInput <TextInput
bind:value={newPassword} bind:value={oldPassword}
label="New Password" label="Current Password"
type="password" type="password"
required required
/> />
<TextInput <TextInput
bind:value={newPasswordVerify} bind:value={newPassword}
label="New Password (Verify)" label="New Password"
type="password" type="password"
required required
/> />
<Button size="lg" color="primary" submit {loading} disabled={loading}> <TextInput
Submit bind:value={newPasswordVerify}
</Button> label="New Password (Verify)"
</form> type="password"
required
/>
<Button size="lg" color="primary" submit {loading} disabled={loading}>
Submit
</Button>
</form>
</div>
<div class="flex flex-col gap-2 max-w-sm w-full">
<h1 class="font-bold text-2xl">2FA</h1>
<p>
2FA on Lemmy is unstable and you may lock yourself out of your account.
</p>
{#if totpLink}
<TextInput disabled type="password" value={totpLink} label="TOTP Link">
<button
slot="suffix"
class="contents"
on:click={() => {
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">
Paste this in your authenticator app.
</span>
</TextInput>
<Button on:click={() => twofa(false)} size="lg" color="primary">
Disable
</Button>
{:else}
<Button on:click={() => twofa(true)} size="lg" color="primary">
Setup
</Button>
{/if}
</div>
</div>
@@ -26,15 +26,18 @@
let pfp = profileImage let pfp = profileImage
? await uploadImage(profileImage[0], $profile.instance, $profile.jwt) ? await uploadImage(profileImage[0], $profile.instance, $profile.jwt)
: undefined : undefined
const res = await getClient().saveUserSettings({ const res = await getClient().saveUserSettings({
auth: $profile.jwt, auth: $profile.jwt,
...formData, ...formData,
avatar: pfp, avatar: pfp,
}) })
toast({ toast({
content: 'Saved your user settings.', content: 'Saved your user settings.',
type: 'success', type: 'success',
}) })
if (res.verify_email_sent) { if (res.verify_email_sent) {
toast({ toast({
content: 'A verification email was sent.', content: 'A verification email was sent.',
+1 -1
View File
@@ -18,7 +18,7 @@
'/profile/blocks', '/profile/blocks',
'/profile/password', '/profile/password',
]} ]}
optionNames={['User', 'Settings', 'Blocks', 'Login']} optionNames={['User', 'Settings', 'Blocks', 'Credentials']}
selected={$page.url.pathname} selected={$page.url.pathname}
on:select={(e) => { on:select={(e) => {
goto(e.detail) goto(e.detail)