Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/**
* Get configuration
*/
const conf = getConfig(config.resources[resourceName].auth.magicLink);
const MagicTokens = db.collection(conf.collection);
const User = db.collection(resourceName);
/**
* Verify email and tokens
*/
const email = (req.body[conf.emailField] || '').toLowerCase();
if (!email) throw new HttpError.BadRequest('Missing Email');
if (!isEmail(email)) throw new HttpError.BadRequest('Invalid Email');
const tokens = await MagicTokens.find({ email, exp: { $gte: new Date() } }).toArray();
if (tokens.length >= conf.max) throw new HttpError.TooManyRequests('Token limit reached');
/**
* Handle custom logic
*/
const user = await User.findOne({ [conf.emailField]: email });
const _id = !user ? new ObjectId().toString():false;
if (conf.doGenerate) {
await conf.doGenerate({
req, res, next, user, HttpError, db, resourceName, newUserId: _id,
});
}
/**
* Create and store token, also create user if it didnt exist
*/
const token = generate(50) + new ObjectId().toString();
const search = generate(50) + new ObjectId().toString();
tooManyRequests: function tooManyRequests (message) {
return new createError.TooManyRequests(message)
},
/**
* Get configuration
*/
const conf = getConfig(config.resources[resourceName].auth.magicCode);
const MagicTokens = db.collection(conf.collection);
const User = db.collection(resourceName);
/**
* Verify email and tokens
*/
const email = (req.body[conf.emailField] || '').toLowerCase();
if (!email) throw new HttpError.BadRequest('Missing Email');
if (!isEmail(email)) throw new HttpError.BadRequest('Invalid Email');
const tokens = await MagicTokens.find({ email, exp: { $gte: new Date() } }).toArray();
if (tokens.length >= conf.max) throw new HttpError.TooManyRequests('Token limit reached');
/**
* Handle custom logic
*/
const user = await User.findOne({ [conf.emailField]: email });
const _id = !user ? new ObjectId().toString():false;
if (conf.doGenerate) {
await conf.doGenerate({
req, res, next, user, HttpError, db, resourceName, newUserId: _id,
});
}
/**
* Create and store token, also create user if it didnt exist
*/
const token = generate({
length: 4,