mirror of
https://github.com/BradNut/boredgame
synced 2025-09-08 17:40:22 +00:00
Adding disclaimer to signup without email and using a specific schema defined in zod-schema.
This commit is contained in:
parent
b2c118171b
commit
1a4aa13e69
4 changed files with 79 additions and 32 deletions
|
|
@ -44,6 +44,31 @@ export const userSchema = z.object({
|
||||||
updatedAt: z.date().optional()
|
updatedAt: z.date().optional()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const signUpSchema = userSchema
|
||||||
|
.pick({
|
||||||
|
firstName: true,
|
||||||
|
lastName: true,
|
||||||
|
email: true,
|
||||||
|
username: true,
|
||||||
|
password: true,
|
||||||
|
confirm_password: true,
|
||||||
|
terms: true
|
||||||
|
})
|
||||||
|
.superRefine(({ confirm_password, password }, ctx) => {
|
||||||
|
if (confirm_password !== password) {
|
||||||
|
// ctx.addIssue({
|
||||||
|
// code: 'custom',
|
||||||
|
// message: 'Password and Confirm Password must match',
|
||||||
|
// path: ['password']
|
||||||
|
// });
|
||||||
|
ctx.addIssue({
|
||||||
|
code: 'custom',
|
||||||
|
message: ' Password and Confirm Password must match',
|
||||||
|
path: ['confirm_password']
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
export const updateUserPasswordSchema = userSchema
|
export const updateUserPasswordSchema = userSchema
|
||||||
.pick({ password: true, confirm_password: true })
|
.pick({ password: true, confirm_password: true })
|
||||||
.superRefine(({ confirm_password, password }, ctx) => {
|
.superRefine(({ confirm_password, password }, ctx) => {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { page } from '$app/stores';
|
||||||
import { superForm } from 'sveltekit-superforms/client';
|
import { superForm } from 'sveltekit-superforms/client';
|
||||||
import * as flashModule from 'sveltekit-flash-message/client';
|
import * as flashModule from 'sveltekit-flash-message/client';
|
||||||
|
import toast from 'svelte-french-toast';
|
||||||
import { AlertCircle } from "lucide-svelte";
|
import { AlertCircle } from "lucide-svelte";
|
||||||
import { userSchema } from '$lib/config/zod-schemas.js';
|
import { userSchema } from '$lib/config/zod-schemas.js';
|
||||||
import Label from '$components/ui/label/Label.svelte';
|
import Label from '$components/ui/label/Label.svelte';
|
||||||
|
|
@ -14,10 +16,10 @@
|
||||||
module: flashModule,
|
module: flashModule,
|
||||||
onError: ({ result, message }) => {
|
onError: ({ result, message }) => {
|
||||||
// Error handling for the flash message:
|
// Error handling for the flash message:
|
||||||
// - result is the ActionResult
|
// - result is the ActionResult
|
||||||
// - message is the flash store (not the status message store)
|
// - message is the flash store (not the status message store)
|
||||||
const errorMessage = result.error.message
|
const errorMessage = result.error.message
|
||||||
message.set({ type: 'error', message: errorMessage });
|
message.set({ type: 'error', message: errorMessage });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
syncFlashMessage: false,
|
syncFlashMessage: false,
|
||||||
|
|
@ -25,10 +27,33 @@
|
||||||
validationMethod: 'oninput',
|
validationMethod: 'oninput',
|
||||||
delayMs: 0,
|
delayMs: 0,
|
||||||
});
|
});
|
||||||
console.log($errors);
|
|
||||||
|
const flash = flashModule.getFlash(page);
|
||||||
|
|
||||||
|
$: {
|
||||||
|
if ($flash) {
|
||||||
|
toast.error($flash.message, {
|
||||||
|
duration: 5000
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div>
|
<div class="signin">
|
||||||
|
<form method="POST" use:enhance>
|
||||||
|
<div class="grid w-full max-w-sm items-center gap-2">
|
||||||
|
<h2
|
||||||
|
class="scroll-m-20 border-b pb-2 text-3xl font-semibold tracking-tight transition-colors first:mt-0"
|
||||||
|
>
|
||||||
|
Sign into your account
|
||||||
|
</h2>
|
||||||
|
<Label for="username">Username</Label>
|
||||||
|
<Input type="text" id="username" name="username" placeholder="Username" autocomplete="username" data-invalid={$errors.username} bind:value={$form.username} />
|
||||||
|
<Label for="password">Password</Label>
|
||||||
|
<Input type="password" id="password" name="password" placeholder="Password" autocomplete="new-password" data-invalid={$errors.password} bind:value={$form.password} />
|
||||||
|
<Button type="submit">Sign In</Button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
{#if $errors._errors}
|
{#if $errors._errors}
|
||||||
<Alert variant="destructive">
|
<Alert variant="destructive">
|
||||||
<AlertCircle class="h-4 w-4" />
|
<AlertCircle class="h-4 w-4" />
|
||||||
|
|
@ -38,18 +63,11 @@
|
||||||
</AlertDescription>
|
</AlertDescription>
|
||||||
</Alert>
|
</Alert>
|
||||||
{/if}
|
{/if}
|
||||||
<form method="POST" use:enhance>
|
</div>
|
||||||
<div class="grid w-full max-w-sm items-center gap-2">
|
|
||||||
<h2
|
<style lang="postcss">
|
||||||
class="scroll-m-20 border-b pb-2 text-3xl font-semibold tracking-tight transition-colors first:mt-0"
|
.signin {
|
||||||
>
|
display: grid;
|
||||||
Sign into your account
|
gap: 2rem;
|
||||||
</h2>
|
}
|
||||||
<Label for="username">Username</Label>
|
</style>
|
||||||
<Input type="text" id="username" name="username" placeholder="Username" autocomplete="username" data-invalid={$errors.username} bind:value={$form.username} />
|
|
||||||
<Label for="password">Password</Label>
|
|
||||||
<Input type="password" id="password" name="password" placeholder="Password" autocomplete="new-password" data-invalid={$errors.password} bind:value={$form.password} />
|
|
||||||
<Button type="submit">Sign In</Button>
|
|
||||||
</div>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
|
|
@ -51,6 +51,8 @@ export const actions = {
|
||||||
const form = await superValidate<typeof signUpSchema, Message>(event, signUpSchema);
|
const form = await superValidate<typeof signUpSchema, Message>(event, signUpSchema);
|
||||||
|
|
||||||
if (!form.valid) {
|
if (!form.valid) {
|
||||||
|
form.data.password = '';
|
||||||
|
form.data.confirm_password = '';
|
||||||
return fail(400, {
|
return fail(400, {
|
||||||
form
|
form
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -2,30 +2,24 @@
|
||||||
import { page } from '$app/stores';
|
import { page } from '$app/stores';
|
||||||
import { superForm } from 'sveltekit-superforms/client';
|
import { superForm } from 'sveltekit-superforms/client';
|
||||||
import * as flashModule from 'sveltekit-flash-message/client';
|
import * as flashModule from 'sveltekit-flash-message/client';
|
||||||
|
import toast from 'svelte-french-toast';
|
||||||
import { ChevronsUpDown } from "lucide-svelte";
|
import { ChevronsUpDown } from "lucide-svelte";
|
||||||
import Button from '$components/ui/button/Button.svelte';
|
import Button from '$components/ui/button/Button.svelte';
|
||||||
import Input from '$components/ui/input/Input.svelte';
|
import Input from '$components/ui/input/Input.svelte';
|
||||||
import Label from '$components/ui/label/Label.svelte';
|
import Label from '$components/ui/label/Label.svelte';
|
||||||
import { userSchema } from '$lib/config/zod-schemas.js';
|
import { signUpSchema } from '$lib/config/zod-schemas.js';
|
||||||
import toast from 'svelte-french-toast';
|
|
||||||
import {
|
import {
|
||||||
Collapsible,
|
Collapsible,
|
||||||
CollapsibleContent,
|
CollapsibleContent,
|
||||||
CollapsibleTrigger
|
CollapsibleTrigger
|
||||||
} from "$components/ui/collapsible";
|
} from "$components/ui/collapsible";
|
||||||
|
import Alert from '$components/ui/alert/Alert.svelte';
|
||||||
|
import AlertTitle from '$components/ui/alert/AlertTitle.svelte';
|
||||||
|
import AlertDescription from '$components/ui/alert/AlertDescription.svelte';
|
||||||
|
|
||||||
export let data;
|
export let data;
|
||||||
let isOpen = false;
|
let isOpen = false;
|
||||||
|
|
||||||
const signUpSchema = userSchema.pick({
|
|
||||||
firstName: true,
|
|
||||||
lastName: true,
|
|
||||||
username: true,
|
|
||||||
email: true,
|
|
||||||
password: true,
|
|
||||||
confirm_password: true
|
|
||||||
});
|
|
||||||
|
|
||||||
const { form, errors, constraints, enhance, delayed } = superForm(data.form, {
|
const { form, errors, constraints, enhance, delayed } = superForm(data.form, {
|
||||||
flashMessage: {
|
flashMessage: {
|
||||||
module: flashModule,
|
module: flashModule,
|
||||||
|
|
@ -109,6 +103,14 @@
|
||||||
<Button type="submit">Signup</Button>
|
<Button type="submit">Signup</Button>
|
||||||
<Button variant="link" href="/">or Cancel</Button>
|
<Button variant="link" href="/">or Cancel</Button>
|
||||||
</div>
|
</div>
|
||||||
|
{#if !$form.email}
|
||||||
|
<Alert>
|
||||||
|
<AlertTitle>Heads up!</AlertTitle>
|
||||||
|
<AlertDescription>
|
||||||
|
Without an email address, you won't be able to reset your password. Submit only if you are sure. You can always add this later.
|
||||||
|
</AlertDescription>
|
||||||
|
</Alert>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue