Coool page

This commit is contained in:
I-Have-An-Issue 2022-10-10 18:28:46 -04:00
parent 552c046a69
commit 34eb270aca
No known key found for this signature in database
GPG Key ID: E55435DEA0825091
8 changed files with 118 additions and 14 deletions

5
package-lock.json generated
View File

@ -10,6 +10,7 @@
"dependencies": {
"@sveltejs/adapter-node": "^1.0.0-next.96",
"bcrypt": "^5.1.0",
"cookie": "^0.5.0",
"mongodb": "^4.10.0"
},
"devDependencies": {
@ -891,7 +892,6 @@
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz",
"integrity": "sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==",
"dev": true,
"engines": {
"node": ">= 0.6"
}
@ -3727,8 +3727,7 @@
"cookie": {
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz",
"integrity": "sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==",
"dev": true
"integrity": "sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw=="
},
"cssesc": {
"version": "3.0.0",

View File

@ -4,7 +4,7 @@
"private": true,
"scripts": {
"start": "node build/",
"dev": "vite dev",
"dev": "vite dev --port 80",
"build": "vite build",
"preview": "vite preview",
"lint": "prettier --plugin-search-dir . --check .",
@ -26,6 +26,7 @@
"dependencies": {
"@sveltejs/adapter-node": "^1.0.0-next.96",
"bcrypt": "^5.1.0",
"cookie": "^0.5.0",
"mongodb": "^4.10.0"
}
}

6
src/lib/constants.js Normal file
View File

@ -0,0 +1,6 @@
export const COOKIE_NAME = ".ROWBLOX_SESSION_DO_NOT_SHARE";
export const USERNAME_REGEX = /[^A-Za-z0-9\-_ ]/g;
export const MIN_USERNAME_LENGTH = 3;
export const MAX_USERNAME_LENGTH = 16;
export const MIN_PASSWORD_LENGTH = 0;
export const INVITE_KEY_PREFIX = "rowblox-";

View File

@ -1,6 +1,6 @@
import { MongoClient } from "mongodb";
import { hash, compare } from "bcrypt";
import { prerendering } from "$app/env";
import { prerendering } from "$app/environment";
if (!process.env.MONGO_URL) throw new Error("Missing MONGO_URL env variable!");

View File

@ -0,0 +1,42 @@
import { invalid, redirect } from "@sveltejs/kit";
import { MIN_USERNAME_LENGTH, MAX_USERNAME_LENGTH, USERNAME_REGEX, MIN_PASSWORD_LENGTH, INVITE_KEY_PREFIX, COOKIE_NAME } from "$lib/constants";
/** @type {import('./$types').Actions} */
export const actions = {
default: async ({ cookies, request }) => {
const session = cookies.get(COOKIE_NAME);
if (session) return redirect(302, "/");
const data = await request.formData();
const username = data.get("username");
const password = data.get("password");
const confirm_password = data.get("confirm_password");
const invite_key = data.get("invite_key");
console.log(username, password, confirm_password, invite_key);
if (username.length < MIN_USERNAME_LENGTH || username.length > MAX_USERNAME_LENGTH || new RegExp(USERNAME_REGEX).test(username))
return invalid(400, {
username,
invite_key,
error: "username"
});
if (username.length < MIN_PASSWORD_LENGTH || confirm_password !== password)
return invalid(400, {
username,
invite_key,
error: "password"
});
if (!invite_key.startsWith(INVITE_KEY_PREFIX))
return invalid(400, {
username,
invite_key,
error: "invite_key"
});
cookies.set(COOKIE_NAME, "murder row");
return invalid(500, { message: "Dis shit does NOT WORK!" });
}
};

View File

@ -1,20 +1,53 @@
<script>
let username, passwd, cpasswd, invite_key;
function submit() {}
/** @type {import('./$types').ActionData} */ export let form;
</script>
<svelte:head>
<title>Register - Rowblox</title>
<script src="https://js.hcaptcha.com/1/api.js" async defer></script>
</svelte:head>
<form on:submit|preventDefault={submit} class="max-w-lg mx-auto text-center text-white">
<form method="POST" class="max-w-lg mx-auto text-center text-white">
<img class="mx-auto" src="/favicon.png" alt="" />
<div class="flex flex-col">
<input class="rounded-lg w-72 px-4 border-2 border-blue-500 text-black mx-auto my-1.5 py-2" type="username" name="username" id="username" placeholder="Username" bind:value={username} />
<input class="rounded-lg w-72 px-4 border-2 border-blue-500 text-black mx-auto my-1.5 py-2" type="password" name="passwd" id="passwd" placeholder="Password" bind:value={passwd} />
<input class="rounded-lg w-72 px-4 border-2 border-blue-500 text-black mx-auto my-1.5 py-2" type="password" name="cpasswd" id="cpasswd" placeholder="Confirm Password" bind:value={cpasswd} />
<input class="rounded-lg w-72 px-4 border-2 border-blue-500 text-black mx-auto my-1.5 py-2" type="text" name="invite" id="invite" placeholder="Invite Key" bind:value={invite_key} />
<input
class="rounded-lg w-72 px-4 border-2 {form?.error == 'username' ? 'border-red-500' : 'border-blue-500'} text-black mx-auto my-1.5 py-2"
required
type="username"
name="username"
id="username"
placeholder="Username"
value={form?.username ?? ""}
/>
<input
class="rounded-lg w-72 px-4 border-2 {form?.error == 'password' ? 'border-red-500' : 'border-blue-500'} text-black mx-auto my-1.5 py-2"
required
type="password"
name="password"
id="password"
placeholder="Password"
/>
<input
class="rounded-lg w-72 px-4 border-2 {form?.error == 'password' ? 'border-red-500' : 'border-blue-500'} text-black mx-auto my-1.5 py-2"
required
type="password"
name="confirm_password"
id="confirm_password"
placeholder="Confirm Password"
/>
<input
class="rounded-lg w-72 px-4 border-2 {form?.error == 'invite_key' ? 'border-red-500' : 'border-blue-500'} text-black mx-auto my-1.5 py-2"
required
type="text"
name="invite_key"
id="invite_key"
placeholder="Invite Key"
value={form?.invite_key ?? ""}
/>
</div>
<input type="submit" class="my-1.5 text-xl text-white px-4 py-1 w-full rounded shadow-lg border-2 border-blue-500 hover:cursor-pointer" value="Register" />
{#if form?.message}
<p class="rounded w-72 px-4 border-2 border-red-500 text-red-500 mx-auto my-1.5 py-1.5">{form?.message}</p>
{/if}
<div class="h-captcha" data-sitekey="30000000-ffff-ffff-ffff-000000000003" />
<button class="my-1.5 text-xl text-white px-4 py-1 w-full rounded shadow-lg border-2 border-blue-500">Register</button>
</form>

View File

View File

@ -0,0 +1,23 @@
import { json } from "@sveltejs/kit";
import { serialize, parse } from "cookie";
import { createUser } from "$lib/database";
import { MIN_USERNAME_LENGTH, MAX_USERNAME_LENGTH, USERNAME_REGEX, MIN_PASSWORD_LENGTH, INVITE_KEY_PREFIX, COOKIE_NAME } from "$lib/constants";
/** @type {import('./$types').RequestHandler} */
export async function POST({ request }) {
const cookies = parse(request.headers.get("cookie") || "");
const cookie = cookies[COOKIE_NAME];
if (cookie)
return json({
success: false,
error: "You are already logged in!"
});
const body = await request.json();
console.log(body);
return json({
success: false,
error: "LOle!"
});
}