mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-08-03 20:25:18 +02:00
Refactor admin settings page, clear all sessions, and add user signup functionality
This commit is contained in:
parent
a186d514af
commit
29e9b308ba
4 changed files with 58 additions and 12 deletions
|
@ -19,7 +19,6 @@
|
||||||
let isServerSetup = data.isServerSetup;
|
let isServerSetup = data.isServerSetup;
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
console.log("isServerSetup", isServerSetup);
|
|
||||||
if (!isServerSetup && $page.url.pathname !== "/setup") {
|
if (!isServerSetup && $page.url.pathname !== "/setup") {
|
||||||
goto("/setup");
|
goto("/setup");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import { error, redirect, type Actions } from "@sveltejs/kit";
|
import { error, redirect, type Actions, type Handle } from "@sveltejs/kit";
|
||||||
import type { PageServerLoad } from "./$types";
|
import type { PageServerLoad } from "./$types";
|
||||||
|
import { db } from "$lib/db/db.server";
|
||||||
|
import { sessionTable } from "$lib/db/schema";
|
||||||
|
|
||||||
export const load: PageServerLoad = async (event) => {
|
export const load: PageServerLoad = async (event) => {
|
||||||
if (!event.locals.user) {
|
if (!event.locals.user) {
|
||||||
|
@ -10,3 +12,25 @@ export const load: PageServerLoad = async (event) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const actions: Actions = {
|
||||||
|
clearAllSessions: async (event) => {
|
||||||
|
if (event.locals.user && event.locals.user.role !== "admin") {
|
||||||
|
return error(403, {
|
||||||
|
message: "You are not authorized to perform this action",
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.log("ALL SESSIONS CLEARED");
|
||||||
|
await db.delete(sessionTable).execute();
|
||||||
|
return {
|
||||||
|
status: 200,
|
||||||
|
headers: {
|
||||||
|
"content-type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
message: "Cleared all sessions",
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
|
@ -4,6 +4,10 @@
|
||||||
import { type SubmitFunction } from "@sveltejs/kit";
|
import { type SubmitFunction } from "@sveltejs/kit";
|
||||||
let errors: { message?: string } = {};
|
let errors: { message?: string } = {};
|
||||||
let message: { message?: string } = {};
|
let message: { message?: string } = {};
|
||||||
|
let username: string = "";
|
||||||
|
let first_name: string = "";
|
||||||
|
let last_name: string = "";
|
||||||
|
let password: string = "";
|
||||||
const addUser: SubmitFunction = async ({ formData, action, cancel }) => {
|
const addUser: SubmitFunction = async ({ formData, action, cancel }) => {
|
||||||
const response = await fetch(action, {
|
const response = await fetch(action, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
|
@ -13,8 +17,11 @@
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
console.log("User Added Successfully!");
|
console.log("User Added Successfully!");
|
||||||
errors = {};
|
errors = {};
|
||||||
|
username = "";
|
||||||
|
first_name = "";
|
||||||
|
last_name = "";
|
||||||
|
password = "";
|
||||||
cancel();
|
cancel();
|
||||||
window.location.reload();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,18 +48,21 @@
|
||||||
<input
|
<input
|
||||||
name="username"
|
name="username"
|
||||||
id="username"
|
id="username"
|
||||||
|
bind:value={username}
|
||||||
class="block mb-2 input input-bordered w-full max-w-xs"
|
class="block mb-2 input input-bordered w-full max-w-xs"
|
||||||
/><br />
|
/><br />
|
||||||
<label for="first_name">First Name</label>
|
<label for="first_name">First Name</label>
|
||||||
<input
|
<input
|
||||||
name="first_name"
|
name="first_name"
|
||||||
id="first_name"
|
id="first_name"
|
||||||
|
bind:value={first_name}
|
||||||
class="block mb-2 input input-bordered w-full max-w-xs"
|
class="block mb-2 input input-bordered w-full max-w-xs"
|
||||||
/><br />
|
/><br />
|
||||||
<label for="last_name">Last Name</label>
|
<label for="last_name">Last Name</label>
|
||||||
<input
|
<input
|
||||||
name="last_name"
|
name="last_name"
|
||||||
id="last_name"
|
id="last_name"
|
||||||
|
bind:value={last_name}
|
||||||
class="block mb-2 input input-bordered w-full max-w-xs"
|
class="block mb-2 input input-bordered w-full max-w-xs"
|
||||||
/><br />
|
/><br />
|
||||||
<label for="password">Password</label>
|
<label for="password">Password</label>
|
||||||
|
@ -60,6 +70,7 @@
|
||||||
type="password"
|
type="password"
|
||||||
name="password"
|
name="password"
|
||||||
id="password"
|
id="password"
|
||||||
|
bind:value={password}
|
||||||
class="block mb-2 input input-bordered w-full max-w-xs"
|
class="block mb-2 input input-bordered w-full max-w-xs"
|
||||||
/><br />
|
/><br />
|
||||||
<label for="role">Admin User?</label>
|
<label for="role">Admin User?</label>
|
||||||
|
@ -78,3 +89,14 @@
|
||||||
{errors.message}
|
{errors.message}
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
<h2 class="text-center font-extrabold text-2xl">Session Managment</h2>
|
||||||
|
<div class="flex justify-center items-center">
|
||||||
|
<form use:enhance method="POST" action="?/clearAllSessions">
|
||||||
|
<input
|
||||||
|
type="submit"
|
||||||
|
class="btn btn-warning"
|
||||||
|
value="Clear All Users Sessions"
|
||||||
|
/>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
|
@ -23,7 +23,7 @@ export const actions: Actions = {
|
||||||
|
|
||||||
// check all to make sure all fields are provided
|
// check all to make sure all fields are provided
|
||||||
if (!username || !password || !firstName || !lastName) {
|
if (!username || !password || !firstName || !lastName) {
|
||||||
return fail(400, {
|
return error(400, {
|
||||||
message: "All fields are required",
|
message: "All fields are required",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ export const actions: Actions = {
|
||||||
username.length > 31 ||
|
username.length > 31 ||
|
||||||
!/^[a-z0-9_-]+$/.test(username)
|
!/^[a-z0-9_-]+$/.test(username)
|
||||||
) {
|
) {
|
||||||
return fail(400, {
|
return error(400, {
|
||||||
message: "Invalid username",
|
message: "Invalid username",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -52,7 +52,7 @@ export const actions: Actions = {
|
||||||
password.length < 6 ||
|
password.length < 6 ||
|
||||||
password.length > 255
|
password.length > 255
|
||||||
) {
|
) {
|
||||||
return fail(400, {
|
return error(400, {
|
||||||
message: "Invalid password",
|
message: "Invalid password",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -62,7 +62,7 @@ export const actions: Actions = {
|
||||||
firstName.length < 1 ||
|
firstName.length < 1 ||
|
||||||
firstName.length > 255
|
firstName.length > 255
|
||||||
) {
|
) {
|
||||||
return fail(400, {
|
return error(400, {
|
||||||
message: "Invalid first name",
|
message: "Invalid first name",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -72,14 +72,11 @@ export const actions: Actions = {
|
||||||
lastName.length < 1 ||
|
lastName.length < 1 ||
|
||||||
lastName.length > 255
|
lastName.length > 255
|
||||||
) {
|
) {
|
||||||
return fail(400, {
|
return error(400, {
|
||||||
message: "Invalid last name",
|
message: "Invalid last name",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const userId = generateId(15);
|
|
||||||
const hashedPassword = await new Argon2id().hash(password);
|
|
||||||
|
|
||||||
const usernameTaken = await db
|
const usernameTaken = await db
|
||||||
.select()
|
.select()
|
||||||
.from(userTable)
|
.from(userTable)
|
||||||
|
@ -88,10 +85,14 @@ export const actions: Actions = {
|
||||||
.then((results) => results[0] as unknown as DatabaseUser | undefined);
|
.then((results) => results[0] as unknown as DatabaseUser | undefined);
|
||||||
|
|
||||||
if (usernameTaken) {
|
if (usernameTaken) {
|
||||||
return fail(400, {
|
return error(400, {
|
||||||
message: "Username already taken",
|
message: "Username already taken",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const userId = generateId(15);
|
||||||
|
const hashedPassword = await new Argon2id().hash(password);
|
||||||
|
|
||||||
await db
|
await db
|
||||||
.insert(userTable)
|
.insert(userTable)
|
||||||
.values({
|
.values({
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue