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).
|
||||
|
||||
## 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
2
package-lock.json
generated
@ -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",
|
||||
|
||||
@ -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",
|
||||
|
||||
@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
@ -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: [],
|
||||
|
||||
@ -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,
|
||||
};
|
||||
|
||||
@ -1,51 +1,51 @@
|
||||
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;
|
||||
private readonly user?: User;
|
||||
|
||||
private authRequired = false;
|
||||
private adminRequired = false;
|
||||
private authRequired = false;
|
||||
private adminRequired = false;
|
||||
|
||||
constructor(user?: User, options?: { authRequired?: boolean, adminRequired?: boolean }) {
|
||||
this.user = user;
|
||||
this.authRequired = options?.authRequired ?? false;
|
||||
this.adminRequired = options?.adminRequired ?? false;
|
||||
}
|
||||
constructor(user?: User, options?: { authRequired?: boolean; adminRequired?: boolean }) {
|
||||
this.user = user;
|
||||
this.authRequired = options?.authRequired ?? false;
|
||||
this.adminRequired = options?.adminRequired ?? false;
|
||||
}
|
||||
|
||||
public requiresAuth() {
|
||||
return new Guard(this.user, { authRequired: true });
|
||||
}
|
||||
public requiresAuth() {
|
||||
return new Guard(this.user, { authRequired: true });
|
||||
}
|
||||
|
||||
public requiresAdmin() {
|
||||
return new Guard(this.user, { authRequired: true, adminRequired: true });
|
||||
}
|
||||
public requiresAdmin() {
|
||||
return new Guard(this.user, { authRequired: true, adminRequired: true });
|
||||
}
|
||||
|
||||
public orRedirects() {
|
||||
if (this.authRequired && !this.user) {
|
||||
redirect(302, "/login");
|
||||
}
|
||||
public orRedirects() {
|
||||
if (this.authRequired && !this.user) {
|
||||
redirect(302, '/login');
|
||||
}
|
||||
|
||||
if (this.adminRequired && !this.user?.admin) {
|
||||
redirect(302, "/dashboard");
|
||||
}
|
||||
if (this.adminRequired && !this.user?.admin) {
|
||||
redirect(302, '/dashboard');
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public isFailed() {
|
||||
if (this.authRequired && !this.user) {
|
||||
return true;
|
||||
}
|
||||
public isFailed() {
|
||||
if (this.authRequired && !this.user) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.adminRequired && !this.user?.admin) {
|
||||
return true;
|
||||
}
|
||||
if (this.adminRequired && !this.user?.admin) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public getUser() {
|
||||
return this.user!;
|
||||
}
|
||||
}
|
||||
public getUser() {
|
||||
return this.user!;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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,
|
||||
},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user