I am trying to validate a user's password using bcryptjs. I have this function which returns a Promise, however when I get to bycrypt.hash, all i get is Promise { <pending> } Therefore .then() will not execute on undefined. Please help, I've been stuck on this a while
userSchema.methods.verifyPassword = function (password, err, next) {
const saltSecret = this.saltSecret;
const a = async function(resolve, reject) {
console.log('hi4')
console.log('this.saltSecret2', saltSecret);
console.log(password);
const hashed_pass = await bcrypt.hash(password, saltSecret);
console.log('hash', hashed_pass);
const valid = await bcrypt.compare(password, hashed_pass);
if(valid){
console.log('GOOD');
}
};
a();
};
I like to use async-await syntax to handle promises. It is less confusing. and gives the ability of quickly understanding someone else code.
you can make your function an async one. wait until bcrypt does its job
const password = await bcrypt.hash(password, saltSecret);
However bcrypt library provides a function to compare password and the hash
const valid = await bcrypt.compare(password, hashed_pass);
try this
async function(resolve, reject) {
console.log('hi4')
console.log(this.saltSecret);
console.log(password);
const hashed_pass = await bcrypt.hash(password, saltSecret);
console.log('hash', hashed_pass);
const valid = await bcrypt.compare(password, hashed_pass);
if(valid){
console.log('GOOD');
}
};
This line will always return a Promise.
console.log(bcrypt.hash(password, this.saltSecret));
You could always do something like this.
return new Promise(async (resolve, reject) => {
const hash = await bcrypt.hash(password, this.saltSecret);
if (hash == this.password) {
return resolve(true);
}
return reject();
});
bcrypt.hash uses a callback, not a promise (that's what the .then is doing)
You should use it like so:
bcrypt.hash(password, this.saltSecret, (err, hash) => {
...
});
Related
createFolder() function is returning an empty array. I am not sure what I am doing wrong but it needs to return the items within project_array
function get_project_folders(){
return new Promise((resolve, reject)=>{
fs.readdir(__dirname + '/projects', (error, data1)=>{
if(error){
reject(console.log(`Error. Unable to read directory - ${error}`))
}else{
resolve(data1)
}
})
})
}
async function createFolder(){
let list_of_projects = await get_project_folders()
let project_array = []
return new Promise((resolve, reject)=>{
for(let project of list_of_projects){
let splitProject = project.split("-")
fs.readdir(__dirname + `/projects/${splitProject[0]}-${splitProject[1]}`, (error, data1)=>{
if(error){
console.log('Error. Unable to read directory.')
}else{
project_array.push({circuit: splitProject[0], fuse: splitProject[1], pole: data1})
}
})
}
resolve(project_array)
})
}
async function testIt(){
let folderData = await createFolder()
console.log(folderData)
}
testIt()
This is a classic, what you are doing is resolving the promise with the empty array before your node fs async methods have resolved. Try this instead:
async function createFolder(){
const list_of_projects = await get_project_folders();
const result = await Promise.all( list_of_projects.map(project => new Promise((resolve, reject) => {
const splitProject = project.split("-");
fs.readdir(__dirname + `/projects/${splitProject[0]}-${splitProject[1]}`, (error, data1) => {
if(error){
console.error('Error. Unable to read directory.');
resolve( null );
} else {
resolve({
circuit: splitProject[0],
fuse: splitProject[1],
pole: data1
});
}
});
});
// Filter out the errors that resolved as `null`
return result.filter( Boolean );
}
In essence, wrap every fs. call in a promise, then use Promise.all to generate an array of promises. Because Promise.all requires all to be resolved for it to be resolved, make sure you even resolve when there is an error - just return something falsy (in my case null) so you can filter it out later.
I am having a problem where an async function does not appear to be waiting. I am calling one async function from another, with the second returning a value after async operations have completed and then the first should be returning this as it has awaited it. But when logging accessToken in the first function it logs before awaiting the return from the second. Where am I going wrong? Thanks in advance.
export const confirmCode = async (verificationId, code) => {
try {
const credential = await firebase.auth.PhoneAuthProvider.credential(verificationId, code);
const accessToken = await authenticate(credential);
console.log(accessToken); // prints undefined as does not wait for above function call?
return accessToken;
} catch (error) {
console.log(error)
// this.showMessageErrorByCode(e.error.code);
}
}
const authenticate = async (credential) => {
try {
await firebase.auth().signInWithCredential(credential).then(result => {
const user = result.user;
user.getIdToken().then(accessToken => {
return accessToken;
});
})
} catch (error) {
console.log(error);
}
}
You should not mix async/await with the older version .then().
Just use it without then() like so:
export const confirmCode = async (verificationId, code) => {
try {
const credential = await firebase.auth.PhoneAuthProvider.credential(verificationId, code);
const accessToken = await authenticate(credential);
console.log(accessToken); // prints undefined as does not wait for above function call?
return accessToken;
} catch (error) {
console.log(error)
// this.showMessageErrorByCode(e.error.code);
}
}
const authenticate = async (credential) => {
try {
let result = await firebase.auth().signInWithCredential(credential); // <-- use await
const user = result.user;
accessToken = await user.getIdToken(); // <-- use await
return accessToken;
} catch (error) {
console.log(error);
}
}
For more detailed explanation, why your code does not work:
You are returning from within a .then() which is not possible
If you would want to use the old way of writing async functions, you would need to use:
return new Promise((resolve, reject) => { /* Code ... */ }); to wrap your function content
resolve(accessToken) instead of return
.then() and .catch() instead of await and try/catch
and some rejects where you can't resolve anything (so probably in the catch block)
But I would suggest you to use the async/await approach, as it is easier to read.
I've got into the pattern of using async await in my aws nodejs lambda functions, and I common thing I run into is the need to await the result of a promise and use the response in the next async/await promise, and sort of repeat this pattern until I've run all my logic.
let userId;
await GetUserId({AccessToken: headers.accesstoken}).then(function(res){
userId = res;
},function(err){
});
let username;
await GetUserName(userId).then(function(res){
username = res;
},function(err){
});
Is there anyway I can declare and assign userId a value in the same line as invoking the function.
sudo code:
let userId = await GetUserId().then(()=>{ //bubble response up to userId })
The reason I'm asking is that it just sort of messing/wrong initializing a variable separately. Maybe I need a different pattern, or it's just something I'll have to live with.
Solution
var ExampleFunction = async() => {
try {
const userId = await GetUserId('token');
const username = await GetUserName(userId);
console.log(`userId: ${userId}`);
console.log(`username: ${username}`);
} catch (err) {
console.log(err);
console.log(`Exit Function`);
}
function GetUserId(token) {
return new Promise(function(resolve, reject) {
if (!token)
reject('no token');
resolve('ID');
});
}
function GetUserName(userId) {
return new Promise(function(resolve, reject) {
if (!userId)
reject('no userId');
resolve('NAME');
});
}
}
ExampleFunction();
The await is supposed to replace the then syntax (except you really need to distinguish fulfillment from rejection with it). The await expression either throws the rejection reason as an exception to catch, or results in the fulfilment value of the promise. You can directly use that:
const userId = await GetUserId({AccessToken: headers.accesstoken});
const username = await GetUserName(userId);
(I assume that it was unintentional that your current code ignored errors and continued with undefined values).
The keyword await makes JavaScript wait until that promise resolves and returns its result.
let userId = await GetUserId({AccessToken: headers.accesstoken});
if you assign the result of await to var it will be the promise resolve value
so you can
let userId = await GetUserId({AccessToken: headers.accesstoken});
and
let username = await GetUserName(userId);
PS. don't forget error handling using try/catch.
Calling an async function in a pre save hook is returning me undefined for the password. Am I fundamentally misunderstanding async here? I've used it successfully in other areas of my app an it seems to be working fine...
userSchema.pre('save', function (next) {
let user = this
const saltRounds = 10;
const password = hashPassword(user)
user.password = password;
next();
})
async hashPassword(user) {
let newHash = await bcrypt.hash(password, saltRounds, function(err, hash) {
if (err) {
console.log(err)
}
return hash
});
return newHash
}
I think you'd need to handle the promise returned by hashPassword:
hashPassword(user)
.then(password => {user.password = password
next()}
)
I don't think you can turn userSchema.pre into an async function.
Mongoose hooks allow async functions in them. It worked for me. Note that the "next" callback parameter is not needed in async functions as the function executes synchronously.
Here is what a correct async/await version of the code posted in the question would be. Please note that the corrections are marked in bold:
userSchema.pre('save', async function () {
let user = this;
const saltRounds = 10;
const hashed_password = await hashPassword(user.password, saltRounds);
user.password = hashed_password;
}); // pre save hook ends here
async hashPassword(password, saltRounds) {
try {
let newHash = await bcrypt.hash(password, saltRounds);
} catch(err){
// error handling here
}
return newHash;
}
Just to clean things up a bit:
userSchema.pre('save', function(next) {
if (!this.isModified('password')) {
return next();
}
this.hashPassword(this.password)
.then((password) => {
this.password = password;
next();
});
});
userSchema.methods = {
hashPassword(password) {
return bcrypt.hash(password, 10);
},
}
let user = this can be dropped when using an arrow function in then.
When using bcrypt.hash() with no callback, it returns a promise.
async for hashPassword is redundant when using .then when calling
Following on from this question.
I feel like I'm almost there, but my incomplete understanding of async is preventing me from solving this. I'm basically trying to just hash a password using bcrypt and have decided to seperate out the hashPassword function so that I can potentially use it in other parts of the app.
hashedPassword keeps returning undefined though...
userSchema.pre('save', async function (next) {
let user = this
const password = user.password;
const hashedPassword = await hashPassword(user);
user.password = hashedPassword
next()
})
async function hashPassword (user) {
const password = user.password
const saltRounds = 10;
const hashedPassword = await bcrypt.hash(password, saltRounds, function(err, hash) {
if (err) {
return err;
}
return hash
});
return hashedPassword
}
await dosent wait for bcrypt.hash because bcrypt.hash does not
return a promise. Use the following method, which wraps bcrypt in a promise in order to use await.
async function hashPassword (user) {
const password = user.password
const saltRounds = 10;
const hashedPassword = await new Promise((resolve, reject) => {
bcrypt.hash(password, saltRounds, function(err, hash) {
if (err) reject(err)
resolve(hash)
});
})
return hashedPassword
}
Update:-
The library has added code to return a promise which will make the use
of async/await possible, which was not available
earlier. the new way of usage would be as follows.
const hashedPassword = await bcrypt.hash(password, saltRounds)
By default, bcrypt.hash(password,10) will return as promise. please check here
Example: Run the code,
var bcrypt= require('bcrypt');
let password = "12345";
var hashPassword = async function(){
console.log(bcrypt.hash(password,10));
var hashPwd = await bcrypt.hash(password,10);
console.log(hashPwd);
}
hashPassword();
Output:
Promise { <pending> }
$2b$10$8Y5Oj329TeEh8weYpJA6EOE39AA/BXVFOEUn1YOFC.sf1chUi4H8i
When you use await inside the async function, it will wait untill it get resolved from the promise.
use The method bcrypt.hashSync(), It is Synchronous out of the box.
const hashedPassword = bcrypt.hashSync(password,saltRounds);
Hashing bcrypt asynchronously should be like this
bcrypt.hash(password, saltRounds, function(err, hash) {
if (err) {
throw err;
}
// Do whatever you like with the hash
});
If you are confused with sync and async. You need to read more about them.
There are a lot of good articles out there.
You need to look here in the documentation.
Async methods that accept a callback, return a Promise when callback
is not specified if Promise support is available.
So, if your function call takes in a callback then you can't use await on it since this function signature doesn't return a Promise. In order to use await you need to remove the callback function. You can also wrap it in a Promise and await on it but that's a bit overkill since the library already provides a mechanism to do so.
Code refactor:
try {
// I removed the callbackFn argument
const hashedPassword = await bcrypt.hash(password, saltRounds)
} catch (e) {
console.log(e)
}
const hashedPassword = (password, salt) => {
return new Promise((resolve, reject) => {
bcrpyt.hash(password, salt, (err, hash) => {
if (err) reject();
resolve(hash);
});
});
};
hashedPassword('password', 10).then((passwordHash) => {
console.log(passwordHash);
});
Had same issue...
solved by assigning the Boolean value from a function:
compareHash = (password, hashedPassword) => {
if (!password || !hashedPassword) {
return Promise.resolve(false);
}
return bcrypt.compare(password, hashedPassword);
};
Here the 2 arguments will not be undefined, which is the cause of issue.
And calling the function:
let hashCompare = this.compareHash(model.password, entity.password);