Fix error messages not showing on login form
This commit is contained in:
parent
1bbc55563d
commit
a9e9c5b080
|
@ -16,72 +16,64 @@ if(Astro.request.method === "POST") {
|
||||||
const username = data.get("username") as string | "";
|
const username = data.get("username") as string | "";
|
||||||
const password = data.get("password") as string | "";
|
const password = data.get("password") as string | "";
|
||||||
|
|
||||||
if(username === "") {
|
if(username === "") error = "username is required";
|
||||||
error = "username is required";
|
if(password === "") error = "password is required";
|
||||||
Astro.redirect("/login");
|
|
||||||
}
|
|
||||||
|
|
||||||
if(password === "") {
|
if(error === "") {
|
||||||
error = "password is required";
|
const userDB = await getUserByNickOrEmail(username as string);
|
||||||
Astro.redirect("/login");
|
|
||||||
}
|
|
||||||
|
|
||||||
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 res = await fetch(`https://localhost:4321/api/auth/generateAccessToken`, {
|
||||||
const user = locationManager.getUser(userDB._id);
|
method: 'POST',
|
||||||
if(!user) throw new Error("User not found");
|
body: JSON.stringify({
|
||||||
const sessionTime = config.SESSION_TIME_MINUTES * 60;
|
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`, {
|
const token = (await res.json()).accessToken;
|
||||||
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, {
|
Astro.cookies.set("username", username, {
|
||||||
path: "/",
|
path: "/",
|
||||||
maxAge: sessionTime,
|
maxAge: sessionTime,
|
||||||
sameSite: "lax",
|
sameSite: "lax",
|
||||||
secure: true
|
secure: true
|
||||||
});
|
});
|
||||||
|
|
||||||
Astro.cookies.set("username", username, {
|
Astro.cookies.set("userid", user.id?.toString() as string, {
|
||||||
path: "/",
|
path: "/",
|
||||||
maxAge: sessionTime,
|
maxAge: sessionTime,
|
||||||
sameSite: "lax",
|
sameSite: "lax",
|
||||||
secure: true
|
secure: true
|
||||||
});
|
});
|
||||||
|
|
||||||
Astro.cookies.set("userid", user.id?.toString() as string, {
|
Astro.cookies.set("currentPlanet", user.mainPlanet._id, {
|
||||||
path: "/",
|
path: "/",
|
||||||
maxAge: sessionTime,
|
maxAge: sessionTime,
|
||||||
sameSite: "lax",
|
sameSite: "lax",
|
||||||
secure: true
|
secure: true
|
||||||
});
|
});
|
||||||
|
|
||||||
Astro.cookies.set("currentPlanet", user.mainPlanet._id, {
|
return Astro.redirect("/game");
|
||||||
path: "/",
|
} else error = "invalid username or password";
|
||||||
maxAge: sessionTime,
|
|
||||||
sameSite: "lax",
|
|
||||||
secure: true
|
|
||||||
});
|
|
||||||
|
|
||||||
return Astro.redirect("/game");
|
|
||||||
} else {
|
|
||||||
error = "invalid username or password";
|
|
||||||
return Astro.redirect("/login");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
---
|
---
|
||||||
|
|
Loading…
Reference in New Issue