mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-08-04 12:45:17 +02:00
Add error handeling handling to the signup form
This commit is contained in:
parent
5ee4d91473
commit
c3b84c912d
4 changed files with 53 additions and 44 deletions
|
@ -31,7 +31,7 @@ export const actions: Actions = {
|
||||||
typeof username !== "string" ||
|
typeof username !== "string" ||
|
||||||
username.length < 3 ||
|
username.length < 3 ||
|
||||||
username.length > 31 ||
|
username.length > 31 ||
|
||||||
!/^[a-z0-9_-]+$/.test(username)
|
!/^[a-zA-Z0-9_-]+$/.test(username)
|
||||||
) {
|
) {
|
||||||
return error(400, {
|
return error(400, {
|
||||||
message: "Invalid username",
|
message: "Invalid username",
|
||||||
|
@ -47,7 +47,7 @@ export const actions: Actions = {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const existingUser:any = await db
|
const existingUser: any = await db
|
||||||
.select()
|
.select()
|
||||||
.from(userTable)
|
.from(userTable)
|
||||||
.where(eq(userTable.username, username))
|
.where(eq(userTable.username, username))
|
||||||
|
@ -86,6 +86,5 @@ export const actions: Actions = {
|
||||||
});
|
});
|
||||||
|
|
||||||
return redirect(302, "/");
|
return redirect(302, "/");
|
||||||
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -73,7 +73,7 @@
|
||||||
<div class="flex justify-center mb-4">
|
<div class="flex justify-center mb-4">
|
||||||
<form
|
<form
|
||||||
method="POST"
|
method="POST"
|
||||||
action="/signup"
|
action="?/adduser"
|
||||||
use:enhance={addUser}
|
use:enhance={addUser}
|
||||||
class="w-full max-w-xs"
|
class="w-full max-w-xs"
|
||||||
>
|
>
|
||||||
|
|
|
@ -6,10 +6,17 @@ import { Argon2id } from "oslo/password";
|
||||||
import { db } from "$lib/db/db.server";
|
import { db } from "$lib/db/db.server";
|
||||||
import type { DatabaseUser } from "$lib/server/auth";
|
import type { DatabaseUser } from "$lib/server/auth";
|
||||||
|
|
||||||
import type { Actions } from "./$types";
|
import type { Actions, PageServerLoad } from "./$types";
|
||||||
import { userTable } from "$lib/db/schema";
|
import { userTable } from "$lib/db/schema";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
|
|
||||||
|
export const load: PageServerLoad = async (event) => {
|
||||||
|
if (event.locals.user) {
|
||||||
|
return redirect(302, "/");
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
};
|
||||||
|
|
||||||
export const actions: Actions = {
|
export const actions: Actions = {
|
||||||
default: async (event) => {
|
default: async (event) => {
|
||||||
const formData = await event.request.formData();
|
const formData = await event.request.formData();
|
||||||
|
@ -17,10 +24,12 @@ export const actions: Actions = {
|
||||||
const password = formData.get("password");
|
const password = formData.get("password");
|
||||||
const firstName = formData.get("first_name");
|
const firstName = formData.get("first_name");
|
||||||
const lastName = formData.get("last_name");
|
const lastName = formData.get("last_name");
|
||||||
let role: string = "";
|
|
||||||
// username must be between 4 ~ 31 characters, and only consists of lowercase letters, 0-9, -, and _
|
// username must be between 4 ~ 31 characters, and only consists of lowercase letters, 0-9, -, and _
|
||||||
// keep in mind some database (e.g. mysql) are case insensitive
|
// keep in mind some database (e.g. mysql) are case insensitive
|
||||||
|
|
||||||
|
if (event.locals.user) {
|
||||||
|
return redirect(302, "/");
|
||||||
|
}
|
||||||
// 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 error(400, {
|
return error(400, {
|
||||||
|
@ -28,20 +37,11 @@ export const actions: Actions = {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!event.locals.user) {
|
|
||||||
role = "user";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (event.locals.user && event.locals.user.role === "admin") {
|
|
||||||
const isAdmin = formData.get("role") === "on";
|
|
||||||
role = isAdmin ? "admin" : "user";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
typeof username !== "string" ||
|
typeof username !== "string" ||
|
||||||
username.length < 3 ||
|
username.length < 3 ||
|
||||||
username.length > 31 ||
|
username.length > 31 ||
|
||||||
!/^[a-z0-9_-]+$/.test(username)
|
!/^[a-zA-Z0-9_-]+$/.test(username)
|
||||||
) {
|
) {
|
||||||
return error(400, {
|
return error(400, {
|
||||||
message: "Invalid username",
|
message: "Invalid username",
|
||||||
|
@ -102,12 +102,11 @@ export const actions: Actions = {
|
||||||
last_name: lastName,
|
last_name: lastName,
|
||||||
hashed_password: hashedPassword,
|
hashed_password: hashedPassword,
|
||||||
signup_date: new Date(),
|
signup_date: new Date(),
|
||||||
role: role,
|
role: "user",
|
||||||
last_login: new Date(),
|
last_login: new Date(),
|
||||||
} as DatabaseUser)
|
} as DatabaseUser)
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
if (!event.locals.user) {
|
|
||||||
const session = await lucia.createSession(userId, {});
|
const session = await lucia.createSession(userId, {});
|
||||||
const sessionCookie = lucia.createSessionCookie(session.id);
|
const sessionCookie = lucia.createSessionCookie(session.id);
|
||||||
event.cookies.set(sessionCookie.name, sessionCookie.value, {
|
event.cookies.set(sessionCookie.name, sessionCookie.value, {
|
||||||
|
@ -115,23 +114,6 @@ export const actions: Actions = {
|
||||||
...sessionCookie.attributes,
|
...sessionCookie.attributes,
|
||||||
});
|
});
|
||||||
|
|
||||||
redirect(302, "/");
|
return redirect(302, "/");
|
||||||
} else {
|
|
||||||
if (event.locals.user && event.locals.user.role !== "admin") {
|
|
||||||
return error(403, {
|
|
||||||
message: "You are not authorized to add users",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
status: 200,
|
|
||||||
headers: {
|
|
||||||
"content-type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
message: "User date",
|
|
||||||
}),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,15 +1,38 @@
|
||||||
<!-- routes/signup/+page.svelte -->
|
<!-- routes/signup/+page.svelte -->
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { enhance } from "$app/forms";
|
import { enhance } from "$app/forms";
|
||||||
|
import { goto } from "$app/navigation";
|
||||||
import { getRandomQuote } from "$lib";
|
import { getRandomQuote } from "$lib";
|
||||||
|
import type { SubmitFunction } from "@sveltejs/kit";
|
||||||
import { onMount } from "svelte";
|
import { onMount } from "svelte";
|
||||||
|
|
||||||
|
let errors: { message?: string } = {};
|
||||||
let backgroundImageUrl = "https://source.unsplash.com/random/?mountains";
|
let backgroundImageUrl = "https://source.unsplash.com/random/?mountains";
|
||||||
|
|
||||||
let quote: string = "";
|
let quote: string = "";
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
quote = getRandomQuote();
|
quote = getRandomQuote();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const handleSubmit: SubmitFunction = async ({ formData, action, cancel }) => {
|
||||||
|
const response = await fetch(action, {
|
||||||
|
method: "POST",
|
||||||
|
body: formData,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
errors = {};
|
||||||
|
goto("/signup");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { type, error } = await response.json();
|
||||||
|
if (type === "error") {
|
||||||
|
errors = { message: error.message };
|
||||||
|
}
|
||||||
|
console.log(errors);
|
||||||
|
cancel();
|
||||||
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
|
@ -22,7 +45,7 @@
|
||||||
</article>
|
</article>
|
||||||
|
|
||||||
<div class="flex justify-center">
|
<div class="flex justify-center">
|
||||||
<form method="post" use:enhance class="w-full max-w-xs">
|
<form method="post" use:enhance={handleSubmit} class="w-full max-w-xs">
|
||||||
<label for="username">Username</label>
|
<label for="username">Username</label>
|
||||||
<input
|
<input
|
||||||
name="username"
|
name="username"
|
||||||
|
@ -52,6 +75,12 @@
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{#if errors.message}
|
||||||
|
<div class="text-center text-error mt-4">
|
||||||
|
{errors.message}
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
<div class="flex justify-center mt-12 mr-25 ml-25">
|
<div class="flex justify-center mt-12 mr-25 ml-25">
|
||||||
<blockquote class="w-80 text-center text-lg break-words">
|
<blockquote class="w-80 text-center text-lg break-words">
|
||||||
{#if quote != ""}
|
{#if quote != ""}
|
||||||
|
@ -62,7 +91,6 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- username first last pass -->
|
<!-- username first last pass -->
|
||||||
|
|
||||||
<svelte:head>
|
<svelte:head>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue