async findUserByEmail(email) { return await User.query().first().where({ email }) }
static getFindOneResolver(options = {}) { return (async (root, args) => { if (args[this.idColumn]) { return this.loadById(args[this.idColumn], options); } const keys = Object.keys(args); if (!keys.length) return null; if (keys.length === 1) { return this.loadByColumn(keys[0], args[keys[0]], options); } const query = this.query(); keys.forEach((key) => { query.where(key, args[key]); }); return query.first(); }); }
// eslint-disable-next-line arrow-body-style const authorize = () => { return async (req, res, next) => { if (res.locals.user.exists) { try { const person = await User.query().first().where({ email: req.body.email }); const { fname, lname, email, role, password: hash } = person; const passwordIsValid = await bcrypt.compareSync(req.body.password, hash); const userData = { authorized: passwordIsValid, fname, lname, email, role, }; Object.assign(res.locals.user, userData); } catch (err) { res.locals.user = { authorized: false, authmessage: err }; } } else { res.locals.user.authorized = null; } next(); }; }