Skip to content

Commit

Permalink
frontend login validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Gautham-kj committed Sep 30, 2023
1 parent ef26a1d commit b65ae3d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width" />
%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover" data-theme="wintry">
<body data-sveltekit-preload-data="hover" data-theme="hamlindigo">
<div style="display: contents" class="h-full overflow-hidden">%sveltekit.body%</div>
</body>
</html>
1 change: 0 additions & 1 deletion src/lib/index.ts

This file was deleted.

9 changes: 6 additions & 3 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import hljs from 'highlight.js';
import 'highlight.js/styles/github-dark.css';
import { storeHighlightJs } from '@skeletonlabs/skeleton';
import { page } from '$app/stores';
storeHighlightJs.set(hljs);
// asset loading
Expand All @@ -18,9 +19,11 @@
</script>

<svelte:head>
{#each images as item}
<link as="image" href={item} rel="preload" class="h-full rounded-r-lg" />
{/each}
{#if !page.user}
{#each images as item}
<link as="image" href={item} rel="preload" class="h-full rounded-r-lg" />
{/each}
{/if}
</svelte:head>
<AppShell>
<slot />
Expand Down
35 changes: 35 additions & 0 deletions src/routes/login/+page.server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as api from '../../lib/api';
import { fail, redirect } from '@sveltejs/kit';
import { MAX_COOKIE_AGE } from '../../lib/constants.ts';

export async function load({ locals }) {
if (locals.user) {
throw redirect(300, '/');
}
}
export const actions = {
default: async ({ cookies, request }) => {
const data = await request.formData();
const username = data.get('username')?.toString();
const password = data.get('password')?.toString();

if (!username || !password) {
return fail(400, { username, password });
}
const res = await api.login(username, password);
const responseData = res;

if (responseData.error) {
const errorDetail = responseData.error.detail ? responseData.error.detail : 'Unknown error';
return fail(400, { message: errorDetail });
} else {
if (responseData.data) {
const value = btoa(JSON.stringify(responseData));
cookies.set('user', value, { path: '/', maxAge: MAX_COOKIE_AGE });
throw redirect(300, '/');
} else {
return fail(400, { message: 'Response data is undefined' });
}
}
}
};
12 changes: 10 additions & 2 deletions src/routes/login/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// picture switching function and attributes
let picture: string = 'assets/image-1.png';
let picNum: number = 1;
let username: string = '';
let password: string = '';
function pictureSwitch(n: number): number {
if (n < 5) {
Expand Down Expand Up @@ -54,6 +56,7 @@
name="username"
placeholder="Username"
class=" w-full variant-form-material"
bind:value={username}
required
/>
</div>
Expand All @@ -66,6 +69,7 @@
name="password"
placeholder="*********"
class=" w-full variant-form-material"
bind:value={password}
required
/>
</div>
Expand All @@ -80,13 +84,17 @@
>
</div>
{#if form?.message}
<div class=" variant-filled-warning flex p-2 m-auto">
<div class=" variant-filled-error flex p-2 m-auto">
<strong>Password or Username in incorrect</strong>
</div>
{/if}
<!-- Submit button -->
<div class="pb-4">
<button class="btn variant-filled">Login</button>
{#if /\s/.test(username) || password.length < 8}
<button class="btn variant-filled" disabled>Login</button>
{:else}
<button class="btn variant-filled">Login</button>
{/if}
</div>
</div>
</form>
Expand Down

0 comments on commit b65ae3d

Please sign in to comment.