From 2cf8d7d1687c20d7456b6a09f93338507518cdc6 Mon Sep 17 00:00:00 2001 From: Gautham Krishna Date: Sat, 30 Sep 2023 20:55:43 +0530 Subject: [PATCH] frontend signup validation --- src/routes/signup/+page.server.js | 27 +++++++++++----------- src/routes/signup/+page.svelte | 37 ++++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/routes/signup/+page.server.js b/src/routes/signup/+page.server.js index 448289f..1844742 100644 --- a/src/routes/signup/+page.server.js +++ b/src/routes/signup/+page.server.js @@ -1,10 +1,12 @@ import * as api from '../../lib/api'; -import { error, fail } from '@sveltejs/kit'; -// export async function load({ cookies }) { -// const { data, error } = await api.signup(cookies.username, cookies.password, cookies.email); -// return { data, error }; -// } +import { redirect, fail } from '@sveltejs/kit'; +import { MAX_COOKIE_AGE } from '$lib/constants'; +export async function load({ locals }) { + if (locals.user) { + throw redirect(307, '/'); + } +} export const actions = { default: async ({ cookies, request }) => { const data = await request.formData(); @@ -20,15 +22,12 @@ export const actions = { if (responseData.error) { const errorDetail = responseData.error.detail ? responseData.error.detail : 'Unknown error'; - throw error(400, { message: errorDetail.toString() }); - } else { - if (responseData.data) { - cookies.set('sessionid', responseData.data['session-id']?.toString()); - } else { - throw error(400, { message: 'Response data is undefined' }); - } + const errorMessage = Array.isArray(errorDetail) ? errorDetail[0].msg : errorDetail; + console.log('error detail is ', errorMessage); + throw fail(400, { message: errorMessage }); } - - console.log('response data is ', responseData); + const value = btoa(JSON.stringify(responseData)); + cookies.set('user', value, { path: '/', maxAge: MAX_COOKIE_AGE }); + throw redirect(300, '/'); } }; diff --git a/src/routes/signup/+page.svelte b/src/routes/signup/+page.svelte index dfb748f..9a90ae3 100644 --- a/src/routes/signup/+page.svelte +++ b/src/routes/signup/+page.svelte @@ -1,9 +1,13 @@ @@ -14,7 +18,7 @@ > Sign Up -
+
@@ -26,6 +30,7 @@ placeholder="Username" class=" w-full variant-form-material" required + bind:value={username} />
@@ -49,18 +54,20 @@ name="password" placeholder="*********" class=" w-full variant-form-material" + bind:value={pass} required />
- Password + Confirm Password
@@ -73,9 +80,27 @@ Login Here +
+ + Terms and Conditions +
- + {#if /\s/.test(username) || pass.length < 8 || pass != confpass} + + {:else} + + {/if}