Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Tests

on:
push:
branches: ["**"]
pull_request:
branches: ["**"]

jobs:
test:
name: Node.js ${{ matrix.node-version }} / MongoDB ${{ matrix.mongodb-version }}
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x, 22.x]
mongodb-version: ["6.0", "7.0", "8.0"]

permissions:
contents: read

services:
mongodb:
image: mongo:${{ matrix.mongodb-version }}
ports:
- 27017:27017

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: "npm"

- name: Install dependencies
run: npm ci

- name: Run tests (unit + integration)
run: npm test
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
test:
@./node_modules/.bin/mocha --reporter spec
@./node_modules/.bin/jshint


.PHONY: test
13 changes: 5 additions & 8 deletions lib/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ module.exports = function (schema, options) {
return model.findOne(queryParameters);
};
const schemaFields = {};
if (!schema.path(options.usernameField)) {
schemaFields[options.usernameField] = {type: String, unique: options.usernameUnique};
}
if (!schema.path(options.usernameField)) {
schemaFields[options.usernameField] = {
type: String,
Expand All @@ -37,15 +34,15 @@ module.exports = function (schema, options) {
full_name += this['first_name'] + " ";
if (this['last_name'])
full_name += this['last_name'];
return full_name;
return full_name.trim();
};


schema.pre("save", function (next) {
if (this.isNew) {
this.username= this.username.toLowerCase();
schema.pre("save", function () {
var usernameField = options.usernameField;
if (this.isNew && usernameField && typeof this[usernameField] === 'string') {
this[usernameField] = this[usernameField].toLowerCase();
}
return next();
});


Expand Down
24 changes: 11 additions & 13 deletions lib/email.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const uuidv4 = require('uuid/v4');
const { v4: uuidv4 } = require('uuid');


module.exports = function (schema) {
Expand Down Expand Up @@ -46,15 +46,15 @@ module.exports = function (schema) {
});
};

schema.methods.setPrimaryEmail = function (email) {
schema.methods.setPrimaryEmail = async function (email) {
var currentPrimaryEmail = this['email'];
var newPrimaryEmail = this.getEmail(email);

if (!newPrimaryEmail) throw new Error('Email does not exist');

if (currentPrimaryEmail !== newPrimaryEmail) {
if (currentPrimaryEmail !== email) {
this['email'] = email;
this.save();
await this.save();
}
return newPrimaryEmail;
};
Expand All @@ -69,20 +69,18 @@ module.exports = function (schema) {
return false;
}
};
schema.methods.sendVerificationToken = function (email, callback) {

schema.methods.sendVerificationToken = async function (email) {
var email_doc = (typeof email === 'string' ? this.getEmail(email) : email);
if (!email_doc) {
return callback(email, null);
throw new Error('Email not found');
}
if(email_doc.verified === true){
return callback(null, null);
if (email_doc.verified === true) {
return null;
}
if (email_doc.token === undefined) {
return callback(email, null);
throw new Error('Token not available');
}


return callback(email_doc.address, email_doc.token);
return {address: email_doc.address, token: email_doc.token};
};
Comment on lines +73 to 85

Copilot AI Mar 31, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sendVerificationToken was changed from a callback-style API to an async method that throws/returns values. This is a breaking API change for library consumers and also diverges from the documented usage. Consider supporting both forms (e.g., accept an optional callback) or bumping the major version and updating documentation accordingly.

Copilot uses AI. Check for mistakes.
};

20 changes: 9 additions & 11 deletions lib/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,22 @@ module.exports = function (model) {
}
const User = model;

return function (req, res, next) {
User.findOne({'emails.token': req.params.token}, 'emails', function (err, user) {
if (err) {
next(err);
return;
}
return async function (req, res, next) {
try {
const user = await User.findOne({'emails.token': req.params.token}, 'emails');
if (user === null) {
next(new Error('Token invalid'));
return;
return next(new Error('Invalid token'));
}
var email = user.emails.find(function (email) {
return email['token'] === req.params.token;
});

email.token = undefined;
email.verified = true;
user.save();
next();
});
await user.save();
return next();
} catch (err) {
return next(err);
}
};
};
Loading
Loading