Skip to content

Commit

Permalink
feat: config zod schema validation (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
moonlitgrace authored Jan 19, 2025
1 parent 7fb4a16 commit cd9a87e
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 12 deletions.
13 changes: 12 additions & 1 deletion frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"typescript": "^5.0.0",
"typescript-eslint": "^8.0.0",
"vite": "^5.0.3",
"vitest": "^2.0.4"
"vitest": "^2.0.4",
"zod": "^3.24.1"
},
"overrides": {
"cookie": "^0.7.0"
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/lib/components/modals/auth/forms/join.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
auth_type = 'login';
}
} else if (result.type === 'failure') {
console.log(result);
errors = result.data;
}
pending = false;
Expand Down Expand Up @@ -66,6 +67,7 @@
action="/auth?/{auth_type}"
use:enhance={handle_submit}
class="flex flex-col gap-3"
novalidate
>
<label class="input input-bordered flex items-center gap-2">
<coreicons-shape-mail class="size-4"></coreicons-shape-mail>
Expand Down
8 changes: 8 additions & 0 deletions frontend/src/lib/utils/zod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { z } from 'zod';

export const zod_required_string = ({ field_name }: { field_name: string }) => {
return z
.string()
.min(1, { message: `${field_name} is required` })
.trim();
};
13 changes: 13 additions & 0 deletions frontend/src/lib/zod_schemas/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { zod_required_string } from '$lib/utils/zod';
import { z } from 'zod';

export const auth_schema = z.object({
email: zod_required_string({ field_name: 'Email' }).email({
message: 'Email is invalid'
}),
password: zod_required_string({ field_name: 'Password' }).min(8, {
message: 'Password must contain atleast 8 chararcters'
})
});

export type AuthSchema = z.infer<typeof auth_schema>;
37 changes: 27 additions & 10 deletions frontend/src/routes/(actions)/auth/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
import { dev } from '$app/environment';
import client from '$lib/clients/v1/client';
import { auth_schema } from '$lib/zod_schemas/auth';
import type { Actions } from './$types';
import { fail } from '@sveltejs/kit';

export const actions = {
login: async ({ request, cookies }) => {
const form_data = await request.formData();

const { data, error, response } = await client.POST('/api/v1/auth/login/', {
body: {
email: String(form_data.get('email')),
password: String(form_data.get('password'))
}
const {
data: parse_data,
error: parse_error,
success: parse_success
} = auth_schema.safeParse({
email: form_data.get('email'),
password: form_data.get('password')
});

if (response.ok && data) {
cookies.set('auth_token', data.token, {
if (!parse_success) {
return fail(400, {
detail: Object.values(parse_error.flatten().fieldErrors)?.[0]?.[0]
});
}

const {
data: res_data,
error: res_error,
response: res_response
} = await client.POST('/api/v1/auth/login/', {
body: { ...parse_data }
});

if (res_response.ok && res_data) {
cookies.set('auth_token', res_data.token, {
httpOnly: true,
secure: !dev,
path: '/',
sameSite: 'lax'
});

return { token: data.token };
} else if (error) {
return fail(response.status, error.errors[0]);
return { token: res_data.token };
} else if (res_error) {
return fail(res_response.status, res_error.errors[0]);
}
},
register: async ({ request }) => {
Expand Down

0 comments on commit cd9a87e

Please sign in to comment.