So I wrote a rather primitive login logic in Node.js, which authenticates the user and handles JWT. Is it any good in terms of security, efficiency, building, async/sync, logging. SECURITY is my main concern. On in a prettier format the question would be:
- SECURITY: Is my website secure in any way, shape, or form? I'm wondering if I could implement any security measures other than the ones that are built in to the methods provided by
Node.js. Also, I know the passwords are plainly obvious to guess, but they are like that to ensure logging in as different users works. - EFFICIENCY: Is how I'm checking usernames and password efficient? Is there any better way to do this?
- BUILDING: Is how I loaded my website acceptable? Reading from a file and then ending the response?
- ASYNC/SYNC: I know I preform
asyncandsynccalls at the same time. Is there any problem to this? - LOGGING: I log all connections to the server, and all login attempts. Is this a good practice, or am I overdoing what logging is supposed to accomplish? (Source: Login Server with Node.js)
My code is:
// main login logic
app.post('/api/login', apiLimiter, async function (req, res) {
// TODO: implement cookie check whether a still valid token is set and if so respond with cookie already set
// TODO: add roles into jwt and add roles checking into other code
// TODO: if wrong password send a response telling there's a wrong password/username
try {
const pw = req.body.password;
const submittedUser = req.body.email;
User.findOne({eMail: req.body.email}, async function (err, user) {
if (err) throw err;
console.log(user);
const match = await bcrypt.compare(pw, user.password);
console.log(match);
if (match === true && user.eMail == submittedUser) {
jwt2.sign({user}, 'secrettokenhere', { expiresIn: '15min'}, (err, token) =>{
res.cookie(
'access_token', 'Bearer '+ token, {
//domain: 'localhost',
path: '/',
expires: new Date(Date.now() + 900000), // cookie will be removed after 15 mins
httpOnly: true // in production also add secure: true
})
.json(
user
);
});
}
else {
res.status(200).send("Bad login");
}
});
} catch (err) {
res.status(500).send();
console.log(err);
}
});
P.S. there's gonna be a follow-up separate question with Frontend logic.