Refactor admin settings page and add user signup functionality

This commit is contained in:
Sean Morley
2024-04-21 16:16:29 +00:00
parent ef42491421
commit a186d514af
3 changed files with 129 additions and 9 deletions

View File

@@ -0,0 +1,12 @@
import { error, redirect, type Actions } from "@sveltejs/kit";
import type { PageServerLoad } from "./$types";
export const load: PageServerLoad = async (event) => {
if (!event.locals.user) {
return redirect(302, "/login");
} else {
if (event.locals.user.role !== "admin") {
return redirect(302, "/settings");
}
}
};

View File

@@ -0,0 +1,80 @@
<script lang="ts">
import { enhance } from "$app/forms";
import { goto } from "$app/navigation";
import { type SubmitFunction } from "@sveltejs/kit";
let errors: { message?: string } = {};
let message: { message?: string } = {};
const addUser: SubmitFunction = async ({ formData, action, cancel }) => {
const response = await fetch(action, {
method: "POST",
body: formData,
});
if (response.ok) {
console.log("User Added Successfully!");
errors = {};
cancel();
window.location.reload();
return;
}
const { type, error } = await response.json();
if (type === "error") {
errors = { message: error.message };
}
console.log(errors);
cancel();
};
</script>
<h1 class="text-center font-extrabold text-4xl">Admin Settings</h1>
<h2 class="text-center font-extrabold text-2xl">Add User</h2>
<div class="flex justify-center">
<form
method="POST"
action="/signup"
use:enhance={addUser}
class="w-full max-w-xs"
>
<label for="username">Username</label>
<input
name="username"
id="username"
class="block mb-2 input input-bordered w-full max-w-xs"
/><br />
<label for="first_name">First Name</label>
<input
name="first_name"
id="first_name"
class="block mb-2 input input-bordered w-full max-w-xs"
/><br />
<label for="last_name">Last Name</label>
<input
name="last_name"
id="last_name"
class="block mb-2 input input-bordered w-full max-w-xs"
/><br />
<label for="password">Password</label>
<input
type="password"
name="password"
id="password"
class="block mb-2 input input-bordered w-full max-w-xs"
/><br />
<label for="role">Admin User?</label>
<input
type="checkbox"
name="role"
id="admin"
class="block mb-2 checkbox-primary checkbox"
/><br />
<button class="py-2 px-4 btn btn-primary">Signup</button>
</form>
</div>
{#if errors.message}
<div class="text-center text-error mt-4">
{errors.message}
</div>
{/if}