// Returns a promise for whether this password compares to equal this // hashed password. function compare(password, hashedPassword) { // Cannot bcrypt compare when one is undefined if (!password || !hashedPassword) { return Promise.resolve(false); } return bcrypt.compare(password, hashedPassword); }
bcrypt.compare(_password, this.password, function (err, isMatch) { if (err) return cb(err) cb(null, isMatch) })
function getLocalStrategy(db, settings) { const localOptions = { usernameField: settings.usernameField || 'email', passwordField: settings.passwordField || 'password', session: false, }; const localStrategy = new LocalStrategy( localOptions, (email, password, done) => { db.findOne(settings.usersTable || 'users', { email }) .then((user) => { if (!user) { return done(null, false, { message: 'This email is not registered', }); } return bcrypt.compare(password, user.password, (err, isMatch) => { if (err || !isMatch) { return done(null, false, { message: 'Incorrect credentials' }); } return done(null, user); }); }) .catch((err) => { done(err, null, { message: 'Error connecting to database' }); }); } ); return localStrategy; }
const bcryptNew = require('bcryptjs'); bcryptOriginal.hash('my1Long:password', null, null, function(err, res) { bcryptNew.compare('my1Long:password', res, function(err, res) { expect(res).toBeTruthy(); done();
async login(parent, { email, password }, context) { const user = await context.prisma.user({ email }) if (!user) { throw new Error(`No user found for email: ${email}`) } const passwordValid = await bcrypt.compare(password, user.password) if (!passwordValid) { throw new Error('Invalid password') } return { token: jwt.sign({ userId: user.id }, process.env.APP_SECRET), user, } }
/** * Methods */ userSchema.method({ transform() { const transformed = {}; const fields = ['id', 'name', 'email', 'picture', 'role', 'createdAt']; fields.forEach((field) => { transformed[field] = this[field]; }); return transformed; }, token() { const playload = { exp: moment().add(jwtExpirationInterval, 'minutes').unix(), iat: moment().unix(), sub: this._id, }; return jwt.encode(playload, jwtSecret); }, async passwordMatches(password) { return bcrypt.compare(password, this.password); }, });
// Login Router registrationRoutes.route(RouteNames.login).post(function(req, res) { Registration.findOne({ user_name: req.body.user_name }) .then(user => { console.log("User from login", user) if (!user) res.sendStatus(204); else { bcrypt.compare(req.body.password, user.password) .then(passwordMatch => passwordMatch ? res.sendStatus(200) : res.sendStatus(204)) } }); });
//check for existing user User.findOne({ email }).then((user) => { if (!user) return res.status(400).json("Incorrect Email or Password"); // Validate password bcrypt.compare(password, user.password).then((isMatch) => { if (!isMatch) return res.status(400).json("Incorrect Email or Password"); const sessUser = { id: user.id, name: user.name, email: user.email }; req.session.user = sessUser; // Auto saves session data in mongo store res.json(sessUser); // sends cookie with sessionID automatically in response }); });
function matchPasswordHash(textPassword, hash) { console.log(textPassword, hash); bcrypt .compare(textPassword, hash) .then(res => console.log(res)) .catch(err => console.error(err)); }
//Match the Password bcrypt.compare(password.toString(), user.password, (err, isMatch) => { if (err) throw err; if (isMatch) { resolve(user); } else { //Password didn't match reject("Authentication Failed"); } });
const comparePassword = async (password, dbPassword) => { try { const match = await bcrypt.compare(password, dbPassword); if (!match) { throw new Error('Authentication error'); } return match; } catch (error) { throw new errors.Unauthorized('Wrong password.'); } }
function compare(string, hashValue) { return new Promise((resolve, reject) => { bcrypt.compare(string, hashValue, (err, result) => { if (!result || err) { reject({ status: 403, content: 'Incorrect Password' }); } else { resolve(); } }); }); }
bcrypt.compare(candidatePassword, hash, function (err, isMatch) { if (err) throw err; callback(null, isMatch); });
/** * @param {** decrypt password in case user login implementation} payloadPassword * @param {*} userPassword * @param {*} cb */ let decryptPswrd = (payloadPassword, userPassword, cb) => { BCRYPT.compare(payloadPassword, userPassword, (err, isMatched) => { return err ? cb(err) : cb(null, isMatched); }); }
checkPassword(givenPassword, hash) { return new Promise((resolve, reject) => { bcrypt.compare(givenPassword, hash, (err, isMatch) => { if (err || !isMatch) reject(err); resolve(); }); }); }