various refactor

also switched to 2-spaces indentation
This commit is contained in:
axel 2025-04-16 11:35:44 +02:00
parent 3acff47663
commit d547af7272
8 changed files with 53 additions and 90 deletions

View File

@ -1,38 +1,6 @@
# sv
## todo
Everything you need to build a Svelte project, powered by [`sv`](https://github.com/sveltejs/cli).
## Creating a project
If you're seeing this, you've probably already done this step. Congrats!
```bash
# create a new project in the current directory
npx sv create
# create a new project in my-app
npx sv create my-app
```
## Developing
Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
```bash
npm run dev
# or start the server and open the app in a new browser tab
npm run dev -- --open
```
## Building
To create a production version of your app:
```bash
npm run build
```
You can preview the production build with `npm run preview`.
> To deploy your app, you may need to install an [adapter](https://svelte.dev/docs/kit/adapters) for your target environment.
- separate data access into repositories
- (possibly switch database away from LowDB)
- make /sessions
- make user dropdown work with keyboard navigation

2
package-lock.json generated
View File

@ -24,7 +24,7 @@
"@sveltejs/kit": "^2.16.0",
"@sveltejs/vite-plugin-svelte": "^5.0.0",
"@tailwindcss/vite": "^4.0.0",
"prettier": "^3.4.2",
"prettier": "^3.5.3",
"prettier-plugin-svelte": "^3.3.3",
"svelte": "^5.0.0",
"svelte-check": "^4.0.0",

View File

@ -19,7 +19,7 @@
"@sveltejs/kit": "^2.16.0",
"@sveltejs/vite-plugin-svelte": "^5.0.0",
"@tailwindcss/vite": "^4.0.0",
"prettier": "^3.4.2",
"prettier": "^3.5.3",
"prettier-plugin-svelte": "^3.3.3",
"svelte": "^5.0.0",
"svelte-check": "^4.0.0",

View File

@ -1,7 +1,7 @@
import { db } from '$lib/server/db';
import { Guard } from '$lib/server/guard';
import { getUserFromSession } from '$lib/server/sessions';
import type { ServerInit } from '@sveltejs/kit';
import type { Handle, ServerInit } from '@sveltejs/kit';
import bcrypt from 'bcryptjs';
import { writeFileSync } from 'fs';
import { nanoid } from 'nanoid';
@ -30,10 +30,10 @@ export const init: ServerInit = async () => {
}
};
export async function handle({ event, resolve }) {
export const handle: Handle = async ({ event, resolve }) => {
const { cookies, locals } = event;
locals.guard = new Guard(getUserFromSession(cookies.get('session')));
return await resolve(event);
}
};

View File

@ -3,13 +3,13 @@ import type { Device } from './types/device';
import type { Group } from './types/group';
import type { User } from './types/user';
type Data = {
type AppData = {
users: User[];
groups: Group[];
devices: Device[];
};
export const db = await JSONFilePreset<Data>('./data/db.json', {
export const db = await JSONFilePreset<AppData>('./data/db.json', {
users: [],
groups: [],
devices: [],

View File

@ -6,9 +6,3 @@ export type Device = {
port: number;
packets: number;
};
export const defaultDevice: Partial<Device> = {
broadcast: '255.255.255.255',
port: 9,
packets: 3,
};

View File

@ -1,5 +1,5 @@
import { redirect } from "@sveltejs/kit";
import type { User } from "@prisma/client";
import { redirect } from '@sveltejs/kit';
import type { User } from './db/types/user';
export class Guard {
private readonly user?: User;
@ -7,7 +7,7 @@ export class Guard {
private authRequired = false;
private adminRequired = false;
constructor(user?: User, options?: { authRequired?: boolean, adminRequired?: boolean }) {
constructor(user?: User, options?: { authRequired?: boolean; adminRequired?: boolean }) {
this.user = user;
this.authRequired = options?.authRequired ?? false;
this.adminRequired = options?.adminRequired ?? false;
@ -23,11 +23,11 @@ export class Guard {
public orRedirects() {
if (this.authRequired && !this.user) {
redirect(302, "/login");
redirect(302, '/login');
}
if (this.adminRequired && !this.user?.admin) {
redirect(302, "/dashboard");
redirect(302, '/dashboard');
}
return this;

View File

@ -3,10 +3,11 @@ import { createSession, getUserFromSession } from '$lib/server/sessions';
import { redirect } from '@sveltejs/kit';
import bcrypt from 'bcryptjs';
import type { Actions } from './$types';
import { dev } from '$app/environment';
export const actions = {
default: async ({ cookies, request }) => {
if (await getUserFromSession(cookies.get('session'))) {
if (getUserFromSession(cookies.get('session'))) {
redirect(302, '/dashboard');
}
@ -37,7 +38,7 @@ export const actions = {
{
path: '/',
httpOnly: true,
secure: true,
secure: !dev, // safari doesn't allow secure cookies on localhost
sameSite: true,
maxAge: 60 * 60 * 24,
},