Model does not pass JSON to route

0

My model now works but the JSON does not pass well to the router

Model:

const USER = module.exports = dynamoose.model('Usuarios', USER_SCHEMA);

module.exports.getUserByUsername = function (user, callback) {
  var docClient = new AWS.DynamoDB.DocumentClient();

  var params = {
    TableName: "Usuarios",
    KeyConditionExpression: "#us = :uuuu",
    ExpressionAttributeNames: {
      "#us": "username"
    },
    ExpressionAttributeValues: {
      ":uuuu": user
    }
  };
  docClient.query(params, function (err, data) {
    if (err) {
      console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
    } else {
      data.Items.forEach(function (user, callback) {
        console.log(user.username + ": " + user.password + user.email + user.firstName);
      });
    }
    callback(null, user);
  });
}

Router:

const USER = require('../models/user');

router.post('/authenticate', (req, res, next) => {
  const username = req.body.username;
  const password = req.body.password;


  USER.getUserByUsername(username, (err, user) => {
    if (err) throw err;
    if (!user) {
      return res.json({
        success: false,
        "msg": "User not found"
      });
    }
    USER.comparePassword(password, user.password, (err, isMatch) => {
      if (err) throw err;
      if (isMatch) {
        const token = jwt.sign({
          username: user
        }, secret.secret, {
          expiresIn: 86400
        });

        res.json({
          success: true,
          token: 'JWT ' + token,
          user: {
            user: user.username,
            password: user.password,
            email: user.email
          }
        });
      } else {
        return res.json({
          success: false,
          msg: 'Wrong password'
        })
      }
    });
  });
});

I tried with USER.username user.username and username, none works.

JSON response

{
    "success": true,
    "token": "JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Im5pY29sYXMuZ2FyY2lhIiwiaWF0IjoxNTA5ODE5NjMxLCJleHAiOjE1MDk5MDYwMzF9.ubHbqwOZznoeR0C-FkUoX83MoYPomk5SW1ThIA7ww74",
    "user": {}
}

User should have information

    
asked by Nico 03.11.2017 в 03:11
source

0 answers