I can register duplicate user if I don't restart the server
lengsovandara opened this issue · comments
lengsovandara commented
I can register duplicate user if I don't restart the server
lengsovandara commented
Replace userdb by below code in function isAuthenticated
function isAuthenticated({email, password}){
return JSON.parse(fs.readFileSync('./users.json', 'UTF-8')).users.findIndex(user => user.email === email && user.password === password) !== -1
}
when user have successful created, userdb didn't got refresh.
Alex Kamalov commented
I can confirm the same behavior. User gets registered. I can register duplicates over and over again:
File: newuser.json
{
"email": "alex@example.com",
"password":"G4nd4lfth3Gr3y"
}
Go ahead register the same user over and over again:
curl -X POST -d "@newuser.json" "http://127.0.0.1:8000/auth/register" =
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1NzMwNTcyNTMsImV4cCI6MTU3MzA2MDg1M30.ccsSZcm_D2GAoHBriukXZGGBM9NLdMUUVPno2xWEUzI"
}% '
curl -X POST -d "@newuser.json" "http://127.0.0.1:8000/auth/register"
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1NzMwNTcyNTgsImV4cCI6MTU3MzA2MDg1OH0.OAQD2POtZTWg9cq5K26vI_6eXcYidm4CaajHo6crnTE"
}%
curl -X POST -d "@newuser.json" "http://127.0.0.1:8000/auth/register"
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1NzMwNTcyNjAsImV4cCI6MTU3MzA2MDg2MH0.MxDFqpdES529H8klbDXSYWCix_Svayc-xmDMu8ze1F4"
}%
$
Config file
File: server.js
...
// Check if the user exists in database
function isAuthenticated({email, password}){
return JSON.parse(fs.readFileSync('./users.json', 'UTF-8')).users.findIndex(user => (user.email === email && user.password === password) !== -1)
// return userdb.users.findIndex(user => (user.email === email && user.password === password) !== -1)
}
// Register New User
server.post('/auth/register', (req, res) => {
console.log("register endpoint called; request body:");
console.log(req.body);
const {email, password} = req.body;
if(isAuthenticated({email, password}) === true) {
const status = 401;
const message = 'Email and Password already exist';
res.status(status).json({status, message});
return
}
fs.readFile("./users.json", (err, data) => {
if (err) {
const status = 401
const message = err
res.status(status).json({status, message})
return
};
...
File usrers.json
shows updated index, but no data populated.
File: users.json
{
"users": [
{
"id": 1,
"email": "bruno@email.com",
"password": "bruno"
},
{
"id": 2,
"email": "techie@email.com",
"password": "techie"
},
{
"id": 3,
"email": "nilson@email.com",
"password": "nilson"
},
{
"id": 4,
"email": "nilson1@email.com",
"password": "nilson"
},
{
"id": 5,
"email": "nilson2@email.com",
"password": "nilson"
},
{
"id": 6,
"email": "nilson3@email.com",
"password": "nilson"
},
{
"id": 7,
"email": "nilson4@email.com",
"password": "nilson"
},
{
"id": 8,
"email": "nilson7@email.com",
"password": "nilson"
},
{
"id": 9,
"email": "nilson8@email.com",
"password": "nilson"
},
{
"id": 10
},
{
"id": 11
},
{
"id": 12
},
{
"id": 13
},
{
"id": 14
},
{
"id": 15
},
{
"id": 16
},
{
"id": 17
},
{
"id": 18
},
{
"id": 19
}
]
}