I've a problem about async/await on typescript with target es2017. Below is my code :
my route.ts :
method: 'POST',
config: {
auth: {
strategy: 'token',
}
},
path: '/users',
handler: (request, reply) {
let { username, password } = request.payload;
const getOperation = User
.findAll({
where: {
username: username
}
})
.then(([User]) => {
if(!User) {
reply({
error: true,
errMessage: 'The specified user was not found'
});
return;
}
let comparedPassword = compareHashPassword(User.password, password);
console.log(comparedPassword);
if(comparedPassword) {
const token = jwt.sign({
username,
scope: User.id
}, 'iJiYpmaVwXMp8PpzPkWqc9ShQ5UhyEfy', {
algorithm: 'HS256',
expiresIn: '1h'
});
reply({
token,
scope: User.id
});
} else {
reply('incorrect password');
}
} )
.catch(error => { reply('server-side error') });
};
my helper.ts :
export async function compareHashPassword(pw:string, originalPw:string) {
console.log(pw);
console.log(originalPw);
let compared = bcrypt.compare(originalPw, pw, function(err, isMatch) {
if(err) {
return console.error(err);
}
console.log(isMatch);
return isMatch;
});
return await compared;
}
this auth route supposed to return JWT token when user login. but the problem here is even when I enter the valid password to sign-in the function compareHashPassword always return undefined.
For example when i call the api with json string
{
"username": "x",
"password": "helloword"
}
When i track using console.log(), the log is :
$2a$10$Y9wkjblablabla -> hashed password stored in db
helloword
Promise {
<pending>,
domain:
Domain {
domain: null,
_events: { error: [Function: bound ] },
_eventsCount: 1,
_maxListeners: undefined,
members: [] } }
true
maybe this is just my lack of understanding about using async/await with typescript. for note my env is :
node : v8.6.0
typescript : v2.5.2
ts-node : v3.3.0
my tsconfig.json //
{
"compilerOptions": {
"outDir": "dist",
"target": "es2017",
"module": "commonjs",
"removeComments": true,
"types": [
"node"
],
"allowJs": true,
"moduleResolution": "classic"
},
"exclude": [
"node_modules"
]
}