Skip to content

Commit

Permalink
Merge branch 'main' into feature/jetlog-import
Browse files Browse the repository at this point in the history
  • Loading branch information
johanohly authored Sep 17, 2024
2 parents e87cd0d + d888419 commit 7b5f1ba
Show file tree
Hide file tree
Showing 20 changed files with 336 additions and 297 deletions.
10 changes: 7 additions & 3 deletions scripts/other/update-airports-data.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var fs = require('fs');

const IGNORED_FIELDS = [
'GPS',
'LOCAL',
'type',
'restriction',
Expand Down Expand Up @@ -43,15 +42,20 @@ const IGNORED_FIELDS = [
if (!isNaN(+cell) && cell !== null) {
cell = +cell;
}
airport[header] = cell;

if (i === 2 && airport['ICAO'].length !== 4 && cell?.length === 4) {
airport['ICAO'] = cell;
} else {
airport[header] = cell;
}
});
return airport;
})
.filter((airport) => airport.tier > 3 && airport.ICAO.length === 4);

const airportsDataJson = JSON.stringify(airportsData, null);
const airportsTs = `export const AIRPORTS = ${airportsDataJson};`;
fs.writeFileSync('../src/lib/data/airports.ts', airportsTs);
fs.writeFileSync('../../src/lib/data/airports.ts', airportsTs);
})();

const sanitizeValue = (value) => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/modals/add-flight/AddFlightModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
classes="max-h-full overflow-y-auto max-w-lg"
>
<h2>Add Flight</h2>
<form method="POST" action="?/save-flight" class="grid gap-4" use:enhance>
<form method="POST" action="/api/flight/save" class="grid gap-4" use:enhance>
<AirportField field="from" {form} />
<AirportField field="to" {form} />
<DateTimeField field="departure" {form} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
</Dialog.Trigger>
<Dialog.Content classes="max-h-full overflow-y-auto max-w-lg">
<h2>Edit Flight</h2>
<form method="POST" action="?/save-flight" use:enhance class="grid gap-4">
<form method="POST" action="/api/flight/save" use:enhance class="grid gap-4">
<AirportField field="from" {form} />
<AirportField field="to" {form} />
<DateTimeField field="departure" {form} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

<Modal bind:open dialogOnly>
<h1>Edit Password</h1>
<form method="POST" action="/edit-password" use:enhance>
<form method="POST" action="/api/users/edit-password" use:enhance>
<Form.Field {form} name="currentPassword">
<Form.Control let:attrs>
<Form.Label>Current Password</Form.Label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
const { form: formData, enhance } = form;
</script>

<form method="POST" action="/edit-user" use:enhance>
<form method="POST" action="/api/users/edit" use:enhance>
<Form.Field {form} name="username">
<Form.Control let:attrs>
<Form.Label>Username</Form.Label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

<Modal bind:open>
<h2 class="leading-4">Add User</h2>
<form method="POST" action="/add-user" use:enhance>
<form method="POST" action="/api/users/add" use:enhance>
<Form.Field {form} name="username">
<Form.Control let:attrs>
<Form.Label>Username</Form.Label>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/data/airports.ts

Large diffs are not rendered by default.

18 changes: 10 additions & 8 deletions src/lib/zod/flight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { z } from 'zod';
import { FlightReasons, SeatClasses, SeatTypes } from '$lib/db/types';

// |^$ is for empty string in the case where the user deletes the input
const timeRegex24 = /^([01]?[0-9]|2[0-3]):[0-5][0-9](?:\s?(?:am|pm))?$|^$/i;
const timeRegex12 = /^\d{1,2}:\d{2}\s?(?:am|pm)$/i;
const regex24h =
/^([01]?[0-9]|2[0-3])(?::|\.|)[0-5][0-9](?:\s?(?:am|pm))?$|^$/i;
const regex12hLike = /^\d{1,2}(?::|\.|)\d{2}\s?(?:am|pm)$/i;
const regex12h = /^([1-9]|1[0-2])(?::|\.|)[0-5][0-9]\s?(?:am|pm)$/i;

