Loopback context is cleared on calling bcrypt to compare password
Bug
My loopback context is lost if I call bcrypt compare function bcrypt.compare(currentPassword, user.password, function(err, isMatch) {
});
- [x] Bug
- [ ] Feature request
Description of feature (or steps to reproduce if bug)
When I tried to call the bcrypt within any model of my loopback application
//Loopback context available here
var LoopBackContext = require('loopback-context');
var context = LoopBackContext.getCurrentContext();
console.log(context); //Loopback context available here
bcrypt.compare(currentPassword, user.password, function(err, isMatch) {
//Loopback context available here
var LoopBackContext = require('loopback-context');
var context = LoopBackContext.getCurrentContext();
console.log(context); //Loopback context available here gives null ?
if (isMatch) {
//Perform my operation.
}
});
//Loopback context available here
var LoopBackContext = require('loopback-context');
var context = LoopBackContext.getCurrentContext();
console.log(context); //Loopback context available here gives null ?
Or If I use the inbuilt method to compare password the same thing goes
//Loopback context available here
var LoopBackContext = require('loopback-context');
var context = LoopBackContext.getCurrentContext();
console.log(context); //Loopback context available here
userObject.hasPassword((currentPassword, function(err, isMatch) {
//Loopback context available here
var LoopBackContext = require('loopback-context');
var context = LoopBackContext.getCurrentContext();
console.log(context); //Loopback context available here gives null ?
if (isMatch) {
//Perform my operation.
}
});
//Loopback context available here
var LoopBackContext = require('loopback-context');
var context = LoopBackContext.getCurrentContext();
console.log(context); //Loopback context available here gives null ?
After a bit research I found the the the internal method of loopback hasPassword for user Model uses the bcrypt library and the same goes there too !
Additional information (Node.js version, LoopBack version, etc)
"loopback": "^3.2.1",
"loopback-boot": "^2.23.0",
"loopback-component-explorer": "^4.0.0",
"loopback-connector-mongodb": "1.15.1",
"loopback-context": "^3.0.0",
Hi, I'm currently experimenting with a solution for another issue, which I hope could work for this one too, if it works and when it's ready. See also https://github.com/strongloop/loopback-context/issues/21#issuecomment-284673999
Provided that bcrypt is the cause of the issue (I didn't try to reproduce it yet), another solution could be to shim the NPM module which is causing the issue (bcrypt in this case), in order to make it work fine with cls-hooked.
(It is a fork of continuation-local-storage used by loopback-context).
This is a common approach. The problem is that cls-bcrypt (the shimmed version of bcrypt) looks unmantained.
If you wish, you could fork cls-bcrypt, update its dependencies at least, and if it still works, and tests still pass, then publish it on NPM under a slightly different name (don't forget to cite the original project in your README).
And then you could submit a PR to loopback, where you replace the dependency bcrypt with your published cls-bcrypt-whatever.
Also, this is less general than the solution I'm working on, but I would find it perfectly fine anyway.
But I'm not a community maintainer of loopback, so I suggest you to ask to a Loopback maintainer before you start to work on all this stuff.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.