Skip to content

Commit

Permalink
fix: keep unauthenticated user logged in if allowed #114
Browse files Browse the repository at this point in the history
  • Loading branch information
C4illin committed Aug 23, 2024
1 parent f0d0e43 commit bc4ad49
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -415,21 +415,23 @@ const app = new Elysia({
user = await jwt.verify(auth.value);

if (user !== false && user.id) {
// make sure user exists in db
const existingUser = db
.query("SELECT * FROM users WHERE id = ?")
.as(User)
.get(user.id);

if (!existingUser) {
if (auth?.value) {
auth.remove();
if (Number.parseInt(user.id) < 2 ** 24 || !ALLOW_UNAUTHENTICATED) {
// make sure user exists in db
const existingUser = db
.query("SELECT * FROM users WHERE id = ?")
.as(User)
.get(user.id);

if (!existingUser) {
if (auth?.value) {
auth.remove();
}
return redirect("/login", 302);
}
return redirect("/login", 302);
}
}
} else if (ALLOW_UNAUTHENTICATED) {
const newUserId = String(randomInt(2 ^ 24, Number.MAX_SAFE_INTEGER));
const newUserId = String(randomInt(2 ** 24, Number.MAX_SAFE_INTEGER));
const accessToken = await jwt.sign({
id: newUserId,
});
Expand Down

0 comments on commit bc4ad49

Please sign in to comment.