Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can register duplicate user if I don't restart the server #7

Open
lengsovandara opened this issue Sep 21, 2019 · 2 comments
Open

Comments

@lengsovandara
Copy link

No description provided.

@lengsovandara lengsovandara changed the title I can' register duplicate user if I don't restart the server I can register duplicate user if I don't restart the server Sep 21, 2019
@lengsovandara
Copy link
Author

lengsovandara commented Sep 21, 2019

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.

@akamalov
Copy link

akamalov commented Nov 6, 2019

I can confirm the same behavior. User gets registered. I can register duplicates over and over again:

File: newuser.json

{
  "email": "[email protected]",
  "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": "[email protected]",
      "password": "bruno"
    },
    {
      "id": 2,
      "email": "[email protected]",
      "password": "techie"
    },
    {
      "id": 3,
      "email": "[email protected]",
      "password": "nilson"
    },
    {
      "id": 4,
      "email": "[email protected]",
      "password": "nilson"
    },
    {
      "id": 5,
      "email": "[email protected]",
      "password": "nilson"
    },
    {
      "id": 6,
      "email": "[email protected]",
      "password": "nilson"
    },
    {
      "id": 7,
      "email": "[email protected]",
      "password": "nilson"
    },
    {
      "id": 8,
      "email": "[email protected]",
      "password": "nilson"
    },
    {
      "id": 9,
      "email": "[email protected]",
      "password": "nilson"
    },
    {
      "id": 10
    },
    {
      "id": 11
    },
    {
      "id": 12
    },
    {
      "id": 13
    },
    {
      "id": 14
    },
    {
      "id": 15
    },
    {
      "id": 16
    },
    {
      "id": 17
    },
    {
      "id": 18
    },
    {
      "id": 19
    }
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants