diff --git a/src/pages/login.astro b/src/pages/login.astro index 4460be1..e5c38bd 100644 --- a/src/pages/login.astro +++ b/src/pages/login.astro @@ -16,72 +16,64 @@ if(Astro.request.method === "POST") { const username = data.get("username") as string | ""; const password = data.get("password") as string | ""; - if(username === "") { - error = "username is required"; - Astro.redirect("/login"); - } + if(username === "") error = "username is required"; + if(password === "") error = "password is required"; - if(password === "") { - error = "password is required"; - Astro.redirect("/login"); - } + if(error === "") { + const userDB = await getUserByNickOrEmail(username as string); - const userDB = await getUserByNickOrEmail(username as string); + if(userDB !== null && await verify(userDB.password, password as string)) { + const user = locationManager.getUser(userDB._id); + if(!user) throw new Error("User not found"); + const sessionTime = config.SESSION_TIME_MINUTES * 60; - if(userDB !== null && await verify(userDB.password, password as string)) { - const user = locationManager.getUser(userDB._id); - if(!user) throw new Error("User not found"); - const sessionTime = config.SESSION_TIME_MINUTES * 60; + const res = await fetch(`https://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; - const res = await fetch(`https://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; + await updateLastLogin(user); - await updateLastLogin(user); + Astro.cookies.set("sessionToken", token, { + path: "/", + maxAge: sessionTime, + sameSite: "lax", + secure: true + }); - 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("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 + }); - Astro.cookies.set("userid", user.id?.toString() as string, { - path: "/", - maxAge: sessionTime, - sameSite: "lax", - secure: true - }); + Astro.cookies.set("currentPlanet", user.mainPlanet._id, { + path: "/", + maxAge: sessionTime, + sameSite: "lax", + secure: true + }); - Astro.cookies.set("currentPlanet", user.mainPlanet._id, { - path: "/", - maxAge: sessionTime, - sameSite: "lax", - secure: true - }); - - return Astro.redirect("/game"); - } else { - error = "invalid username or password"; - return Astro.redirect("/login"); + return Astro.redirect("/game"); + } else error = "invalid username or password"; } } ---