qnd solution for first user
This commit is contained in:
parent
862d635012
commit
9cc67e217a
|
|
@ -2,10 +2,17 @@ import type { Handle } from "@sveltejs/kit";
|
||||||
import { error, redirect } from "@sveltejs/kit";
|
import { error, redirect } from "@sveltejs/kit";
|
||||||
|
|
||||||
import SessionStore from "$lib/server/session_store"
|
import SessionStore from "$lib/server/session_store"
|
||||||
import { init_db, close_db, get_user } from "$lib/server/database";
|
import { init_db, close_db, create_user } from "$lib/server/database";
|
||||||
|
|
||||||
import Logs from "$lib/server/log";
|
import Logs from "$lib/server/log";
|
||||||
|
|
||||||
|
let local_setup = {
|
||||||
|
user_setup: (username: string, password: string) => {
|
||||||
|
create_user({name: "name", gender: "x", address: "home", username: username, password: password });
|
||||||
|
local_setup.user_setup = () => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function init() {
|
async function init() {
|
||||||
|
|
||||||
Logs.process.info("Initializing server");
|
Logs.process.info("Initializing server");
|
||||||
|
|
@ -46,6 +53,7 @@ process.on("uncaughtExceptionMonitor", (error, origin) => {
|
||||||
await init();
|
await init();
|
||||||
|
|
||||||
export let handle: Handle = async function ({ event, resolve }) {
|
export let handle: Handle = async function ({ event, resolve }) {
|
||||||
|
event.locals.setup = local_setup
|
||||||
|
|
||||||
Logs.route.debug(`incoming ${event.request.method} request to: ${event.url.href} (route id: ${event.route.id})`);
|
Logs.route.debug(`incoming ${event.request.method} request to: ${event.url.href} (route id: ${event.route.id})`);
|
||||||
|
|
||||||
|
|
@ -56,7 +64,6 @@ export let handle: Handle = async function ({ event, resolve }) {
|
||||||
"Referrer-Policy": "strict-origin-when-cross-origin"
|
"Referrer-Policy": "strict-origin-when-cross-origin"
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log(event.url.href)
|
|
||||||
if (event.route.id == null) {
|
if (event.route.id == null) {
|
||||||
Logs.route.info(`Tried to access a route which does not exist: ${event.url.href}`)
|
Logs.route.info(`Tried to access a route which does not exist: ${event.url.href}`)
|
||||||
return error(404, "This page does not exist.");
|
return error(404, "This page does not exist.");
|
||||||
|
|
|
||||||
|
|
@ -53,6 +53,9 @@ const USER_DATABASE_GET_USER: string =
|
||||||
const USER_DATABASE_GET_USER_BY_NAME: string =
|
const USER_DATABASE_GET_USER_BY_NAME: string =
|
||||||
"SELECT * FROM users WHERE username = $username;"
|
"SELECT * FROM users WHERE username = $username;"
|
||||||
|
|
||||||
|
const USER_DATABASE_EMPTY: string =
|
||||||
|
"SELECT EXISTS (SELECT 1 FROM users);"
|
||||||
|
|
||||||
/*const USER_DATABASE_ADD_ACCESS_TOKEN: string =
|
/*const USER_DATABASE_ADD_ACCESS_TOKEN: string =
|
||||||
"INSERT INTO access_tokens (user_id, token, expiry_date) VALUES ($user_id, $token, $expiry_date);"
|
"INSERT INTO access_tokens (user_id, token, expiry_date) VALUES ($user_id, $token, $expiry_date);"
|
||||||
|
|
||||||
|
|
@ -352,6 +355,8 @@ export async function init_db() {
|
||||||
create_user({name: "PM", gender: "x", address: "home", username: "P", password: "M" });
|
create_user({name: "PM", gender: "x", address: "home", username: "P", password: "M" });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
console.log(do_users_exist())
|
||||||
}
|
}
|
||||||
|
|
||||||
export function close_db() {
|
export function close_db() {
|
||||||
|
|
@ -452,3 +457,10 @@ export function get_user_by_name(username: string): User | null {
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function do_users_exist(): any {
|
||||||
|
const answer = user_database.prepare(USER_DATABASE_EMPTY).get();
|
||||||
|
|
||||||
|
// sqlite trims the first "SELECT " and ";" from the query string
|
||||||
|
return (answer as any)?.[USER_DATABASE_EMPTY.slice(7, -1)];
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,8 @@ export const actions = {
|
||||||
const username = params.get("username") as string | null;
|
const username = params.get("username") as string | null;
|
||||||
const password = params.get("password") as string | null;
|
const password = params.get("password") as string | null;
|
||||||
|
|
||||||
|
locals.setup.user_setup(username, password)
|
||||||
|
|
||||||
Logs.user.info(`Login attempt for user ${username}`)
|
Logs.user.info(`Login attempt for user ${username}`)
|
||||||
|
|
||||||
if (username == null || password == null) {
|
if (username == null || password == null) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue