1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-29 17:59:36 +02:00

Refactor admin settings page, add UserCard component, and update user management functionality

This commit is contained in:
Sean Morley 2024-04-21 20:26:22 +00:00
parent 29e9b308ba
commit b3bd8780e7
4 changed files with 46 additions and 7 deletions

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{ {
"name": "adventurelog", "name": "adventurelog",
"version": "0.1.0", "version": "0.1.6",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "adventurelog", "name": "adventurelog",
"version": "0.1.0", "version": "0.1.6",
"dependencies": { "dependencies": {
"@lucia-auth/adapter-drizzle": "^1.0.7", "@lucia-auth/adapter-drizzle": "^1.0.7",
"@vercel/analytics": "^1.2.2", "@vercel/analytics": "^1.2.2",

View file

@ -0,0 +1,17 @@
<script lang="ts">
import type { DatabaseUser } from "$lib/server/auth";
export let user: DatabaseUser;
</script>
<div class="card w-96 bg-base-100 shadow-xl">
<div class="card-body">
<h2 class="card-title">{user.first_name} {user.last_name}</h2>
<p>{user.username}</p>
<p>Last Login: {user.last_login}</p>
<p>Created: {user.signup_date}</p>
<div class="card-actions justify-end">
<button class="btn btn-primary">Edit User</button>
</div>
</div>
</div>

View file

@ -1,16 +1,24 @@
import { error, redirect, type Actions, type Handle } 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 { db } from "$lib/db/db.server";
import { sessionTable } from "$lib/db/schema"; import { sessionTable, userTable } from "$lib/db/schema";
import type { DatabaseUser } from "$lib/server/auth";
export const load: PageServerLoad = async (event) => { export const load: PageServerLoad = async (event) => {
let users: DatabaseUser[] = [];
if (!event.locals.user) { if (!event.locals.user) {
return redirect(302, "/login"); return redirect(302, "/login");
} else { }
if (event.locals.user.role !== "admin") { if (event.locals.user.role !== "admin") {
return redirect(302, "/settings"); return redirect(302, "/settings");
} }
if (event.locals.user.role === "admin") {
users = (await db.select().from(userTable).execute()) as DatabaseUser[];
console.log(users);
} }
return {
users,
};
}; };
export const actions: Actions = { export const actions: Actions = {

View file

@ -1,13 +1,18 @@
<script lang="ts"> <script lang="ts">
import { page } from "$app/stores";
import { enhance } from "$app/forms"; import { enhance } from "$app/forms";
import { goto } from "$app/navigation"; import { goto } from "$app/navigation";
import { type SubmitFunction } from "@sveltejs/kit"; import { type SubmitFunction } from "@sveltejs/kit";
import type { DatabaseUser } from "lucia";
import UserCard from "$lib/components/UserCard.svelte";
let errors: { message?: string } = {}; let errors: { message?: string } = {};
let message: { message?: string } = {}; let message: { message?: string } = {};
let username: string = ""; let username: string = "";
let first_name: string = ""; let first_name: string = "";
let last_name: string = ""; let last_name: string = "";
let password: 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",
@ -100,3 +105,12 @@
/> />
</form> </form>
</div> </div>
<h2 class="text-center font-extrabold text-2xl">User Managment</h2>
<div class="text-center">
{#each $page.data.users as user}
<div>
<UserCard {user} />
</div>
{/each}
</div>