Skip to content

Commit 1ffab60

Browse files
committed
Add comment clarifying checkAuthenticated steps
else block not necessary with early return
1 parent 889c495 commit 1ffab60

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

nodeJS/authentication/sessions.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -298,19 +298,21 @@ async function checkAuthenticated(req, res, next) {
298298
try {
299299
if (!req.session.userId) {
300300
res.redirect("/login");
301-
} else {
302-
const { rows } = await pool.query(
303-
"SELECT * FROM users WHERE id = $1",
304-
[req.session.userId],
305-
);
306-
// add the user details we need to req
307-
// so we can access it in the next middleware
308-
req.user = {
309-
id: rows[0].id,
310-
username: rows[0].username,
311-
};
312-
next();
301+
return;
313302
}
303+
304+
// if there is a user ID in the session, grab that matching user
305+
const { rows } = await pool.query(
306+
"SELECT * FROM users WHERE id = $1",
307+
[req.session.userId],
308+
);
309+
// add the user details we need to req
310+
// so we can access it in the next middleware
311+
req.user = {
312+
id: rows[0].id,
313+
username: rows[0].username,
314+
};
315+
next();
314316
} catch (err) {
315317
next(err);
316318
}

0 commit comments

Comments
 (0)