Skip to content

Commit

Permalink
toasts
Browse files Browse the repository at this point in the history
  • Loading branch information
craigkai committed Oct 6, 2024
1 parent 91e9496 commit ff21d45
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/routes/auth/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { signUpSchema, signInSchema, resetPasswordSchema } from './schemas';
import { setError, superValidate, fail } from 'sveltekit-superforms';
import { setError, message, superValidate, fail } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';

export const actions = {
Expand Down Expand Up @@ -35,7 +35,7 @@ export const actions = {
return { signInForm: form, success: true };
},

resetPassword: async ({ request, locals: { supabase } }) => {
resetpassword: async ({ request, locals: { supabase } }) => {
const form = await superValidate(request, zod(resetPasswordSchema));
if (!form.valid) return fail(400, { form });

Expand All @@ -49,6 +49,6 @@ export const actions = {
return fail(400, { form });
}

return { form, success: true };
return message(form, 'Password reset email sent');
}
};
39 changes: 28 additions & 11 deletions src/routes/auth/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
const {
form: signupForm,
enhance: signupEnhance,
allErrors: signupErrors
errors: signupErrors,
message: signupMessages
}: SuperForm<{ email: string; password: string }> = superForm(data.signupForm, {
warnings: {
duplicateId: false
Expand All @@ -20,7 +21,8 @@
const {
form: signInForm,
enhance: signInEnhance,
allErrors: signInErrors
errors: signInErrors,
message: signInMessages
}: SuperForm<{ email: string; password: string }> = superForm(data.signInForm, {
warnings: {
duplicateId: false
Expand All @@ -30,7 +32,8 @@
const {
form: resetPasswordForm,
enhance: resetPasswordEnhance,
allErrors: resetPasswordErrors
errors: resetPasswordErrors,
message: resetPasswordMessages
}: SuperForm<{ email: string }> = superForm(data.resetPasswordForm, {
warnings: {
duplicateId: false
Expand All @@ -39,14 +42,25 @@
let authMode = $state('signin'); // Switch between sign-in and sign-up modes
let allErrors = $derived([...$signupErrors, ...$signInErrors, ...$resetPasswordErrors]);
$effect(() => {
[$signupErrors, $signInErrors, $resetPasswordErrors].forEach((error) => {
for (const key in error) {
if (error[key]) {
for (const message of $state.snapshot(error[key])) {
toast.error(message);
}
}
}
});
});
$effect(() => {
if (allErrors.length) {
for (const error of allErrors) {
toast.error(error.messages.join('. '));
[$signupMessages, $signInMessages, $resetPasswordMessages].forEach((message) => {
console.log(message);
if (message) {
toast.success(message);
}
}
});
});
</script>

Expand Down Expand Up @@ -134,9 +148,9 @@
>
</form>
{:else if authMode === 'reset'}
<form use:resetPasswordEnhance action="/resetPassword" method="POST">
<form use:resetPasswordEnhance action="?/resetpassword" method="POST">
<div class="form-field">
<Field form={resetPasswordForm} name="name">
<Field form={resetPasswordForm} name="email">
<Control let:attrs>
<Label class="form-label dark:text-gray-300">Email</Label>
<Input
Expand All @@ -149,7 +163,10 @@
<FieldErrors class="form-errors dark:text-red-400" />
</Field>
</div>
<button type="submit">Reset Password</button>
<button
class="mt-2 w-full rounded-md bg-blue-500 px-4 py-2 text-white hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500"
type="submit">Reset Password</button
>
</form>
{/if}

Expand Down

0 comments on commit ff21d45

Please sign in to comment.