117 lines
4.4 KiB
Plaintext
117 lines
4.4 KiB
Plaintext
---
|
|
import NavBar from '../components/NavBar.astro';
|
|
import Layout from '../layouts/Layout.astro';
|
|
|
|
import { createUser } from '../lib/db/users';
|
|
|
|
import { AstroCookieSetOptions } from 'astro';
|
|
import { ObjectId } from 'mongodb';
|
|
import config from '../../config.json';
|
|
import { getAllGalaxies } from '../lib/db/galaxies';
|
|
import { createPlanet } from '../lib/db/planets';
|
|
import { addSystemToSector } from '../lib/db/sectors';
|
|
import { createSystem } from '../lib/db/systems';
|
|
import DBPlanet from '../types/db/DBPlanet';
|
|
import DBSystem from '../types/db/DBSystem';
|
|
import locationManager from '../lib/classes/managers/LocationManager';
|
|
|
|
let error = "";
|
|
|
|
if(Astro.request.method === "POST") {
|
|
const data = await Astro.request.formData();
|
|
const username = data.get("username") as string | "";
|
|
const email = data.get("email") as string | "";
|
|
const password = data.get("password") as string | "";
|
|
const password2 = data.get("password2") as string | "";
|
|
|
|
if(username === "") error = "username is required";
|
|
if(username.match(/^[a-zA-Z0-9]{3,20}$/) === null) error = "username must be between 3 and 20 characters long and can only contain letters and numbers";
|
|
if(email === "") error = "email is required";
|
|
if(email.match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/) === null) error = "email is invalid";
|
|
if(password === "") error = "password is required";
|
|
if(password2 === "") error = "password2 is required";
|
|
if(password.length < 8 || password.length > 50) error = "password must be between 8 and 50 characters long";
|
|
if(password !== password2) error = "passwords must match";
|
|
|
|
if(error === "") {
|
|
const userId = new ObjectId();
|
|
const planetId = new ObjectId();
|
|
|
|
const user = await createUser(userId, username, email, password, planetId);
|
|
|
|
const sessionTime = config.SESSION_TIME_MINUTES * 60;
|
|
|
|
const galaxyIndex = Math.floor(Math.random() * 4);
|
|
const sectorIndex = Math.floor(Math.random() * 8);
|
|
|
|
const planetData: DBPlanet = {
|
|
_id: planetId,
|
|
owner: user._id,
|
|
name: `${username}'s home planet`,
|
|
fields: 100,
|
|
buildings: [],
|
|
ships: [],
|
|
resources: []
|
|
}
|
|
|
|
await createPlanet(planetData);
|
|
|
|
const systemData: DBSystem = {
|
|
_id: new ObjectId(),
|
|
name: `${username}'s home system`,
|
|
ownedBy: user._id,
|
|
structures: [],
|
|
planets: [planetData._id],
|
|
}
|
|
|
|
await createSystem(systemData);
|
|
|
|
const galaxies = await getAllGalaxies();
|
|
const sectorId = galaxies[galaxyIndex].sectors[sectorIndex];
|
|
|
|
await addSystemToSector(sectorId, systemData._id);
|
|
|
|
const cookieOptions: AstroCookieSetOptions = {
|
|
path: "/",
|
|
maxAge: sessionTime,
|
|
sameSite: "lax",
|
|
secure: true
|
|
}
|
|
|
|
Astro.cookies.set("username", username, cookieOptions);
|
|
Astro.cookies.set("userid", user._id.toString() as string, cookieOptions);
|
|
Astro.cookies.set("currentPlanet", planetData._id.toString(), cookieOptions);
|
|
Astro.cookies.set("currentSystem", systemData._id.toString(), cookieOptions);
|
|
|
|
await locationManager.init();
|
|
const res = await fetch(`${Astro.url.origin}/api/auth/generateAccessToken`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
username,
|
|
createdFrom: 'loginForm',
|
|
duration: sessionTime
|
|
}),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + config.MASTER_ACCESSTOKEN
|
|
}
|
|
});
|
|
const token = (await res.json()).accessToken;
|
|
Astro.cookies.set("sessionToken", token, cookieOptions);
|
|
|
|
return Astro.redirect("/game");
|
|
}
|
|
}
|
|
---
|
|
|
|
<Layout title="Register">
|
|
<NavBar loggedIn="false" active="register" />
|
|
<form method="POST">
|
|
<input type="text" name="username" placeholder="username" /><br />
|
|
<input type="email" name="email" placeholder="email" /><br />
|
|
<input type="password" name="password" placeholder="password" /><br />
|
|
<input type="password" name="password2" placeholder="password2" /><br />
|
|
<input type="submit" value="register" />
|
|
{ error !== "" ? <p style="color: red;">{error}</p> : "" }
|
|
</form>
|
|
</Layout> |