I'm following this (YouTube Link) node tutorial cause I have to build a web app for a friend. He need to use Dynamo DB and node js and I'm learning while I build this app, I was doing great but now I'm stuck.
I'm trying to get a JWT token (as mentioned on the video) but nothing happens. Here's my route:
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(user, secret.secret, {
expiresIn: 86400
});
res.json({
success: true,
token: 'JWT ' + token,
user: {
user: user.username,
password: user.password,
email: USER.email
}
});
}
});
});
});
Here's the passport js code
module.exports = function (passport) {
let opts = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderWithScheme('jwt');
opts.secretOrKey = secret;
passport.use(new JwtStrategy(opts, (jwt_payload, done) => {
USER.getUserbyUsername(jwy_payload.user2, (err, user) => {
if (err) {
return done(err, false);
}
if (user) {
return done(null, user);
} else {
return done(null, false);
}
});
}));
}
And here's the function in the model user.js
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 (item) {
console.log(item.username + ": " + item.password);
let user = item.username;
console.log(user);
return user;
});
}
});
}
That last function prints the username + the password, so the query is working but that's it, I got no response at all after passing the username and password through Postman.
Thanks guys!