various refactor
also switched to 2-spaces indentation
This commit is contained in:
parent
3acff47663
commit
d547af7272
42
README.md
42
README.md
@ -1,38 +1,6 @@
|
|||||||
# sv
|
## todo
|
||||||
|
|
||||||
Everything you need to build a Svelte project, powered by [`sv`](https://github.com/sveltejs/cli).
|
- separate data access into repositories
|
||||||
|
- (possibly switch database away from LowDB)
|
||||||
## Creating a project
|
- make /sessions
|
||||||
|
- make user dropdown work with keyboard navigation
|
||||||
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.
|
|
||||||
|
|||||||
2
package-lock.json
generated
2
package-lock.json
generated
@ -24,7 +24,7 @@
|
|||||||
"@sveltejs/kit": "^2.16.0",
|
"@sveltejs/kit": "^2.16.0",
|
||||||
"@sveltejs/vite-plugin-svelte": "^5.0.0",
|
"@sveltejs/vite-plugin-svelte": "^5.0.0",
|
||||||
"@tailwindcss/vite": "^4.0.0",
|
"@tailwindcss/vite": "^4.0.0",
|
||||||
"prettier": "^3.4.2",
|
"prettier": "^3.5.3",
|
||||||
"prettier-plugin-svelte": "^3.3.3",
|
"prettier-plugin-svelte": "^3.3.3",
|
||||||
"svelte": "^5.0.0",
|
"svelte": "^5.0.0",
|
||||||
"svelte-check": "^4.0.0",
|
"svelte-check": "^4.0.0",
|
||||||
|
|||||||
@ -19,7 +19,7 @@
|
|||||||
"@sveltejs/kit": "^2.16.0",
|
"@sveltejs/kit": "^2.16.0",
|
||||||
"@sveltejs/vite-plugin-svelte": "^5.0.0",
|
"@sveltejs/vite-plugin-svelte": "^5.0.0",
|
||||||
"@tailwindcss/vite": "^4.0.0",
|
"@tailwindcss/vite": "^4.0.0",
|
||||||
"prettier": "^3.4.2",
|
"prettier": "^3.5.3",
|
||||||
"prettier-plugin-svelte": "^3.3.3",
|
"prettier-plugin-svelte": "^3.3.3",
|
||||||
"svelte": "^5.0.0",
|
"svelte": "^5.0.0",
|
||||||
"svelte-check": "^4.0.0",
|
"svelte-check": "^4.0.0",
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { db } from '$lib/server/db';
|
import { db } from '$lib/server/db';
|
||||||
import { Guard } from '$lib/server/guard';
|
import { Guard } from '$lib/server/guard';
|
||||||
import { getUserFromSession } from '$lib/server/sessions';
|
import { getUserFromSession } from '$lib/server/sessions';
|
||||||
import type { ServerInit } from '@sveltejs/kit';
|
import type { Handle, ServerInit } from '@sveltejs/kit';
|
||||||
import bcrypt from 'bcryptjs';
|
import bcrypt from 'bcryptjs';
|
||||||
import { writeFileSync } from 'fs';
|
import { writeFileSync } from 'fs';
|
||||||
import { nanoid } from 'nanoid';
|
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;
|
const { cookies, locals } = event;
|
||||||
|
|
||||||
locals.guard = new Guard(getUserFromSession(cookies.get('session')));
|
locals.guard = new Guard(getUserFromSession(cookies.get('session')));
|
||||||
|
|
||||||
return await resolve(event);
|
return await resolve(event);
|
||||||
}
|
};
|
||||||
|
|||||||
@ -3,13 +3,13 @@ import type { Device } from './types/device';
|
|||||||
import type { Group } from './types/group';
|
import type { Group } from './types/group';
|
||||||
import type { User } from './types/user';
|
import type { User } from './types/user';
|
||||||
|
|
||||||
type Data = {
|
type AppData = {
|
||||||
users: User[];
|
users: User[];
|
||||||
groups: Group[];
|
groups: Group[];
|
||||||
devices: Device[];
|
devices: Device[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const db = await JSONFilePreset<Data>('./data/db.json', {
|
export const db = await JSONFilePreset<AppData>('./data/db.json', {
|
||||||
users: [],
|
users: [],
|
||||||
groups: [],
|
groups: [],
|
||||||
devices: [],
|
devices: [],
|
||||||
|
|||||||
@ -6,9 +6,3 @@ export type Device = {
|
|||||||
port: number;
|
port: number;
|
||||||
packets: number;
|
packets: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const defaultDevice: Partial<Device> = {
|
|
||||||
broadcast: '255.255.255.255',
|
|
||||||
port: 9,
|
|
||||||
packets: 3,
|
|
||||||
};
|
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { redirect } from "@sveltejs/kit";
|
import { redirect } from '@sveltejs/kit';
|
||||||
import type { User } from "@prisma/client";
|
import type { User } from './db/types/user';
|
||||||
|
|
||||||
export class Guard {
|
export class Guard {
|
||||||
private readonly user?: User;
|
private readonly user?: User;
|
||||||
@ -7,7 +7,7 @@ export class Guard {
|
|||||||
private authRequired = false;
|
private authRequired = false;
|
||||||
private adminRequired = false;
|
private adminRequired = false;
|
||||||
|
|
||||||
constructor(user?: User, options?: { authRequired?: boolean, adminRequired?: boolean }) {
|
constructor(user?: User, options?: { authRequired?: boolean; adminRequired?: boolean }) {
|
||||||
this.user = user;
|
this.user = user;
|
||||||
this.authRequired = options?.authRequired ?? false;
|
this.authRequired = options?.authRequired ?? false;
|
||||||
this.adminRequired = options?.adminRequired ?? false;
|
this.adminRequired = options?.adminRequired ?? false;
|
||||||
@ -23,11 +23,11 @@ export class Guard {
|
|||||||
|
|
||||||
public orRedirects() {
|
public orRedirects() {
|
||||||
if (this.authRequired && !this.user) {
|
if (this.authRequired && !this.user) {
|
||||||
redirect(302, "/login");
|
redirect(302, '/login');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.adminRequired && !this.user?.admin) {
|
if (this.adminRequired && !this.user?.admin) {
|
||||||
redirect(302, "/dashboard");
|
redirect(302, '/dashboard');
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
@ -3,10 +3,11 @@ import { createSession, getUserFromSession } from '$lib/server/sessions';
|
|||||||
import { redirect } from '@sveltejs/kit';
|
import { redirect } from '@sveltejs/kit';
|
||||||
import bcrypt from 'bcryptjs';
|
import bcrypt from 'bcryptjs';
|
||||||
import type { Actions } from './$types';
|
import type { Actions } from './$types';
|
||||||
|
import { dev } from '$app/environment';
|
||||||
|
|
||||||
export const actions = {
|
export const actions = {
|
||||||
default: async ({ cookies, request }) => {
|
default: async ({ cookies, request }) => {
|
||||||
if (await getUserFromSession(cookies.get('session'))) {
|
if (getUserFromSession(cookies.get('session'))) {
|
||||||
redirect(302, '/dashboard');
|
redirect(302, '/dashboard');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,7 +38,7 @@ export const actions = {
|
|||||||
{
|
{
|
||||||
path: '/',
|
path: '/',
|
||||||
httpOnly: true,
|
httpOnly: true,
|
||||||
secure: true,
|
secure: !dev, // safari doesn't allow secure cookies on localhost
|
||||||
sameSite: true,
|
sameSite: true,
|
||||||
maxAge: 60 * 60 * 24,
|
maxAge: 60 * 60 * 24,
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user