Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@accounts/server

Package Overview
Dependencies
Maintainers
4
Versions
204
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@accounts/server - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5-alpha.451f3013

flow-typed/npm/bcryptjs_vx.x.x.js

602

lib/AccountsServer.js

@@ -28,2 +28,10 @@ 'use strict';

var _email = require('./email');
var _email2 = _interopRequireDefault(_email);
var _emailTemplates = require('./emailTemplates');
var _emailTemplates2 = _interopRequireDefault(_emailTemplates);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -36,15 +44,52 @@

var AccountsServer = exports.AccountsServer = function () {
function AccountsServer(options, db) {
function AccountsServer() {
_classCallCheck(this, AccountsServer);
}
this.options = options;
if (!db) {
throw new _common.AccountsError('A database driver is required');
_createClass(AccountsServer, [{
key: 'config',
/**
* @description Configure AccountsServer.
* @param {Object} options - Options for AccountsServer.
* @param {Object} db - DBInterface for AccountsServer.
* @returns {Object} - Return the options.
*/
value: function config(options, db) {
this._options = _extends({}, _config3.default, options);
if (!db) {
throw new _common.AccountsError('A database driver is required');
}
this.db = db;
if (options.sendMail) {
this.email = { sendMail: options.sendMail };
} else {
this.email = new _email2.default(_config3.default.email);
}
this.emailTemplates = _emailTemplates2.default;
}
this.db = db;
}
// eslint-disable-next-line max-len
/**
* @description Return the AccountsServer options.
* @returns {Object} - Return the options.
*/
_createClass(AccountsServer, [{
}, {
key: 'options',
value: function options() {
return this._options;
}
/**
* @description Login the user with his password.
* @param {Object} user - User to login.
* @param {string} password - Password of user to login.
* @param {string} ip - User ip.
* @param {string} userAgent - User user agent.
* @returns {Promise<Object>} - LoginReturnType.
*/
// eslint-disable-next-line max-len
}, {
key: 'loginWithPassword',

@@ -77,3 +122,3 @@ value: function () {

if (!this.options.passwordAuthenticator) {
if (!this._options.passwordAuthenticator) {
_context.next = 17;

@@ -85,3 +130,3 @@ break;

_context.next = 9;
return this._externalPasswordAuthenticator(this.options.passwordAuthenticator, user, password);
return this._externalPasswordAuthenticator(this._options.passwordAuthenticator, user, password);

@@ -279,2 +324,9 @@ case 9:

}()
/**
* @description Create a new user.
* @param {Object} user - The user object.
* @returns {Promise<string>} - Return the id of user created.
*/
}, {

@@ -370,2 +422,11 @@ key: 'createUser',

}()
/**
* @description Refresh a user token.
* @param {string} accessToken - User access token.
* @param {string} refreshToken - User refresh token.
* @param {string} ip - User ip.
* @param {string} userAgent - User user agent.
* @returns {Promise<Object>} - LoginReturnType.
*/
// eslint-disable-next-line max-len

@@ -393,4 +454,4 @@

_jsonwebtoken2.default.verify(refreshToken, this.options.tokenSecret);
decodedAccessToken = _jsonwebtoken2.default.verify(accessToken, this.options.tokenSecret, {
_jsonwebtoken2.default.verify(refreshToken, this._options.tokenSecret);
decodedAccessToken = _jsonwebtoken2.default.verify(accessToken, this._options.tokenSecret, {
ignoreExpiration: true

@@ -470,6 +531,13 @@ });

}()
/**
* @description Refresh a user token.
* @param {string} sessionId - User session id.
* @returns {Promise<Object>} - Return a new accessToken and refreshToken.
*/
}, {
key: 'createTokens',
value: function createTokens(sessionId) {
var _options = this.options,
var _options = this._options,
tokenSecret = _options.tokenSecret,

@@ -491,2 +559,9 @@ tokenConfigs = _options.tokenConfigs;

}
/**
* @description Logout a user and invalidate his session.
* @param {string} accessToken - User access token.
* @returns {Promise<void>} - Return a promise.
*/
}, {

@@ -584,3 +659,3 @@ key: 'logout',

case 9:
if (!this.options.resumeSessionValidator) {
if (!this._options.resumeSessionValidator) {
_context7.next = 18;

@@ -592,3 +667,3 @@ break;

_context7.next = 13;
return this.options.resumeSessionValidator(user, session);
return this._options.resumeSessionValidator(user, session);

@@ -643,3 +718,3 @@ case 13:

_context8.prev = 3;
decodedAccessToken = _jsonwebtoken2.default.verify(accessToken, this.options.tokenSecret);
decodedAccessToken = _jsonwebtoken2.default.verify(accessToken, this._options.tokenSecret);

@@ -686,2 +761,9 @@ sessionId = decodedAccessToken.data.sessionId;

}()
/**
* @description Find a user by one of his emails.
* @param {string} email - User email.
* @returns {Promise<Object>} - Return a user or null if not found.
*/
}, {

@@ -692,2 +774,9 @@ key: 'findUserByEmail',

}
/**
* @description Find a user by his username.
* @param {string} username - User username.
* @returns {Promise<Object>} - Return a user or null if not found.
*/
}, {

@@ -698,2 +787,9 @@ key: 'findUserByUsername',

}
/**
* @description Find a user by his id.
* @param {string} userId - User id.
* @returns {Promise<Object>} - Return a user or null if not found.
*/
}, {

@@ -704,2 +800,13 @@ key: 'findUserById',

}
/**
* @description Add an email address for a user.
* Use this instead of directly updating the database.
* @param {string} userId - User id.
* @param {string} newEmail - A new email address for the user.
* @param {boolean} [verified] - Whether the new email address should be marked as verified.
* Defaults to false.
* @returns {Promise<void>} - Return a Promise.
*/
}, {

@@ -710,2 +817,11 @@ key: 'addEmail',

}
/**
* @description Remove an email address for a user.
* Use this instead of directly updating the database.
* @param {string} userId - User id.
* @param {string} email - The email address to remove.
* @returns {Promise<void>} - Return a Promise.
*/
}, {

@@ -716,2 +832,9 @@ key: 'removeEmail',

}
/**
* @description Marks the user's email address as verified.
* @param {string} token - The token retrieved from the verification URL.
* @returns {Promise<void>} - Return a Promise.
*/
}, {

@@ -721,3 +844,3 @@ key: 'verifyEmail',

var _ref10 = _asyncToGenerator(regeneratorRuntime.mark(function _callee9(token) {
var user, tokenRecord, emailRecord;
var user, verificationTokens, tokenRecord, emailRecord;
return regeneratorRuntime.wrap(function _callee9$(_context9) {

@@ -728,3 +851,3 @@ while (1) {

_context9.next = 2;
return this.db.findUserByEmailVerificationToken();
return this.db.findUserByEmailVerificationToken(token);

@@ -742,3 +865,4 @@ case 2:

case 5:
tokenRecord = (0, _lodash.find)(user.services.email.verificationTokens, function (t) {
verificationTokens = (0, _lodash.get)(user, ['services', 'email', 'verificationTokens'], []);
tokenRecord = (0, _lodash.find)(verificationTokens, function (t) {
return t.token === token;

@@ -748,3 +872,3 @@ });

if (tokenRecord) {
_context9.next = 8;
_context9.next = 9;
break;

@@ -755,3 +879,4 @@ }

case 8:
case 9:
// TODO check time for expiry date
emailRecord = (0, _lodash.find)(user.emails, function (e) {

@@ -762,3 +887,3 @@ return e.address === tokenRecord.address;

if (emailRecord) {
_context9.next = 11;
_context9.next = 12;
break;

@@ -769,7 +894,7 @@ }

case 11:
_context9.next = 13;
return this.db.verifyEmail(user.id, emailRecord);
case 12:
_context9.next = 14;
return this.db.verifyEmail(user.id, emailRecord.address);
case 13:
case 14:
case 'end':

@@ -788,12 +913,15 @@ return _context9.stop();

}()
/**
* @description Reset the password for a user using a token received in email.
* @param {string} token - The token retrieved from the reset password URL.
* @param {string} newPassword - A new password for the user.
* @returns {Promise<void>} - Return a Promise.
*/
}, {
key: 'setPassword',
value: function setPassword(userId, newPassword) {
return this.db.setPasssword(userId, newPassword);
}
}, {
key: 'setProfile',
key: 'resetPassword',
value: function () {
var _ref11 = _asyncToGenerator(regeneratorRuntime.mark(function _callee10(userId, profile) {
var user;
var _ref11 = _asyncToGenerator(regeneratorRuntime.mark(function _callee10(token, newPassword) {
var user, resetTokens, resetTokenRecord, emails;
return regeneratorRuntime.wrap(function _callee10$(_context10) {

@@ -804,3 +932,3 @@ while (1) {

_context10.next = 2;
return this.db.findUserById(userId);
return this.db.findUserByResetPasswordToken(token);

@@ -815,9 +943,39 @@ case 2:

throw new _common.AccountsError('User not found', { id: userId });
throw new _common.AccountsError('Reset password link expired');
case 5:
_context10.next = 7;
return this.db.setProfile(userId, profile);
resetTokens = (0, _lodash.get)(user, ['services', 'password', 'resetTokens']);
resetTokenRecord = (0, _lodash.find)(resetTokens, function (t) {
return t.token === token;
});
case 7:
if (resetTokenRecord) {
_context10.next = 9;
break;
}
throw new _common.AccountsError('Reset password link expired');
case 9:
// TODO check time for expiry date
emails = user.emails || [];
if ((0, _lodash.includes)(emails.map(function (email) {
return email.address;
}), resetTokenRecord.email)) {
_context10.next = 12;
break;
}
throw new _common.AccountsError('Token has invalid email address');
case 12:
_context10.next = 14;
return this.db.setResetPasssword(user.id, resetTokenRecord.email, newPassword, token);
case 14:
// Changing the password should invalidate existing sessions
this.db.invalidateAllSessions(user.id);
case 15:
case 'end':

@@ -830,13 +988,34 @@ return _context10.stop();

function setProfile(_x19, _x20) {
function resetPassword(_x19, _x20) {
return _ref11.apply(this, arguments);
}
return setProfile;
return resetPassword;
}()
/**
* @description Change the password for a user.
* @param {string} userId - User id.
* @param {string} newPassword - A new password for the user.
* @returns {Promise<void>} - Return a Promise.
*/
}, {
key: 'updateProfile',
key: 'setPassword',
value: function setPassword(userId, newPassword) {
return this.db.setPasssword(userId, newPassword);
}
/**
* @description Change the profile for a user.
* @param {string} userId - User id.
* @param {Object} profile - The new user profile.
* @returns {Promise<void>} - Return a Promise.
*/
}, {
key: 'setProfile',
value: function () {
var _ref12 = _asyncToGenerator(regeneratorRuntime.mark(function _callee11(userId, profile) {
var user, res;
var user;
return regeneratorRuntime.wrap(function _callee11$(_context11) {

@@ -861,9 +1040,5 @@ while (1) {

_context11.next = 7;
return this.db.setProfile(userId, _extends({}, user.profile, profile));
return this.db.setProfile(userId, profile);
case 7:
res = _context11.sent;
return _context11.abrupt('return', res);
case 9:
case 'end':

@@ -876,8 +1051,283 @@ return _context11.stop();

function updateProfile(_x21, _x22) {
function setProfile(_x21, _x22) {
return _ref12.apply(this, arguments);
}
return setProfile;
}()
/**
* @description Update the profile for a user,
* the new profile will be added to the existing one.
* @param {string} userId - User id.
* @param {Object} profile - User profile to add.
* @returns {Promise<Object>} - Return a Promise.
*/
}, {
key: 'updateProfile',
value: function () {
var _ref13 = _asyncToGenerator(regeneratorRuntime.mark(function _callee12(userId, profile) {
var user, res;
return regeneratorRuntime.wrap(function _callee12$(_context12) {
while (1) {
switch (_context12.prev = _context12.next) {
case 0:
_context12.next = 2;
return this.db.findUserById(userId);
case 2:
user = _context12.sent;
if (user) {
_context12.next = 5;
break;
}
throw new _common.AccountsError('User not found', { id: userId });
case 5:
_context12.next = 7;
return this.db.setProfile(userId, _extends({}, user.profile, profile));
case 7:
res = _context12.sent;
return _context12.abrupt('return', res);
case 9:
case 'end':
return _context12.stop();
}
}
}, _callee12, this);
}));
function updateProfile(_x23, _x24) {
return _ref13.apply(this, arguments);
}
return updateProfile;
}()
/**
* @description Send an email with a link the user can use verify their email address.
* @param {string} userId - The id of the user to send email to.
* @param {string} [address] - Which address of the user's to send the email to.
* This address must be in the user's emails list.
* Defaults to the first unverified email in the list.
* @returns {Promise<void>} - Return a Promise.
*/
}, {
key: 'sendVerificationEmail',
value: function () {
var _ref14 = _asyncToGenerator(regeneratorRuntime.mark(function _callee13(userId, address) {
var user, email, emails, token, verifyEmailUrl;
return regeneratorRuntime.wrap(function _callee13$(_context13) {
while (1) {
switch (_context13.prev = _context13.next) {
case 0:
_context13.next = 2;
return this.db.findUserById(userId);
case 2:
user = _context13.sent;
if (user) {
_context13.next = 5;
break;
}
throw new _common.AccountsError('User not found', { id: userId });
case 5:
// If no address provided find the first unverified email
if (!address) {
email = (0, _lodash.find)(user.emails, function (e) {
return !e.verified;
});
address = email && email.address; // eslint-disable-line no-param-reassign
}
// Make sure the address is valid
emails = user.emails || [];
if (!(!address || !(0, _lodash.includes)(emails.map(function (email) {
return email.address;
}), address))) {
_context13.next = 9;
break;
}
throw new _common.AccountsError('No such email address for user');
case 9:
token = (0, _tokens.generateRandomToken)();
_context13.next = 12;
return this.db.addEmailVerificationToken(userId, address, token);
case 12:
verifyEmailUrl = this._options.siteUrl + '/verify-email/' + token;
_context13.next = 15;
return this.email.sendMail({
from: this.emailTemplates.verifyEmail.from ? this.emailTemplates.verifyEmail.from : this.emailTemplates.from,
to: address,
subject: this.emailTemplates.verifyEmail.subject(user),
text: this.emailTemplates.verifyEmail.text(user, verifyEmailUrl)
});
case 15:
case 'end':
return _context13.stop();
}
}
}, _callee13, this);
}));
function sendVerificationEmail(_x25, _x26) {
return _ref14.apply(this, arguments);
}
return sendVerificationEmail;
}()
/**
* @description Send an email with a link the user can use to reset their password.
* @param {string} userId - The id of the user to send email to.
* @param {string} [address] - Which address of the user's to send the email to.
* This address must be in the user's emails list.
* Defaults to the first email in the list.
* @returns {Promise<void>} - Return a Promise.
*/
}, {
key: 'sendResetPasswordEmail',
value: function () {
var _ref15 = _asyncToGenerator(regeneratorRuntime.mark(function _callee14(userId, address) {
var user, token, resetPasswordUrl;
return regeneratorRuntime.wrap(function _callee14$(_context14) {
while (1) {
switch (_context14.prev = _context14.next) {
case 0:
_context14.next = 2;
return this.db.findUserById(userId);
case 2:
user = _context14.sent;
if (user) {
_context14.next = 5;
break;
}
throw new _common.AccountsError('User not found', { id: userId });
case 5:
address = this._getFirstUserEmail(user, address); // eslint-disable-line no-param-reassign
token = (0, _tokens.generateRandomToken)();
_context14.next = 9;
return this.db.addResetPasswordToken(userId, address, token);
case 9:
resetPasswordUrl = this._options.siteUrl + '/reset-password/' + token;
_context14.next = 12;
return this.email.sendMail({
from: this.emailTemplates.resetPassword.from ? this.emailTemplates.resetPassword.from : this.emailTemplates.from,
to: address,
subject: this.emailTemplates.resetPassword.subject(user),
text: this.emailTemplates.resetPassword.text(user, resetPasswordUrl)
});
case 12:
case 'end':
return _context14.stop();
}
}
}, _callee14, this);
}));
function sendResetPasswordEmail(_x27, _x28) {
return _ref15.apply(this, arguments);
}
return sendResetPasswordEmail;
}()
/**
* @description Send an email with a link the user can use to set their initial password.
* @param {string} userId - The id of the user to send email to.
* @param {string} [address] - Which address of the user's to send the email to.
* This address must be in the user's emails list.
* Defaults to the first email in the list.
* @returns {Promise<void>} - Return a Promise.
*/
}, {
key: 'sendEnrollmentEmail',
value: function () {
var _ref16 = _asyncToGenerator(regeneratorRuntime.mark(function _callee15(userId, address) {
var user, token, enrollAccountUrl;
return regeneratorRuntime.wrap(function _callee15$(_context15) {
while (1) {
switch (_context15.prev = _context15.next) {
case 0:
_context15.next = 2;
return this.db.findUserById(userId);
case 2:
user = _context15.sent;
if (user) {
_context15.next = 5;
break;
}
throw new _common.AccountsError('User not found', { id: userId });
case 5:
address = this._getFirstUserEmail(user, address); // eslint-disable-line no-param-reassign
token = (0, _tokens.generateRandomToken)();
_context15.next = 9;
return this.db.addResetPasswordToken(userId, address, token, 'enroll');
case 9:
enrollAccountUrl = this._options.siteUrl + '/enroll-account/' + token;
_context15.next = 12;
return this.email.sendMail({
from: this.emailTemplates.enrollAccount.from ? this.emailTemplates.enrollAccount.from : this.emailTemplates.from,
to: address,
subject: this.emailTemplates.enrollAccount.subject(user),
text: this.emailTemplates.enrollAccount.text(user, enrollAccountUrl)
});
case 12:
case 'end':
return _context15.stop();
}
}
}, _callee15, this);
}));
function sendEnrollmentEmail(_x29, _x30) {
return _ref16.apply(this, arguments);
}
return sendEnrollmentEmail;
}()
}, {
key: '_getFirstUserEmail',
value: function _getFirstUserEmail(user, address) {
// Pick the first email if we weren't passed an email
if (!address && user.emails && user.emails[0]) {
address = user.emails[0].address; // eslint-disable-line no-param-reassign
}
// Make sure the address is valid
var emails = user.emails || [];
if (!address || !(0, _lodash.includes)(emails.map(function (email) {
return email.address;
}), address)) {
throw new _common.AccountsError('No such email address for user');
}
return address;
}
}]);

@@ -888,54 +1338,2 @@

var Accounts = {
instance: AccountsServer,
config: function config(options, db) {
this.instance = new AccountsServer(_extends({}, _config3.default, options), db);
},
options: function options() {
return this.instance.options;
},
loginWithPassword: function loginWithPassword(user, password, ip, userAgent) {
return this.instance.loginWithPassword(user, password, ip, userAgent);
},
createUser: function createUser(user) {
return this.instance.createUser(user);
},
findUserByEmail: function findUserByEmail(email, onlyId) {
return this.instance.findUserByEmail(email, onlyId);
},
findUserByUsername: function findUserByUsername(username, onlyId) {
return this.instance.findUserByUsername(username, onlyId);
},
findUserById: function findUserById(userId) {
return this.instance.findUserById(userId);
},
addEmail: function addEmail(userId, newEmail, verified) {
return this.instance.addEmail(userId, newEmail, verified);
},
removeEmail: function removeEmail(userId, newEmail) {
return this.instance.removeEmail(userId, newEmail);
},
verifyEmail: function verifyEmail(token) {
return this.instance.verifyEmail(token);
},
setPassword: function setPassword(userId, newPassword) {
return this.instance.setPassword(userId, newPassword);
},
refreshTokens: function refreshTokens(accessToken, refreshToken, ip, userAgent) {
return this.instance.refreshTokens(accessToken, refreshToken, ip, userAgent);
},
logout: function logout(accessToken) {
return this.instance.logout(accessToken);
},
resumeSession: function resumeSession(accessToken) {
return this.instance.resumeSession(accessToken);
},
setProfile: function setProfile(userId, profile) {
return this.instance.setProfile(userId, profile);
},
updateProfile: function updateProfile(userId, profile) {
return this.instance.updateProfile(userId, profile);
}
};
exports.default = Accounts;
exports.default = new AccountsServer();

@@ -11,4 +11,2 @@ 'use strict';

var _AccountsServer2 = _interopRequireDefault(_AccountsServer);
var _encryption = require('./encryption');

@@ -20,11 +18,14 @@

var Accounts = void 0;
describe('Accounts', function () {
beforeEach(function () {
_AccountsServer2.default.config({}, {});
Accounts = new _AccountsServer.AccountsServer();
Accounts.config({}, {});
});
describe('config', function () {
beforeEach(function () {});
it('requires a db driver', function () {
try {
_AccountsServer2.default.config();
Accounts.config();
throw new Error();
} catch (err) {

@@ -39,4 +40,4 @@ var message = err.message;

var db = {};
_AccountsServer2.default.config({}, db);
expect(_AccountsServer2.default.instance.db).toEqual(db);
Accounts.config({}, db);
expect(Accounts.db).toEqual(db);
});

@@ -46,4 +47,4 @@

var db = {};
_AccountsServer2.default.config({ passwordAuthenticator: function passwordAuthenticator() {} }, db);
expect(_AccountsServer2.default.instance.options.passwordAuthenticator).toBeDefined();
Accounts.config({ passwordAuthenticator: function passwordAuthenticator() {} }, db);
expect(Accounts._options.passwordAuthenticator).toBeDefined();
});

@@ -53,6 +54,15 @@

var db = {};
_AccountsServer2.default.config({}, db);
expect(_AccountsServer2.default.instance.options.passwordAuthenticator).toBeUndefined();
Accounts.config({}, db);
expect(Accounts._options.passwordAuthenticator).toBeUndefined();
});
});
describe('options', function () {
it('should return options', function () {
Accounts.config({ config: 'config' }, {});
var options = Accounts.options();
expect(options.config).toEqual('config');
});
});
var db = {

@@ -74,3 +84,3 @@ findUserByUsername: function findUserByUsername() {

beforeEach(function () {
_AccountsServer2.default.config({}, db);
Accounts.config({}, db);
});

@@ -83,6 +93,6 @@ it('requires username or an email', _asyncToGenerator(regeneratorRuntime.mark(function _callee() {

case 0:
_AccountsServer2.default.config({}, db);
Accounts.config({}, db);
_context.prev = 1;
_context.next = 4;
return _AccountsServer2.default.createUser({
return Accounts.createUser({
password: '123456',

@@ -116,3 +126,3 @@ username: '',

case 0:
_AccountsServer2.default.config({}, _extends({}, db, {
Accounts.config({}, _extends({}, db, {
findUserByUsername: function findUserByUsername() {

@@ -124,3 +134,3 @@ return Promise.resolve('user');

_context2.next = 4;
return _AccountsServer2.default.createUser({
return Accounts.createUser({
password: '123456',

@@ -154,3 +164,3 @@ username: 'user1',

case 0:
_AccountsServer2.default.config({}, _extends({}, db, {
Accounts.config({}, _extends({}, db, {
findUserByEmail: function findUserByEmail() {

@@ -162,3 +172,3 @@ return Promise.resolve('user');

_context3.next = 4;
return _AccountsServer2.default.createUser({
return Accounts.createUser({
password: '123456',

@@ -192,3 +202,3 @@ username: '',

case 0:
_AccountsServer2.default.config({}, _extends({}, db, {
Accounts.config({}, _extends({}, db, {
createUser: function createUser() {

@@ -199,3 +209,3 @@ return Promise.resolve('123');

_context4.next = 3;
return _AccountsServer2.default.createUser({
return Accounts.createUser({
password: '123456',

@@ -227,3 +237,3 @@ username: 'user1'

_context5.next = 3;
return _AccountsServer2.default.loginWithPassword(null, '123456');
return Accounts.loginWithPassword(null, '123456');

@@ -255,3 +265,3 @@ case 3:

_context6.next = 3;
return _AccountsServer2.default.loginWithPassword('username', null);
return Accounts.loginWithPassword('username', null);

@@ -283,3 +293,3 @@ case 3:

_context7.next = 3;
return _AccountsServer2.default.loginWithPassword(1, '123456');
return Accounts.loginWithPassword(1, '123456');

@@ -311,3 +321,3 @@ case 3:

_context8.next = 3;
return _AccountsServer2.default.loginWithPassword('username', {});
return Accounts.loginWithPassword('username', {});

@@ -337,3 +347,3 @@ case 3:

case 0:
_AccountsServer2.default.config({}, {
Accounts.config({}, {
findUserByUsername: function findUserByUsername() {

@@ -348,3 +358,3 @@ return Promise.resolve(null);

_context9.next = 4;
return _AccountsServer2.default.loginWithPassword('username', '123456');
return Accounts.loginWithPassword('username', '123456');

@@ -374,3 +384,3 @@ case 4:

case 0:
_AccountsServer2.default.config({}, {
Accounts.config({}, {
findUserByUsername: function findUserByUsername() {

@@ -388,3 +398,3 @@ return Promise.resolve('123');

_context10.next = 4;
return _AccountsServer2.default.loginWithPassword('username', '123456');
return Accounts.loginWithPassword('username', '123456');

@@ -414,3 +424,3 @@ case 4:

case 0:
_AccountsServer2.default.config({}, {
Accounts.config({}, {
findUserByUsername: function findUserByUsername() {

@@ -431,3 +441,3 @@ return Promise.resolve('123');

_context11.next = 4;
return _AccountsServer2.default.loginWithPassword('username', '123456');
return Accounts.loginWithPassword('username', '123456');

@@ -470,6 +480,6 @@ case 4:

_AccountsServer2.default.config({ passwordAuthenticator: authenticator }, db);
Accounts.config({ passwordAuthenticator: authenticator }, db);
_context12.next = 5;
return _AccountsServer2.default.loginWithPassword('username', '123456');
return Accounts.loginWithPassword('username', '123456');

@@ -511,3 +521,3 @@ case 5:

_AccountsServer2.default.config({}, {
Accounts.config({}, {
findUserById: findUserById,

@@ -522,3 +532,3 @@ findPasswordHash: function findPasswordHash() {

_context13.next = 6;
return _AccountsServer2.default.loginWithPassword({ id: '123' }, '1234567');
return Accounts.loginWithPassword({ id: '123' }, '1234567');

@@ -547,3 +557,3 @@ case 6:

it('updates session and returns new tokens and user', _asyncToGenerator(regeneratorRuntime.mark(function _callee14() {
var updateSession, user, _Accounts$instance$cr, accessToken, refreshToken, res;
var updateSession, user, _Accounts$createToken, accessToken, refreshToken, res;

@@ -562,3 +572,3 @@ return regeneratorRuntime.wrap(function _callee14$(_context14) {

_AccountsServer2.default.config({}, {
Accounts.config({}, {
findSessionById: function findSessionById() {

@@ -576,5 +586,5 @@ return Promise.resolve({

});
_Accounts$instance$cr = _AccountsServer2.default.instance.createTokens('456'), accessToken = _Accounts$instance$cr.accessToken, refreshToken = _Accounts$instance$cr.refreshToken;
_Accounts$createToken = Accounts.createTokens('456'), accessToken = _Accounts$createToken.accessToken, refreshToken = _Accounts$createToken.refreshToken;
_AccountsServer2.default.instance.createTokens = function () {
Accounts.createTokens = function () {
return {

@@ -586,3 +596,3 @@ accessToken: 'newAccessToken',

_context14.next = 7;
return _AccountsServer2.default.refreshTokens(accessToken, refreshToken, 'ip', 'user agent');
return Accounts.refreshTokens(accessToken, refreshToken, 'ip', 'user agent');

@@ -611,6 +621,6 @@ case 7:

case 0:
_AccountsServer2.default.config({}, {});
Accounts.config({}, {});
_context15.prev = 1;
_context15.next = 4;
return _AccountsServer2.default.refreshTokens();
return Accounts.refreshTokens();

@@ -638,6 +648,6 @@ case 4:

case 0:
_AccountsServer2.default.config({}, {});
Accounts.config({}, {});
_context16.prev = 1;
_context16.next = 4;
return _AccountsServer2.default.refreshTokens('bad access token', 'bad refresh token');
return Accounts.refreshTokens('bad access token', 'bad refresh token');

@@ -661,3 +671,3 @@ case 4:

it('throws error if session not found', _asyncToGenerator(regeneratorRuntime.mark(function _callee17() {
var _Accounts$instance$cr2, accessToken, refreshToken;
var _Accounts$createToken2, accessToken, refreshToken;

@@ -668,3 +678,3 @@ return regeneratorRuntime.wrap(function _callee17$(_context17) {

case 0:
_AccountsServer2.default.config({}, {
Accounts.config({}, {
findSessionById: function findSessionById() {

@@ -675,5 +685,5 @@ return Promise.resolve(null);

_context17.prev = 1;
_Accounts$instance$cr2 = _AccountsServer2.default.instance.createTokens(), accessToken = _Accounts$instance$cr2.accessToken, refreshToken = _Accounts$instance$cr2.refreshToken;
_Accounts$createToken2 = Accounts.createTokens(), accessToken = _Accounts$createToken2.accessToken, refreshToken = _Accounts$createToken2.refreshToken;
_context17.next = 5;
return _AccountsServer2.default.refreshTokens(accessToken, refreshToken);
return Accounts.refreshTokens(accessToken, refreshToken);

@@ -697,3 +707,3 @@ case 5:

it('throws error if session not valid', _asyncToGenerator(regeneratorRuntime.mark(function _callee18() {
var _Accounts$instance$cr3, accessToken, refreshToken;
var _Accounts$createToken3, accessToken, refreshToken;

@@ -704,3 +714,3 @@ return regeneratorRuntime.wrap(function _callee18$(_context18) {

case 0:
_AccountsServer2.default.config({}, {
Accounts.config({}, {
findSessionById: function findSessionById() {

@@ -713,5 +723,5 @@ return Promise.resolve({

_context18.prev = 1;
_Accounts$instance$cr3 = _AccountsServer2.default.instance.createTokens(), accessToken = _Accounts$instance$cr3.accessToken, refreshToken = _Accounts$instance$cr3.refreshToken;
_Accounts$createToken3 = Accounts.createTokens(), accessToken = _Accounts$createToken3.accessToken, refreshToken = _Accounts$createToken3.refreshToken;
_context18.next = 5;
return _AccountsServer2.default.refreshTokens(accessToken, refreshToken);
return Accounts.refreshTokens(accessToken, refreshToken);

@@ -736,4 +746,4 @@ case 5:

describe('logout', function () {
it('invalidates session', _asyncToGenerator(regeneratorRuntime.mark(function _callee19() {
var invalidateSession, user, _Accounts$instance$cr4, accessToken;
it('throws error if user is not found', _asyncToGenerator(regeneratorRuntime.mark(function _callee19() {
var _Accounts$createToken4, accessToken, message;

@@ -744,2 +754,44 @@ return regeneratorRuntime.wrap(function _callee19$(_context19) {

case 0:
Accounts.config({}, {
findSessionById: function findSessionById() {
return Promise.resolve({
sessionId: '456',
valid: true,
userId: '123'
});
},
findUserById: function findUserById() {
return Promise.resolve(null);
}
});
_context19.prev = 1;
_Accounts$createToken4 = Accounts.createTokens('456'), accessToken = _Accounts$createToken4.accessToken;
_context19.next = 5;
return Accounts.logout(accessToken);
case 5:
throw new Error();
case 8:
_context19.prev = 8;
_context19.t0 = _context19['catch'](1);
message = _context19.t0.message;
expect(message).toEqual('User not found');
case 12:
case 'end':
return _context19.stop();
}
}
}, _callee19, undefined, [[1, 8]]);
})));
it('invalidates session', _asyncToGenerator(regeneratorRuntime.mark(function _callee20() {
var invalidateSession, user, _Accounts$createToken5, accessToken;
return regeneratorRuntime.wrap(function _callee20$(_context20) {
while (1) {
switch (_context20.prev = _context20.next) {
case 0:
invalidateSession = jest.fn(function () {

@@ -753,3 +805,3 @@ return Promise.resolve();

_AccountsServer2.default.config({}, {
Accounts.config({}, {
findSessionById: function findSessionById() {

@@ -767,5 +819,5 @@ return Promise.resolve({

});
_Accounts$instance$cr4 = _AccountsServer2.default.instance.createTokens('456'), accessToken = _Accounts$instance$cr4.accessToken;
_context19.next = 6;
return _AccountsServer2.default.logout(accessToken);
_Accounts$createToken5 = Accounts.createTokens('456'), accessToken = _Accounts$createToken5.accessToken;
_context20.next = 6;
return Accounts.logout(accessToken);

@@ -777,18 +829,18 @@ case 6:

case 'end':
return _context19.stop();
return _context20.stop();
}
}
}, _callee19, undefined);
}, _callee20, undefined);
})));
});
describe('findSessionByAccessToken', function () {
it('requires access token', _asyncToGenerator(regeneratorRuntime.mark(function _callee20() {
return regeneratorRuntime.wrap(function _callee20$(_context20) {
it('requires access token', _asyncToGenerator(regeneratorRuntime.mark(function _callee21() {
return regeneratorRuntime.wrap(function _callee21$(_context21) {
while (1) {
switch (_context20.prev = _context20.next) {
switch (_context21.prev = _context21.next) {
case 0:
_AccountsServer2.default.config({}, {});
_context20.prev = 1;
_context20.next = 4;
return _AccountsServer2.default.logout();
Accounts.config({}, {});
_context21.prev = 1;
_context21.next = 4;
return Accounts.logout();

@@ -799,23 +851,23 @@ case 4:

case 7:
_context20.prev = 7;
_context20.t0 = _context20['catch'](1);
_context21.prev = 7;
_context21.t0 = _context21['catch'](1);
expect(_context20.t0.message).toEqual('An accessToken is required');
expect(_context21.t0.message).toEqual('An accessToken is required');
case 10:
case 'end':
return _context20.stop();
return _context21.stop();
}
}
}, _callee20, undefined, [[1, 7]]);
}, _callee21, undefined, [[1, 7]]);
})));
it('throws error if tokens are not valid', _asyncToGenerator(regeneratorRuntime.mark(function _callee21() {
return regeneratorRuntime.wrap(function _callee21$(_context21) {
it('throws error if tokens are not valid', _asyncToGenerator(regeneratorRuntime.mark(function _callee22() {
return regeneratorRuntime.wrap(function _callee22$(_context22) {
while (1) {
switch (_context21.prev = _context21.next) {
switch (_context22.prev = _context22.next) {
case 0:
_AccountsServer2.default.config({}, {});
_context21.prev = 1;
_context21.next = 4;
return _AccountsServer2.default.logout('bad access token');
Accounts.config({}, {});
_context22.prev = 1;
_context22.next = 4;
return Accounts.logout('bad access token');

@@ -826,22 +878,22 @@ case 4:

case 7:
_context21.prev = 7;
_context21.t0 = _context21['catch'](1);
_context22.prev = 7;
_context22.t0 = _context22['catch'](1);
expect(_context21.t0.message).toEqual('Tokens are not valid');
expect(_context22.t0.message).toEqual('Tokens are not valid');
case 10:
case 'end':
return _context21.stop();
return _context22.stop();
}
}
}, _callee21, undefined, [[1, 7]]);
}, _callee22, undefined, [[1, 7]]);
})));
it('throws error if session not found', _asyncToGenerator(regeneratorRuntime.mark(function _callee22() {
var _Accounts$instance$cr5, accessToken;
it('throws error if session not found', _asyncToGenerator(regeneratorRuntime.mark(function _callee23() {
var _Accounts$createToken6, accessToken;
return regeneratorRuntime.wrap(function _callee22$(_context22) {
return regeneratorRuntime.wrap(function _callee23$(_context23) {
while (1) {
switch (_context22.prev = _context22.next) {
switch (_context23.prev = _context23.next) {
case 0:
_AccountsServer2.default.config({}, {
Accounts.config({}, {
findSessionById: function findSessionById() {

@@ -851,6 +903,6 @@ return Promise.resolve(null);

});
_context22.prev = 1;
_Accounts$instance$cr5 = _AccountsServer2.default.instance.createTokens(), accessToken = _Accounts$instance$cr5.accessToken;
_context22.next = 5;
return _AccountsServer2.default.logout(accessToken);
_context23.prev = 1;
_Accounts$createToken6 = Accounts.createTokens(), accessToken = _Accounts$createToken6.accessToken;
_context23.next = 5;
return Accounts.logout(accessToken);

@@ -861,22 +913,22 @@ case 5:

case 8:
_context22.prev = 8;
_context22.t0 = _context22['catch'](1);
_context23.prev = 8;
_context23.t0 = _context23['catch'](1);
expect(_context22.t0.message).toEqual('Session not found');
expect(_context23.t0.message).toEqual('Session not found');
case 11:
case 'end':
return _context22.stop();
return _context23.stop();
}
}
}, _callee22, undefined, [[1, 8]]);
}, _callee23, undefined, [[1, 8]]);
})));
it('throws error if session not valid', _asyncToGenerator(regeneratorRuntime.mark(function _callee23() {
var _Accounts$instance$cr6, accessToken;
it('throws error if session not valid', _asyncToGenerator(regeneratorRuntime.mark(function _callee24() {
var _Accounts$createToken7, accessToken;
return regeneratorRuntime.wrap(function _callee23$(_context23) {
return regeneratorRuntime.wrap(function _callee24$(_context24) {
while (1) {
switch (_context23.prev = _context23.next) {
switch (_context24.prev = _context24.next) {
case 0:
_AccountsServer2.default.config({}, {
Accounts.config({}, {
findSessionById: function findSessionById() {

@@ -888,6 +940,6 @@ return Promise.resolve({

});
_context23.prev = 1;
_Accounts$instance$cr6 = _AccountsServer2.default.instance.createTokens(), accessToken = _Accounts$instance$cr6.accessToken;
_context23.next = 5;
return _AccountsServer2.default.logout(accessToken);
_context24.prev = 1;
_Accounts$createToken7 = Accounts.createTokens(), accessToken = _Accounts$createToken7.accessToken;
_context24.next = 5;
return Accounts.logout(accessToken);

@@ -898,23 +950,210 @@ case 5:

case 8:
_context23.prev = 8;
_context23.t0 = _context23['catch'](1);
_context24.prev = 8;
_context24.t0 = _context24['catch'](1);
expect(_context23.t0.message).toEqual('Session is no longer valid');
expect(_context24.t0.message).toEqual('Session is no longer valid');
case 11:
case 'end':
return _context23.stop();
return _context24.stop();
}
}
}, _callee23, undefined, [[1, 8]]);
}, _callee24, undefined, [[1, 8]]);
})));
});
describe('findUserByEmail', function () {
it('call this.db.findUserByEmail', _asyncToGenerator(regeneratorRuntime.mark(function _callee25() {
var findUserByEmail, user;
return regeneratorRuntime.wrap(function _callee25$(_context25) {
while (1) {
switch (_context25.prev = _context25.next) {
case 0:
findUserByEmail = jest.fn(function () {
return Promise.resolve('user');
});
Accounts.config({}, { findUserByEmail: findUserByEmail });
_context25.next = 4;
return Accounts.findUserByEmail('email');
case 4:
user = _context25.sent;
expect(findUserByEmail.mock.calls[0]).toEqual(['email']);
expect(user).toEqual('user');
case 7:
case 'end':
return _context25.stop();
}
}
}, _callee25, undefined);
})));
});
describe('findUserByUsername', function () {
it('call this.db.findUserByUsername', _asyncToGenerator(regeneratorRuntime.mark(function _callee26() {
var findUserByUsername, user;
return regeneratorRuntime.wrap(function _callee26$(_context26) {
while (1) {
switch (_context26.prev = _context26.next) {
case 0:
findUserByUsername = jest.fn(function () {
return Promise.resolve('user');
});
Accounts.config({}, { findUserByUsername: findUserByUsername });
_context26.next = 4;
return Accounts.findUserByUsername('username');
case 4:
user = _context26.sent;
expect(findUserByUsername.mock.calls[0]).toEqual(['username']);
expect(user).toEqual('user');
case 7:
case 'end':
return _context26.stop();
}
}
}, _callee26, undefined);
})));
});
describe('findUserById', function () {
it('call this.db.findUserById', _asyncToGenerator(regeneratorRuntime.mark(function _callee27() {
var findUserById, user;
return regeneratorRuntime.wrap(function _callee27$(_context27) {
while (1) {
switch (_context27.prev = _context27.next) {
case 0:
findUserById = jest.fn(function () {
return Promise.resolve('user');
});
Accounts.config({}, { findUserById: findUserById });
_context27.next = 4;
return Accounts.findUserById('id');
case 4:
user = _context27.sent;
expect(findUserById.mock.calls[0]).toEqual(['id']);
expect(user).toEqual('user');
case 7:
case 'end':
return _context27.stop();
}
}
}, _callee27, undefined);
})));
});
describe('addEmail', function () {
it('call this.db.addEmail', _asyncToGenerator(regeneratorRuntime.mark(function _callee28() {
var addEmail;
return regeneratorRuntime.wrap(function _callee28$(_context28) {
while (1) {
switch (_context28.prev = _context28.next) {
case 0:
addEmail = jest.fn(function () {
return Promise.resolve();
});
Accounts.config({}, { addEmail: addEmail });
_context28.next = 4;
return Accounts.addEmail('id', 'email', true);
case 4:
expect(addEmail.mock.calls[0]).toEqual(['id', 'email', true]);
case 5:
case 'end':
return _context28.stop();
}
}
}, _callee28, undefined);
})));
});
describe('removeEmail', function () {
it('call this.db.removeEmail', _asyncToGenerator(regeneratorRuntime.mark(function _callee29() {
var removeEmail;
return regeneratorRuntime.wrap(function _callee29$(_context29) {
while (1) {
switch (_context29.prev = _context29.next) {
case 0:
removeEmail = jest.fn(function () {
return Promise.resolve();
});
Accounts.config({}, { removeEmail: removeEmail });
_context29.next = 4;
return Accounts.removeEmail('id', 'email');
case 4:
expect(removeEmail.mock.calls[0]).toEqual(['id', 'email']);
case 5:
case 'end':
return _context29.stop();
}
}
}, _callee29, undefined);
})));
});
describe('resumeSession', function () {
it('return user', _asyncToGenerator(regeneratorRuntime.mark(function _callee24() {
var user, _Accounts$instance$cr7, accessToken, foundUser;
it('throws error if user is not found', _asyncToGenerator(regeneratorRuntime.mark(function _callee30() {
var _Accounts$createToken8, accessToken, message;
return regeneratorRuntime.wrap(function _callee24$(_context24) {
return regeneratorRuntime.wrap(function _callee30$(_context30) {
while (1) {
switch (_context24.prev = _context24.next) {
switch (_context30.prev = _context30.next) {
case 0:
Accounts.config({}, {
findSessionById: function findSessionById() {
return Promise.resolve({
sessionId: '456',
valid: true,
userId: '123'
});
},
findUserById: function findUserById() {
return Promise.resolve(null);
}
});
_context30.prev = 1;
_Accounts$createToken8 = Accounts.createTokens('456'), accessToken = _Accounts$createToken8.accessToken;
_context30.next = 5;
return Accounts.resumeSession(accessToken);
case 5:
throw new Error();
case 8:
_context30.prev = 8;
_context30.t0 = _context30['catch'](1);
message = _context30.t0.message;
expect(message).toEqual('User not found');
case 12:
case 'end':
return _context30.stop();
}
}
}, _callee30, undefined, [[1, 8]]);
})));
it('return false if session is not valid', _asyncToGenerator(regeneratorRuntime.mark(function _callee31() {
var user, _Accounts$createToken9, accessToken, ret;
return regeneratorRuntime.wrap(function _callee31$(_context31) {
while (1) {
switch (_context31.prev = _context31.next) {
case 0:
user = {

@@ -925,6 +1164,47 @@ userId: '123',

_AccountsServer2.default.config({}, {
Accounts.config({}, {
findSessionById: function findSessionById() {
return Promise.resolve({
sessionId: '456',
valid: false,
userId: '123'
});
},
findUserById: function findUserById() {
return Promise.resolve(user);
}
});
_Accounts$createToken9 = Accounts.createTokens('456'), accessToken = _Accounts$createToken9.accessToken;
_context31.next = 5;
return Accounts.resumeSession(accessToken);
case 5:
ret = _context31.sent;
expect(ret).not.toBeTruthy();
case 7:
case 'end':
return _context31.stop();
}
}
}, _callee31, undefined);
})));
it('return user', _asyncToGenerator(regeneratorRuntime.mark(function _callee32() {
var user, _Accounts$createToken10, accessToken, foundUser;
return regeneratorRuntime.wrap(function _callee32$(_context32) {
while (1) {
switch (_context32.prev = _context32.next) {
case 0:
user = {
userId: '123',
username: 'username'
};
Accounts.config({}, {
findSessionById: function findSessionById() {
return Promise.resolve({
sessionId: '456',
valid: true,

@@ -938,8 +1218,8 @@ userId: '123'

});
_Accounts$instance$cr7 = _AccountsServer2.default.instance.createTokens('456'), accessToken = _Accounts$instance$cr7.accessToken;
_context24.next = 5;
return _AccountsServer2.default.resumeSession(accessToken);
_Accounts$createToken10 = Accounts.createTokens('456'), accessToken = _Accounts$createToken10.accessToken;
_context32.next = 5;
return Accounts.resumeSession(accessToken);
case 5:
foundUser = _context24.sent;
foundUser = _context32.sent;

@@ -950,14 +1230,15 @@ expect(foundUser).toEqual(user);

case 'end':
return _context24.stop();
return _context32.stop();
}
}
}, _callee24, undefined);
}, _callee32, undefined);
})));
});
it('return user with custom validation method', _asyncToGenerator(regeneratorRuntime.mark(function _callee25() {
var resumeSessionValidator, user, _Accounts$instance$cr8, accessToken;
return regeneratorRuntime.wrap(function _callee25$(_context25) {
it('return user with custom validation method', _asyncToGenerator(regeneratorRuntime.mark(function _callee33() {
var resumeSessionValidator, user, _Accounts$createToken11, accessToken;
return regeneratorRuntime.wrap(function _callee33$(_context33) {
while (1) {
switch (_context25.prev = _context25.next) {
switch (_context33.prev = _context33.next) {
case 0:

@@ -972,3 +1253,3 @@ resumeSessionValidator = jest.fn(function () {

_AccountsServer2.default.config({ resumeSessionValidator: resumeSessionValidator }, {
Accounts.config({ resumeSessionValidator: resumeSessionValidator }, {
findSessionById: function findSessionById() {

@@ -986,5 +1267,5 @@ return Promise.resolve({

_Accounts$instance$cr8 = _AccountsServer2.default.instance.createTokens('456'), accessToken = _Accounts$instance$cr8.accessToken;
_context25.next = 6;
return _AccountsServer2.default.resumeSession(accessToken);
_Accounts$createToken11 = Accounts.createTokens('456'), accessToken = _Accounts$createToken11.accessToken;
_context33.next = 6;
return Accounts.resumeSession(accessToken);

@@ -997,13 +1278,14 @@ case 6:

case 'end':
return _context25.stop();
return _context33.stop();
}
}
}, _callee25, undefined);
}, _callee33, undefined);
})));
it('throw when custom validation method rejects', _asyncToGenerator(regeneratorRuntime.mark(function _callee26() {
var resumeSessionValidator, user, _Accounts$instance$cr9, accessToken;
return regeneratorRuntime.wrap(function _callee26$(_context26) {
it('throw when custom validation method rejects', _asyncToGenerator(regeneratorRuntime.mark(function _callee34() {
var resumeSessionValidator, user, _Accounts$createToken12, accessToken;
return regeneratorRuntime.wrap(function _callee34$(_context34) {
while (1) {
switch (_context26.prev = _context26.next) {
switch (_context34.prev = _context34.next) {
case 0:

@@ -1018,3 +1300,3 @@ resumeSessionValidator = jest.fn(function () {

_AccountsServer2.default.config({ resumeSessionValidator: resumeSessionValidator }, {
Accounts.config({ resumeSessionValidator: resumeSessionValidator }, {
findSessionById: function findSessionById() {

@@ -1032,6 +1314,6 @@ return Promise.resolve({

_Accounts$instance$cr9 = _AccountsServer2.default.instance.createTokens('456'), accessToken = _Accounts$instance$cr9.accessToken;
_context26.prev = 4;
_context26.next = 7;
return _AccountsServer2.default.resumeSession(accessToken);
_Accounts$createToken12 = Accounts.createTokens('456'), accessToken = _Accounts$createToken12.accessToken;
_context34.prev = 4;
_context34.next = 7;
return Accounts.resumeSession(accessToken);

@@ -1042,21 +1324,55 @@ case 7:

case 10:
_context26.prev = 10;
_context26.t0 = _context26['catch'](4);
_context34.prev = 10;
_context34.t0 = _context34['catch'](4);
expect(resumeSessionValidator.mock.calls.length).toBe(1);
expect(_context26.t0.message).toEqual('Custom session error');
expect(_context34.t0.message).toEqual('Custom session error');
case 14:
case 'end':
return _context26.stop();
return _context34.stop();
}
}
}, _callee26, undefined, [[4, 10]]);
}, _callee34, undefined, [[4, 10]]);
})));
describe('setProfile', function () {
it('calls set profile on db interface', _asyncToGenerator(regeneratorRuntime.mark(function _callee27() {
it('throws error if user is not found', _asyncToGenerator(regeneratorRuntime.mark(function _callee35() {
var message;
return regeneratorRuntime.wrap(function _callee35$(_context35) {
while (1) {
switch (_context35.prev = _context35.next) {
case 0:
Accounts.config({}, {
findUserById: function findUserById() {
return Promise.resolve(null);
}
});
_context35.prev = 1;
_context35.next = 4;
return Accounts.setProfile();
case 4:
throw new Error();
case 7:
_context35.prev = 7;
_context35.t0 = _context35['catch'](1);
message = _context35.t0.message;
expect(message).toEqual('User not found');
case 11:
case 'end':
return _context35.stop();
}
}
}, _callee35, undefined, [[1, 7]]);
})));
it('calls set profile on db interface', _asyncToGenerator(regeneratorRuntime.mark(function _callee36() {
var user, profile, setProfile;
return regeneratorRuntime.wrap(function _callee27$(_context27) {
return regeneratorRuntime.wrap(function _callee36$(_context36) {
while (1) {
switch (_context27.prev = _context27.next) {
switch (_context36.prev = _context36.next) {
case 0:

@@ -1072,3 +1388,3 @@ user = {

_AccountsServer2.default.config({}, {
Accounts.config({}, {
findUserById: function findUserById() {

@@ -1079,4 +1395,4 @@ return Promise.resolve(user);

});
_context27.next = 6;
return _AccountsServer2.default.setProfile('123', profile);
_context36.next = 6;
return Accounts.setProfile('123', profile);

@@ -1090,12 +1406,13 @@ case 6:

case 'end':
return _context27.stop();
return _context36.stop();
}
}
}, _callee27, undefined);
}, _callee36, undefined);
})));
it('merges profile and calls set profile on db interface', _asyncToGenerator(regeneratorRuntime.mark(function _callee28() {
it('merges profile and calls set profile on db interface', _asyncToGenerator(regeneratorRuntime.mark(function _callee37() {
var user, profile, mergedProfile, setProfile, res;
return regeneratorRuntime.wrap(function _callee28$(_context28) {
return regeneratorRuntime.wrap(function _callee37$(_context37) {
while (1) {
switch (_context28.prev = _context28.next) {
switch (_context37.prev = _context37.next) {
case 0:

@@ -1120,3 +1437,3 @@ user = {

_AccountsServer2.default.config({}, {
Accounts.config({}, {
findUserById: function findUserById() {

@@ -1127,7 +1444,7 @@ return Promise.resolve(user);

});
_context28.next = 7;
return _AccountsServer2.default.updateProfile('123', profile);
_context37.next = 7;
return Accounts.updateProfile('123', profile);
case 7:
res = _context28.sent;
res = _context37.sent;

@@ -1141,9 +1458,295 @@ expect(setProfile.mock.calls.length).toEqual(1);

case 'end':
return _context28.stop();
return _context37.stop();
}
}
}, _callee28, undefined);
}, _callee37, undefined);
})));
});
describe('sendVerificationEmail', function () {
it('throws error if user is not found', _asyncToGenerator(regeneratorRuntime.mark(function _callee38() {
return regeneratorRuntime.wrap(function _callee38$(_context38) {
while (1) {
switch (_context38.prev = _context38.next) {
case 0:
Accounts.config({}, {
findUserById: function findUserById() {
return Promise.resolve(null);
}
});
_context38.prev = 1;
_context38.next = 4;
return Accounts.sendVerificationEmail();
case 4:
throw new Error();
case 7:
_context38.prev = 7;
_context38.t0 = _context38['catch'](1);
expect(_context38.t0.message).toEqual('User not found');
case 10:
case 'end':
return _context38.stop();
}
}
}, _callee38, undefined, [[1, 7]]);
})));
it('throws when bad email address passed', _asyncToGenerator(regeneratorRuntime.mark(function _callee39() {
var user;
return regeneratorRuntime.wrap(function _callee39$(_context39) {
while (1) {
switch (_context39.prev = _context39.next) {
case 0:
user = {
emails: [{ address: 'email' }]
};
Accounts.config({}, {
findUserById: function findUserById() {
return Promise.resolve(user);
}
});
_context39.prev = 2;
_context39.next = 5;
return Accounts.sendVerificationEmail('userId', 'toto');
case 5:
throw new Error();
case 8:
_context39.prev = 8;
_context39.t0 = _context39['catch'](2);
expect(_context39.t0.message).toEqual('No such email address for user');
case 11:
case 'end':
return _context39.stop();
}
}
}, _callee39, undefined, [[2, 8]]);
})));
it('should send to first unverified email', _asyncToGenerator(regeneratorRuntime.mark(function _callee40() {
var user;
return regeneratorRuntime.wrap(function _callee40$(_context40) {
while (1) {
switch (_context40.prev = _context40.next) {
case 0:
user = {
emails: [{ address: 'email' }]
};
Accounts.config({}, {
findUserById: function findUserById() {
return Promise.resolve(user);
},
addEmailVerificationToken: function addEmailVerificationToken() {
return Promise.resolve('token');
}
});
Accounts.email = { sendMail: jest.fn() };
_context40.next = 5;
return Accounts.sendVerificationEmail('userId');
case 5:
expect(Accounts.email.sendMail.mock.calls.length).toEqual(1);
expect(Accounts.email.sendMail.mock.calls[0][0].from).toBeTruthy();
expect(Accounts.email.sendMail.mock.calls[0][0].to).toEqual('email');
expect(Accounts.email.sendMail.mock.calls[0][0].subject).toBeTruthy();
expect(Accounts.email.sendMail.mock.calls[0][0].text).toBeTruthy();
case 10:
case 'end':
return _context40.stop();
}
}
}, _callee40, undefined);
})));
it('should send email', _asyncToGenerator(regeneratorRuntime.mark(function _callee41() {
var user;
return regeneratorRuntime.wrap(function _callee41$(_context41) {
while (1) {
switch (_context41.prev = _context41.next) {
case 0:
user = {
emails: [{ address: 'email' }]
};
Accounts.config({}, {
findUserById: function findUserById() {
return Promise.resolve(user);
},
addEmailVerificationToken: function addEmailVerificationToken() {
return Promise.resolve('token');
}
});
Accounts.email = { sendMail: jest.fn() };
_context41.next = 5;
return Accounts.sendVerificationEmail('userId', 'email');
case 5:
expect(Accounts.email.sendMail.mock.calls.length).toEqual(1);
expect(Accounts.email.sendMail.mock.calls[0][0].from).toBeTruthy();
expect(Accounts.email.sendMail.mock.calls[0][0].to).toEqual('email');
expect(Accounts.email.sendMail.mock.calls[0][0].subject).toBeTruthy();
expect(Accounts.email.sendMail.mock.calls[0][0].text).toBeTruthy();
case 10:
case 'end':
return _context41.stop();
}
}
}, _callee41, undefined);
})));
});
describe('sendResetPasswordEmail', function () {
it('throws error if user is not found', _asyncToGenerator(regeneratorRuntime.mark(function _callee42() {
return regeneratorRuntime.wrap(function _callee42$(_context42) {
while (1) {
switch (_context42.prev = _context42.next) {
case 0:
Accounts.config({}, {
findUserById: function findUserById() {
return Promise.resolve(null);
}
});
_context42.prev = 1;
_context42.next = 4;
return Accounts.sendResetPasswordEmail();
case 4:
throw new Error();
case 7:
_context42.prev = 7;
_context42.t0 = _context42['catch'](1);
expect(_context42.t0.message).toEqual('User not found');
case 10:
case 'end':
return _context42.stop();
}
}
}, _callee42, undefined, [[1, 7]]);
})));
it('throws when bad email address passed', _asyncToGenerator(regeneratorRuntime.mark(function _callee43() {
var user;
return regeneratorRuntime.wrap(function _callee43$(_context43) {
while (1) {
switch (_context43.prev = _context43.next) {
case 0:
user = {
emails: [{ address: 'email' }]
};
Accounts.config({}, {
findUserById: function findUserById() {
return Promise.resolve(user);
}
});
_context43.prev = 2;
_context43.next = 5;
return Accounts.sendResetPasswordEmail('userId', 'toto');
case 5:
throw new Error();
case 8:
_context43.prev = 8;
_context43.t0 = _context43['catch'](2);
expect(_context43.t0.message).toEqual('No such email address for user');
case 11:
case 'end':
return _context43.stop();
}
}
}, _callee43, undefined, [[2, 8]]);
})));
it('should send to first user email', _asyncToGenerator(regeneratorRuntime.mark(function _callee44() {
var user;
return regeneratorRuntime.wrap(function _callee44$(_context44) {
while (1) {
switch (_context44.prev = _context44.next) {
case 0:
user = {
emails: [{ address: 'email' }]
};
Accounts.config({}, {
findUserById: function findUserById() {
return Promise.resolve(user);
},
addResetPasswordToken: function addResetPasswordToken() {
return Promise.resolve('token');
}
});
Accounts.email = { sendMail: jest.fn() };
_context44.next = 5;
return Accounts.sendResetPasswordEmail('userId');
case 5:
expect(Accounts.email.sendMail.mock.calls.length).toEqual(1);
expect(Accounts.email.sendMail.mock.calls[0][0].from).toBeTruthy();
expect(Accounts.email.sendMail.mock.calls[0][0].to).toEqual('email');
expect(Accounts.email.sendMail.mock.calls[0][0].subject).toBeTruthy();
expect(Accounts.email.sendMail.mock.calls[0][0].text).toBeTruthy();
case 10:
case 'end':
return _context44.stop();
}
}
}, _callee44, undefined);
})));
it('should send email', _asyncToGenerator(regeneratorRuntime.mark(function _callee45() {
var user;
return regeneratorRuntime.wrap(function _callee45$(_context45) {
while (1) {
switch (_context45.prev = _context45.next) {
case 0:
user = {
emails: [{ address: 'email' }]
};
Accounts.config({}, {
findUserById: function findUserById() {
return Promise.resolve(user);
},
addResetPasswordToken: function addResetPasswordToken() {
return Promise.resolve('token');
}
});
Accounts.email = { sendMail: jest.fn() };
_context45.next = 5;
return Accounts.sendResetPasswordEmail('userId', 'email');
case 5:
expect(Accounts.email.sendMail.mock.calls.length).toEqual(1);
expect(Accounts.email.sendMail.mock.calls[0][0].from).toBeTruthy();
expect(Accounts.email.sendMail.mock.calls[0][0].to).toEqual('email');
expect(Accounts.email.sendMail.mock.calls[0][0].subject).toBeTruthy();
expect(Accounts.email.sendMail.mock.calls[0][0].text).toBeTruthy();
case 10:
case 'end':
return _context45.stop();
}
}
}, _callee45, undefined);
})));
});
});
});

4

lib/tokens.js

@@ -6,3 +6,3 @@ 'use strict';

});
exports.generateRefreshToken = exports.generateAccessToken = exports.generateEmailToken = undefined;
exports.generateRefreshToken = exports.generateAccessToken = exports.generateRandomToken = undefined;

@@ -19,3 +19,3 @@ var _jsonwebtoken = require('jsonwebtoken');

var generateEmailToken = exports.generateEmailToken = function generateEmailToken() {
var generateRandomToken = exports.generateRandomToken = function generateRandomToken() {
var length = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 43;

@@ -22,0 +22,0 @@ return _crypto2.default.randomBytes(length).toString('hex');

{
"name": "@accounts/server",
"version": "0.0.4",
"version": "0.0.5-alpha.451f3013",
"description": "Fullstack authentication and accounts-management",

@@ -12,2 +12,3 @@ "main": "lib/index.js",

"compile": "babel ./src --out-dir ./lib",
"postcompile": "npm run flow:prepublish",
"prepublish": "npm run compile",

@@ -17,2 +18,4 @@ "test": "npm run testonly",

"testonly": "jest",
"flow:check": "flow check",
"flow:prepublish": "for i in `ls ./src/*.js`; do cp $i `echo $i | sed \"s/src/lib/g\" | sed \"s/js/js\\.flow/g\"`; done",
"coverage": "npm run testonly -- --coverage",

@@ -45,5 +48,6 @@ "coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"

"dependencies": {
"@accounts/common": "^0.0.4",
"@accounts/common": "^0.0.5-alpha.451f3013",
"bcryptjs": "^2.4.0",
"crypto": "^0.0.3",
"emailjs": "^1.0.8",
"jsonwebtoken": "^7.2.1",

@@ -65,3 +69,3 @@ "jwt-decode": "^2.1.0",

"coveralls": "^2.11.14",
"flow-bin": "^0.37.0",
"flow-bin": "^0.39.0",
"jest": "^18.0.0",

@@ -68,0 +72,0 @@ "localstorage-polyfill": "^1.0.1",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc