114 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
			
		
		
	
	
			114 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
| ---
 | |
| import Layout from '../layouts/Layout.astro';
 | |
| import NavBar from '../components/NavBar.astro';
 | |
| 
 | |
| import { createUser } from '../lib/db/users';
 | |
| 
 | |
| import config from '../../config.json';
 | |
| 
 | |
| 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";
 | |
|         Astro.redirect("/register");
 | |
|     }
 | |
| 
 | |
|     if(email === "") {
 | |
|         error = "email is required";
 | |
|         Astro.redirect("/register");
 | |
|     }
 | |
| 
 | |
|     if(password === "") {
 | |
|         error = "password is required";
 | |
|         Astro.redirect("/register");
 | |
|     }
 | |
| 
 | |
|     if(password2 === "") {
 | |
|         error = "password2 is required";
 | |
|         Astro.redirect("/register");
 | |
|     }
 | |
| 
 | |
|     if(username.length < 3 || username.length > 20) {
 | |
|         error = "username must be between 3 and 20 characters long";
 | |
|         Astro.redirect("/register");
 | |
|     }
 | |
| 
 | |
|     if(email.match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/) === null) {
 | |
|         error = "email is invalid";
 | |
|         Astro.redirect("/register");
 | |
|     }
 | |
| 
 | |
|     if(password.length < 8 || password.length > 50) {
 | |
|         error = "password must be between 8 and 50 characters long";
 | |
|         Astro.redirect("/register");
 | |
|     }
 | |
| 
 | |
|     if(password !== password2) {
 | |
|         error = "passwords must match";
 | |
|         Astro.redirect("/register");
 | |
|     }
 | |
| 
 | |
|     if(error === "") {
 | |
|         const user = await createUser(username, email, password);
 | |
| 
 | |
|         const sessionTime = config.SESSION_TIME_MINUTES * 60;
 | |
| 
 | |
|         const res = await fetch(`http://localhost:4321/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, {
 | |
|             path: "/",
 | |
|             maxAge: sessionTime,
 | |
|             sameSite: "lax",
 | |
|             secure: true
 | |
|         });
 | |
| 
 | |
|         Astro.cookies.set("username", username, {
 | |
|             path: "/",
 | |
|             maxAge: sessionTime,
 | |
|             sameSite: "lax",
 | |
|             secure: true
 | |
|         });
 | |
| 
 | |
|         Astro.cookies.set("userid", user._id?.toString() as string, {
 | |
|             path: "/",
 | |
|             maxAge: sessionTime,
 | |
|             sameSite: "lax",
 | |
|             secure: true
 | |
|         })
 | |
| 
 | |
|         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> |