export const flightAirportsSchema = z.object({
from: z.string().min(1, 'Select an origin'),
Expand All @@ -18,21 +20,21 @@ export const flightDateTimeSchema = z.object({
.refine((value) => value !== null, 'Select a departure date'),
departureTime: z
.string()
.refine((value) => timeRegex24.test(value), 'Invalid 24-hour format')
.refine((value) => regex24h.test(value), 'Invalid 24-hour format')
.refine((value) => {
if (timeRegex12.test(value)) {
return /^([1-9]|1[0-2]):[0-5][0-9]\s?(?:am|pm)$/i.test(value);
if (regex12hLike.test(value)) {
return regex12h.test(value);
}
return true; // If it's not in 12-hour format, just return true (it'll be caught by the previous refine)
}, 'Invalid 12-hour format')
.nullable(),
arrival: z.string().datetime('Select an arrival date').nullable(),
arrivalTime: z
.string()
.refine((value) => timeRegex24.test(value), 'Invalid 24-hour format')
.refine((value) => regex24h.test(value), 'Invalid 24-hour format')
.refine((value) => {
if (timeRegex12.test(value)) {
return /^([1-9]|1[0-2]):[0-5][0-9]\s?(?:am|pm)$/i.test(value);
if (regex12hLike.test(value)) {
return regex12h.test(value);
}
return true; // If it's not in 12-hour format, just return true (it'll be caught by the previous refine)
}, 'Invalid 12-hour format')
Expand Down
41 changes: 2 additions & 39 deletions src/routes/(auth)/login/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,12 @@
import type { Actions, PageServerLoad } from './$types';
import type { PageServerLoad } from './$types';
import { trpcServer } from '$lib/server/server';
import { message, superValidate } from 'sveltekit-superforms';
import { superValidate } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';
import { signInSchema } from '$lib/zod/auth';
import { createSession, getUser } from '$lib/server/utils/auth';
import { lucia } from '$lib/server/auth';
import { fail, redirect } from '@sveltejs/kit';
import { verifyPassword } from '$lib/server/utils/password';

export const load: PageServerLoad = async (event) => {
await trpcServer.user.isSetup.ssr(event);

const form = await superValidate(zod(signInSchema));
return { form };
};

export const actions: Actions = {
default: async (event) => {
const form = await superValidate(event, zod(signInSchema));
if (!form.valid) {
return fail(400, { form });
}

const { username, password } = form.data;

const user = await getUser(username);
if (!user) {
return message(
form,
{ type: 'error', text: 'Invalid username or password' },
{ status: 403 },
);
}

const validPassword = await verifyPassword(user.password, password);
if (!validPassword) {
return message(
form,
{ type: 'error', text: 'Invalid username or password' },
{ status: 403 },
);
}

await createSession(lucia, user.id, event.cookies);

redirect(302, '/');
},
};
7 changes: 6 additions & 1 deletion src/routes/(auth)/login/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@
Welcome back! Enter your username and password to login
</p>
</div>
<form use:enhance method="POST" class="grid gap-4">
<form
use:enhance
action="/api/users/login"
method="POST"
class="grid gap-4"
>
<Form.Field {form} name="username">
<Form.Control let:attrs>
<Form.Label>Username</Form.Label>
Expand Down
49 changes: 2 additions & 47 deletions src/routes/(auth)/setup/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,12 @@
import type { Actions, PageServerLoad } from './$types';
import type { PageServerLoad } from './$types';
import { trpcServer } from '$lib/server/server';
import { message, setError, superValidate } from 'sveltekit-superforms';
import { superValidate } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';
import { signUpSchema } from '$lib/zod/auth';
import { fail, redirect } from '@sveltejs/kit';
import {
createSession,
createUser,
usernameExists,
} from '$lib/server/utils/auth';
import { generateId } from 'lucia';
import { lucia } from '$lib/server/auth';
import { hashPassword } from '$lib/server/utils/password';

export const load: PageServerLoad = async (event) => {
await trpcServer.user.isSetup.ssr(event);

const form = await superValidate(zod(signUpSchema));
return { form };
};

export const actions: Actions = {
default: async (event) => {
const form = await superValidate(event, zod(signUpSchema));
if (!form.valid) {
return fail(400, { form });
}

const { username, password, displayName, unit } = form.data;
const exists = await usernameExists(username);
if (exists) {
return setError(form, 'username', 'Username already exists');
}

const userId = generateId(15);
const hashedPassword = await hashPassword(password);

// Always create the first user as the owner
const success = await createUser(
userId,
username,
hashedPassword,
displayName,
unit,
'owner',
);

if (!success) {
return message(form, { type: 'error', text: 'Failed to create user' });
}

await createSession(lucia, userId, event.cookies);

return redirect(302, '/');
},
};
7 changes: 6 additions & 1 deletion src/routes/(auth)/setup/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@
Welcome to AirTrail! Please set up your owner account to get started.
</p>
</div>
<form method="POST" use:enhance class="grid gap-4">
<form
method="POST"
action="/api/users/setup"
use:enhance
class="grid gap-4"
>
<Form.Field {form} name="username">
<Form.Control let:attrs>
<Form.Label>Username</Form.Label>
Expand Down
18 changes: 18 additions & 0 deletions src/routes/+error.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script lang="ts">
import { page } from '$app/stores';
import { Button } from '$lib/components/ui/button';
</script>

<div class="flex flex-col h-full items-center justify-center">
{#if $page.status === 404}
<h1 class="text-3xl font-bold">404</h1>
<p class="text-lg">Page not found</p>
<Button href="/" variant="outline">Go back</Button>
{:else}
<h1 class="text-4xl font-bold">An unknown error occured</h1>
<p class="text-lg">{$page.error?.message}</p>
<p class="italic text-sm">
If you are the site owner, please check the logs for more information.
</p>
{/if}
</div>
Loading

0 comments on commit 7b5f1ba

Please sign in to comment.