Add userVisitedAdventures table and API endpoints for visits and userinfo

This commit is contained in:
Sean Morley
2024-04-03 23:55:00 +00:00
parent ba6a5283fe
commit d834cef83b
10 changed files with 417 additions and 10 deletions

View File

@@ -16,8 +16,9 @@
<article class="prose">
{#if data.user && data.user.username != ""}
<h1 class="mb-4">Welcome {data.user.first_name}. Let's get Exploring!</h1>
{:else}
<h1 class="mb-4">Welcome. Let's get Exploring!</h1>
{/if}
<h1 class="mb-4">Welcome. Let's get Exploring!</h1>
</article>
<img src={campingDrawing} class="w-1/4 mb-4" alt="Logo" />
<button on:click={navToLog} class="btn btn-primary">Open Log</button>
@@ -33,6 +34,6 @@
{#if data.user}
<form method="post" use:enhance>
<button>Sign out</button>
<button class="btn btn-primary">Sign out</button>
</form>
{/if}

View File

@@ -0,0 +1,39 @@
import { lucia } from "$lib/server/auth";
import type { RequestEvent } from "@sveltejs/kit";
export async function GET(event: RequestEvent): Promise<Response> {
if (!event.locals.user) {
return new Response(JSON.stringify({ error: "No user found" }), {
status: 401,
headers: {
"Content-Type": "application/json",
},
});
}
try {
return new Response(
JSON.stringify({
message: "Welcome user info page!",
userId: event.locals.user.id,
username: event.locals.user.username,
firstName: event.locals.user.first_name,
lastName: event.locals.user.last_name,
}),
{
status: 200,
headers: {
"Content-Type": "application/json",
},
}
);
} catch (e) {
console.error(e);
return new Response(JSON.stringify({ error: "Internal server error" }), {
status: 500,
headers: {
"Content-Type": "application/json",
},
});
}
}

View File

@@ -0,0 +1,35 @@
import { lucia } from "$lib/server/auth";
import type { RequestEvent } from "@sveltejs/kit";
import { userVisitedAdventures } from "$lib/db/schema";
import { db } from "$lib/db/db.server";
import { eq } from "drizzle-orm";
// Gets all the adventures that the user has visited
export async function GET(event: RequestEvent): Promise<Response> {
if (!event.locals.user) {
return new Response(JSON.stringify({ error: "No user found" }), {
status: 401,
headers: {
"Content-Type": "application/json",
},
});
}
let result = await db
.select()
.from(userVisitedAdventures)
.where(eq(userVisitedAdventures.userId, event.locals.user.id))
.execute();
return new Response(
JSON.stringify({
result: result,
}),
{
status: 200,
headers: {
"Content-Type": "application/json",
},
}
);
}

View File

@@ -21,6 +21,12 @@ export const actions: Actions = {
const username = formData.get("username");
const password = formData.get("password");
if (!username || !password) {
return fail(400, {
message: "Invalid request",
});
}
if (
typeof username !== "string" ||
username.length < 3 ||

View File

@@ -1,13 +1,42 @@
<!-- routes/login/+page.svelte -->
<script lang="ts">
import { enhance } from "$app/forms";
import { getRandomQuote } from "$lib";
import { onMount } from "svelte";
let quote: string = "";
onMount(async () => {
quote = getRandomQuote();
});
</script>
<h1>Sign in</h1>
<form method="post" use:enhance>
<label for="username">Username</label>
<input name="username" id="username" /><br />
<label for="password">Password</label>
<input type="password" name="password" id="password" /><br />
<button>Continue</button>
</form>
<article class="text-center text-4xl font-extrabold">
<h1>Sign in</h1>
</article>
<div class="flex justify-center">
<form method="post" use:enhance 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="password">Password</label>
<input
type="password"
name="password"
id="password"
class="block mb-2 input input-bordered w-full max-w-xs"
/><br />
<button class="py-2 px-4 rounded btn btn-primary">Login</button>
</form>
</div>
<div class="flex justify-center mt-12 mr-25 ml-25">
<blockquote class="w-80 text-center text-lg break-words">
{#if quote != ""}
"{quote}"
{/if}
<!-- <footer class="text-sm">- Steve Jobs</footer> -->
</blockquote>
</div>