mirror of
https://github.com/wireadmin/wireadmin
synced 2025-06-26 18:28:06 +00:00
fix
This commit is contained in:
parent
0b4aa21cfe
commit
fc0e423f07
@ -1,6 +1,7 @@
|
|||||||
import { type Actions, fail } from '@sveltejs/kit';
|
import { fail } from '@sveltejs/kit';
|
||||||
|
import type { Actions } from '@sveltejs/kit';
|
||||||
import type { PageServerLoad } from './$types';
|
import type { PageServerLoad } from './$types';
|
||||||
import { superValidate } from 'sveltekit-superforms/server';
|
import { setError, superValidate } from 'sveltekit-superforms/server';
|
||||||
import { formSchema } from './schema';
|
import { formSchema } from './schema';
|
||||||
import { HASHED_PASSWORD } from '$env/static/private';
|
import { HASHED_PASSWORD } from '$env/static/private';
|
||||||
import { generateToken } from '$lib/auth';
|
import { generateToken } from '$lib/auth';
|
||||||
@ -12,19 +13,22 @@ export const load: PageServerLoad = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const actions: Actions = {
|
export const actions: Actions = {
|
||||||
default: async ({ request, cookies }) => {
|
default: async (event) => {
|
||||||
const data = await request.formData();
|
const form = await superValidate(event, formSchema);
|
||||||
const password = data.get('password') ?? '';
|
|
||||||
|
if (!form.valid) {
|
||||||
|
return fail(400, { ok: false, message: 'Bad Request', form });
|
||||||
|
}
|
||||||
|
|
||||||
|
const { password } = form.data;
|
||||||
|
|
||||||
if (HASHED_PASSWORD.toLowerCase() !== Buffer.from(password.toString()).toString('hex').toLowerCase()) {
|
if (HASHED_PASSWORD.toLowerCase() !== Buffer.from(password.toString()).toString('hex').toLowerCase()) {
|
||||||
console.warn('auth failed');
|
return setError(form, 'password', 'Incorrect password.');
|
||||||
return fail(401, { message: 'Unauthorized' });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const token = await generateToken();
|
const token = await generateToken();
|
||||||
cookies.set('authorization', token);
|
event.cookies.set('authorization', token);
|
||||||
|
|
||||||
console.info('logged in.');
|
return { ok: true };
|
||||||
return { message: 'Success!' };
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -1,30 +1,44 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import * as Form from '$lib/components/ui/form';
|
|
||||||
import * as Card from '$lib/components/ui/card';
|
|
||||||
import { formSchema, type FormSchema } from './schema';
|
import { formSchema, type FormSchema } from './schema';
|
||||||
import type { SuperValidated } from 'sveltekit-superforms';
|
import type { SuperValidated } from 'sveltekit-superforms';
|
||||||
|
import { Card, CardContent } from '$lib/components/ui/card';
|
||||||
|
import { Form, FormButton, FormField, FormInput, FormItem, FormLabel, FormValidation } from '$lib/components/ui/form';
|
||||||
|
import { goto } from '$app/navigation';
|
||||||
|
|
||||||
export let form: SuperValidated<FormSchema>;
|
export let form: SuperValidated<FormSchema>;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Card.Root>
|
<Card>
|
||||||
<Card.Content>
|
<CardContent>
|
||||||
<Form.Root method="POST" {form} schema={formSchema} let:config class="pt-4 space-y-8">
|
<Form
|
||||||
|
{form}
|
||||||
|
schema={formSchema}
|
||||||
|
let:config
|
||||||
|
method="POST"
|
||||||
|
class="pt-4 space-y-8"
|
||||||
|
options={{
|
||||||
|
onResult: ({ result }) => {
|
||||||
|
if (result.type === 'success') {
|
||||||
|
goto('/');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
>
|
||||||
<div class="w-full flex items-center justify-center">
|
<div class="w-full flex items-center justify-center">
|
||||||
<div class="w-16 aspect-square flex items-center justify-center rounded-full bg-gray-200">
|
<div class="w-16 aspect-square flex items-center justify-center rounded-full bg-gray-200">
|
||||||
<i class="fas fa-user text-primary text-2xl" />
|
<i class="fas fa-user text-primary text-2xl" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Form.Field {config} name="password">
|
<FormField {config} name="password">
|
||||||
<Form.Item>
|
<FormItem>
|
||||||
<Form.Label>Password</Form.Label>
|
<FormLabel>Password</FormLabel>
|
||||||
<Form.Input type="password" autocomplete="off" />
|
<FormInput type="password" autocomplete="off" />
|
||||||
<Form.Validation />
|
<FormValidation />
|
||||||
</Form.Item>
|
</FormItem>
|
||||||
</Form.Field>
|
</FormField>
|
||||||
|
|
||||||
<Form.Button class="w-full">Sign In</Form.Button>
|
<FormButton class="w-full">Sign In</FormButton>
|
||||||
</Form.Root>
|
</Form>
|
||||||
</Card.Content>
|
</CardContent>
|
||||||
</Card.Root>
|
</Card>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
export const formSchema = z.object({
|
export const formSchema = z.object({
|
||||||
password: z.string(),
|
password: z.string().min(1, { message: 'Password is required' }),
|
||||||
});
|
});
|
||||||
export type FormSchema = typeof formSchema;
|
export type FormSchema = typeof formSchema;
|
||||||
|
Loading…
Reference in New Issue
Block a user