Refactor user avatar component and add settings page

This commit is contained in:
Sean Morley
2024-04-10 23:12:25 +00:00
parent 226158da6d
commit 3d0116a684
5 changed files with 106 additions and 4 deletions

View File

@@ -0,0 +1,48 @@
import { redirect, type Actions } from "@sveltejs/kit";
import type { PageServerLoad } from "./$types";
import { db } from "$lib/db/db.server";
import { userTable } from "$lib/db/schema";
import { eq } from "drizzle-orm";
export const load: PageServerLoad = async (event) => {
if (event.locals.user)
return {
user: event.locals.user,
};
return redirect(302, "/login");
};
export const actions: Actions = {
default: async (event: { request: { formData: () => any; }; }) => {
const formData = await event.request.formData();
let userId = formData.get("user_id");
let username = formData.get("username");
let firstName = formData.get("first_name");
let lastName = formData.get("last_name");
if (!userId) {
return {
status: 400,
body: {
message: "User ID is required"
}
};
}
await db.update(userTable)
.set({
username: username,
first_name: firstName,
last_name: lastName,
})
.where(eq(userTable.id, userId));
return {
status: 200,
body: {
message: "User updated"
}
};
}
};