Fix error messages not showing on login form

This commit is contained in:
Aelita4 2024-11-03 23:33:12 +01:00
parent 1bbc55563d
commit a9e9c5b080
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
1 changed files with 49 additions and 57 deletions

View File

@ -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 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 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";
}
}
---