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",
"version": "1.13.5",
"version": "1.14.0",
"private": true,
"scripts": {
"dev": "vite dev",
@@ -2,15 +2,22 @@
import { deleteProfile, profile, setUser } from '$lib/auth.js'
import { getClient } from '$lib/lemmy.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 { ClipboardDocument, Icon } from 'svelte-hero-icons'
import { goto } from '$app/navigation'
import { page } from '$app/stores'
export let data
let oldPassword = '',
newPassword = '',
newPasswordVerify = ''
let loading = false
async function submit() {
$: totpLink = data.my_user?.local_user_view?.local_user?.totp_2fa_url
async function changePassword() {
try {
if (!$profile?.jwt) return
loading = true
@@ -41,29 +48,88 @@
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>
<h1 class="font-bold text-3xl">Change Password</h1>
<form on:submit|preventDefault={submit} class="max-w-sm flex flex-col gap-4">
<TextInput
bind:value={oldPassword}
label="Current Password"
type="password"
required
/>
<TextInput
bind:value={newPassword}
label="New Password"
type="password"
required
/>
<TextInput
bind:value={newPasswordVerify}
label="New Password (Verify)"
type="password"
required
/>
<Button size="lg" color="primary" submit {loading} disabled={loading}>
Submit
</Button>
</form>
<h1 class="font-bold text-3xl">Credentials</h1>
<div class="flex flex-row flex-wrap gap-8">
<div class="flex flex-col gap-2 max-w-sm w-full">
<h1 class="font-bold text-2xl">Password</h1>
<form
on:submit|preventDefault={changePassword}
class="max-w-sm flex flex-col gap-4"
>
<TextInput
bind:value={oldPassword}
label="Current Password"
type="password"
required
/>
<TextInput
bind:value={newPassword}
label="New Password"
type="password"
required
/>
<TextInput
bind:value={newPasswordVerify}
label="New Password (Verify)"
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
? await uploadImage(profileImage[0], $profile.instance, $profile.jwt)
: undefined
const res = await getClient().saveUserSettings({
auth: $profile.jwt,
...formData,
avatar: pfp,
})
toast({
content: 'Saved your user settings.',
type: 'success',
})
if (res.verify_email_sent) {
toast({
content: 'A verification email was sent.',
+1 -1
View File
@@ -18,7 +18,7 @@
'/profile/blocks',
'/profile/password',
]}
optionNames={['User', 'Settings', 'Blocks', 'Login']}
optionNames={['User', 'Settings', 'Blocks', 'Credentials']}
selected={$page.url.pathname}
on:select={(e) => {
goto(e.detail)