I'm using Express.js writing this code to make a simple login post request:
app.post("/login", (req, res) => {
res.send(
{
isUserRegistered: userLogin(req.body.strEmail, req.body.strPassword),
}
)
})
function userLogin(strEmail, strPassword) {
if (strEmail.includes("mike@gmail.com") , strPassword.includes("12345")) {
return true;
} else {
return false;
}
}
My Body (raw):
{
"strEmail":"mike@gmail.com",
"strPassword":"12345"
}
And the expected response is isUserRegistered:True
which depends on what i will pass in the body in postman, Any help?
The problem is with the scope here.
create a folder called utils
and create a userAuthentication.js
file there. userAuthentication.js:
function userLogin(strEmail, strPassword) {
if (strEmail === "mike@gmail.com" && strPassword === "12345") {
return true;
} else {
return false;
}
}
module.exports = userLogin;
In your app.js or index.js file:
const express = require('express');
const userLogin = require('../utils/userAuthentication');
const app = express();
const port = 4000;
app.use(express.json());
app.post('/login', (req, res) =>{
const { strEmail, strPassword } = req.body;
const isAuthenticated = userLogin(strEmail, strPassword);
if (isAuthenticated) {
res.status(200).json({
status: 'Ok',
message: 'A user successfully logged in'
});
} else {
res.status(500).json({
status: 'Fail',
message: 'Wrong credentials'
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.