feat: Update login screen
This commit is contained in:
1 parent
9007eb46d6
commit
c3fc860117
5 files changed
+114
-20
No files matched your search
@@ -7,6 +7,9 @@
|
||||
export let value: string = ''
|
||||
export let maxlength: number = -1
|
||||
export let required = false
|
||||
export let id = ''
|
||||
export let pattern: string | undefined = undefined
|
||||
export let minlength: number = 0
|
||||
|
||||
function typeAction(node: Node) {
|
||||
// @ts-ignore
|
||||
@@ -33,11 +36,18 @@
|
||||
<input
|
||||
use:typeAction
|
||||
bind:value
|
||||
on:keydown={(e) => dispatcher('keydown', e)}
|
||||
on:input={(e) => dispatcher('input', e)}
|
||||
{placeholder}
|
||||
{maxlength}
|
||||
class="w-full px-3 text-sm py-2.5 bg-transparent border border-black/20
|
||||
dark:border-white/20 dark:focus:border-white focus:outline-none
|
||||
focus:border-black transition-colors rounded-md {clazz}"
|
||||
{minlength}
|
||||
{pattern}
|
||||
{id}
|
||||
class="{value.length != 0
|
||||
? 'invalid:border-red-400'
|
||||
: ''} w-full px-3 text-sm py-2.5 bg-transparent
|
||||
border border-black/20 dark:border-white/20 dark:focus:border-white
|
||||
focus:outline-none focus:border-black transition-colors rounded-md {clazz}"
|
||||
{required}
|
||||
/>
|
||||
</label>
|
||||
@@ -43,7 +43,9 @@
|
||||
: ExclamationCircle}
|
||||
/>
|
||||
<div class="flex flex-col p-4 pl-0">
|
||||
<h1 class="text-base font-bold">{toast.title}</h1>
|
||||
{#if toast.title}
|
||||
<h1 class="text-base font-bold">{toast.title}</h1>
|
||||
{/if}
|
||||
<p class="text-sm">{toast.content}</p>
|
||||
</div>
|
||||
<button
|
||||
|
||||
@@ -9,14 +9,51 @@ export enum ToastType {
|
||||
|
||||
export interface Toast {
|
||||
id: number
|
||||
title: string
|
||||
title: string | undefined
|
||||
content: string
|
||||
type: ToastType
|
||||
}
|
||||
|
||||
export const toasts = writable<Toast[]>([])
|
||||
|
||||
export function addToast(title: string, content: string, type: ToastType) {
|
||||
export function toast({
|
||||
title,
|
||||
content,
|
||||
type = ToastType.info,
|
||||
}: {
|
||||
title?: string
|
||||
content: string
|
||||
type: ToastType
|
||||
}) {
|
||||
let id = 0
|
||||
|
||||
toasts.update((toasts) => {
|
||||
id = Math.floor(Math.random() * 10000)
|
||||
|
||||
return [
|
||||
{
|
||||
id: id,
|
||||
content: content,
|
||||
title: title,
|
||||
type: type,
|
||||
},
|
||||
...toasts,
|
||||
]
|
||||
})
|
||||
|
||||
setTimeout(() => {
|
||||
toasts.update((toasts) => toasts.filter((toast) => toast.id != id))
|
||||
}, 5000)
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use @function toast
|
||||
*/
|
||||
export function addToast(
|
||||
title: string | undefined,
|
||||
content: string,
|
||||
type: ToastType
|
||||
) {
|
||||
let id = 0
|
||||
|
||||
toasts.update((toasts) => {
|
||||
|
||||
@@ -3,14 +3,19 @@
|
||||
import Button from '$lib/components/input/Button.svelte'
|
||||
import TextInput from '$lib/components/input/TextInput.svelte'
|
||||
import Card from '$lib/components/ui/Card.svelte'
|
||||
import { ToastType, addToast } from '$lib/components/ui/toasts/toasts.js'
|
||||
import {
|
||||
ToastType,
|
||||
addToast,
|
||||
toast,
|
||||
} from '$lib/components/ui/toasts/toasts.js'
|
||||
import { DEFAULT_INSTANCE_URL, authData, getClient } from '$lib/lemmy.js'
|
||||
import { Color } from '$lib/ui/colors.js'
|
||||
|
||||
let data = {
|
||||
instance: DEFAULT_INSTANCE_URL,
|
||||
instance: '',
|
||||
username: '',
|
||||
password: '',
|
||||
totp: '',
|
||||
loading: false,
|
||||
}
|
||||
|
||||
@@ -36,6 +41,7 @@
|
||||
const response = await getClient(data.instance).login({
|
||||
username_or_email: data.username,
|
||||
password: data.password,
|
||||
totp_2fa_token: data.totp,
|
||||
})
|
||||
|
||||
if (response?.jwt) {
|
||||
@@ -45,13 +51,16 @@
|
||||
username: data.username,
|
||||
})
|
||||
|
||||
addToast('Success', 'Successfully logged in.', ToastType.success)
|
||||
toast({ content: 'Successfully logged in.', type: ToastType.success })
|
||||
goto('/')
|
||||
} else {
|
||||
throw new Error('Invalid credentials')
|
||||
}
|
||||
} catch (error) {
|
||||
addToast('Error', error as any, ToastType.error)
|
||||
toast({
|
||||
content: error as any,
|
||||
type: ToastType.error,
|
||||
})
|
||||
}
|
||||
data.loading = false
|
||||
}
|
||||
@@ -59,20 +68,55 @@
|
||||
|
||||
<svelte:head>Login</svelte:head>
|
||||
|
||||
<Card class="mx-auto p-6 max-w-lg">
|
||||
<!-- <div
|
||||
class="p-4 absolute top-[50%] left-[50%] translate-x-[-50%]
|
||||
translate-y-[-50%] w-full flex flex-col items-center justify-center"
|
||||
> -->
|
||||
<Card class="p-6 max-w-lg w-full mx-auto">
|
||||
<form on:submit|preventDefault={logIn} class="flex flex-col gap-4">
|
||||
<h1 class="font-bold text-lg">Log in</h1>
|
||||
<h1 class="font-bold text-2xl">Log in</h1>
|
||||
<div class="flex flex-row w-full items-center gap-2">
|
||||
<TextInput
|
||||
id="username"
|
||||
bind:value={data.username}
|
||||
label="Username"
|
||||
placeholder="Example"
|
||||
class="flex-1"
|
||||
on:input={(e) => {
|
||||
if (e.detail.data == '@') {
|
||||
document.getElementById('instance_url')?.focus()
|
||||
data.username = data.username.replace('@', '')
|
||||
}
|
||||
}}
|
||||
required
|
||||
/>
|
||||
<span class="mt-5 font-bold">@</span>
|
||||
<TextInput
|
||||
id="instance_url"
|
||||
label="Instance URL"
|
||||
placeholder={DEFAULT_INSTANCE_URL}
|
||||
bind:value={data.instance}
|
||||
class="flex-1"
|
||||
required
|
||||
pattern={'(?!-)[A-Za-z0-9-]+([-.]{1}[a-z0-9]+)*.[A-Za-z]{2,6}'}
|
||||
/>
|
||||
</div>
|
||||
<TextInput
|
||||
bind:value={data.instance}
|
||||
label="Instance URL"
|
||||
placeholder="lemmy.world"
|
||||
id="password"
|
||||
bind:value={data.password}
|
||||
label="Password"
|
||||
type="password"
|
||||
required
|
||||
/>
|
||||
<TextInput
|
||||
bind:value={data.username}
|
||||
label="Username"
|
||||
placeholder="Example"
|
||||
id="totp"
|
||||
bind:value={data.totp}
|
||||
label="2FA Code"
|
||||
placeholder="123456"
|
||||
pattern={'\\d{6}'}
|
||||
minlength={6}
|
||||
maxlength={6}
|
||||
/>
|
||||
<TextInput bind:value={data.password} label="Password" type="password" />
|
||||
<Button
|
||||
loading={data.loading}
|
||||
disabled={data.loading}
|
||||
@@ -84,3 +128,4 @@
|
||||
</Button>
|
||||
</form>
|
||||
</Card>
|
||||
<!-- </div> -->
|
||||
Reference in new issue
Block a user