@accounts/server
Advanced tools
Comparing version 0.0.3 to 0.0.4-alpha.98931215
@@ -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,21 +44,34 @@ | ||
var AccountsServer = exports.AccountsServer = function () { | ||
function AccountsServer(options, db) { | ||
function AccountsServer() { | ||
_classCallCheck(this, AccountsServer); | ||
} | ||
this.options = options; | ||
if (!db) { | ||
throw new _common.AccountsError({ | ||
message: 'A database driver is required' | ||
}); | ||
_createClass(AccountsServer, [{ | ||
key: 'config', | ||
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 | ||
}, { | ||
key: 'options', | ||
value: function options() { | ||
return this._options; | ||
} | ||
// eslint-disable-next-line max-len | ||
_createClass(AccountsServer, [{ | ||
}, { | ||
key: 'loginWithPassword', | ||
value: function () { | ||
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(user, password, ip, userAgent) { | ||
var _ref2, username, email, id, foundUser, hash, isValidPassword, sessionId, _createTokens, accessToken, refreshToken; | ||
var foundUser, sessionId, _createTokens, accessToken, refreshToken; | ||
@@ -66,3 +87,3 @@ return regeneratorRuntime.wrap(function _callee$(_context) { | ||
throw new _common.AccountsError({ message: 'Unrecognized options for login request [400]' }); | ||
throw new _common.AccountsError('Unrecognized options for login request', user, 400); | ||
@@ -75,114 +96,206 @@ case 2: | ||
throw new _common.AccountsError({ message: 'Match failed [400]' }); | ||
throw new _common.AccountsError('Match failed', user, 400); | ||
case 4: | ||
_ref2 = (0, _lodash.isString)(user) ? (0, _common.toUsernameAndEmail)({ user: user }) | ||
// $FlowFixMe | ||
: (0, _common.toUsernameAndEmail)(_extends({}, user)), username = _ref2.username, email = _ref2.email, id = _ref2.id; | ||
foundUser = void 0; | ||
if (!id) { | ||
_context.next = 12; | ||
if (!this._options.passwordAuthenticator) { | ||
_context.next = 17; | ||
break; | ||
} | ||
_context.prev = 6; | ||
_context.next = 9; | ||
return this.db.findUserById(id); | ||
return this._externalPasswordAuthenticator(this._options.passwordAuthenticator, user, password); | ||
case 9: | ||
foundUser = _context.sent; | ||
_context.next = 22; | ||
_context.next = 15; | ||
break; | ||
case 12: | ||
_context.prev = 12; | ||
_context.t0 = _context['catch'](6); | ||
throw new _common.AccountsError(_context.t0, user, 403); | ||
case 15: | ||
_context.next = 20; | ||
break; | ||
case 17: | ||
_context.next = 19; | ||
return this._defaultPasswordAuthenticator(user, password); | ||
case 19: | ||
foundUser = _context.sent; | ||
case 20: | ||
if (foundUser) { | ||
_context.next = 22; | ||
break; | ||
} | ||
throw new _common.AccountsError('User not found', user, 403); | ||
case 22: | ||
_context.next = 24; | ||
return this.db.createSession(foundUser.id, ip, userAgent); | ||
case 24: | ||
sessionId = _context.sent; | ||
_createTokens = this.createTokens(sessionId), accessToken = _createTokens.accessToken, refreshToken = _createTokens.refreshToken; | ||
return _context.abrupt('return', { | ||
sessionId: sessionId, | ||
user: foundUser, | ||
tokens: { | ||
refreshToken: refreshToken, | ||
accessToken: accessToken | ||
} | ||
}); | ||
case 27: | ||
case 'end': | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, this, [[6, 12]]); | ||
})); | ||
function loginWithPassword(_x, _x2, _x3, _x4) { | ||
return _ref.apply(this, arguments); | ||
} | ||
return loginWithPassword; | ||
}() | ||
// eslint-disable-next-line max-len | ||
}, { | ||
key: '_externalPasswordAuthenticator', | ||
value: function () { | ||
var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(authFn, user, password) { | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
return _context2.abrupt('return', authFn(user, password)); | ||
case 1: | ||
case 'end': | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, this); | ||
})); | ||
function _externalPasswordAuthenticator(_x5, _x6, _x7) { | ||
return _ref2.apply(this, arguments); | ||
} | ||
return _externalPasswordAuthenticator; | ||
}() | ||
}, { | ||
key: '_defaultPasswordAuthenticator', | ||
value: function () { | ||
var _ref3 = _asyncToGenerator(regeneratorRuntime.mark(function _callee3(user, password) { | ||
var _ref4, username, email, id, foundUser, hash, isPasswordValid; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
_ref4 = (0, _lodash.isString)(user) ? (0, _common.toUsernameAndEmail)({ user: user }) : (0, _common.toUsernameAndEmail)(_extends({}, user)), username = _ref4.username, email = _ref4.email, id = _ref4.id; | ||
foundUser = void 0; | ||
if (!id) { | ||
_context3.next = 8; | ||
break; | ||
} | ||
_context3.next = 5; | ||
return this.db.findUserById(id); | ||
case 5: | ||
foundUser = _context3.sent; | ||
_context3.next = 18; | ||
break; | ||
case 8: | ||
if (!username) { | ||
_context.next = 18; | ||
_context3.next = 14; | ||
break; | ||
} | ||
_context.next = 15; | ||
_context3.next = 11; | ||
return this.db.findUserByUsername(username); | ||
case 15: | ||
foundUser = _context.sent; | ||
_context.next = 22; | ||
case 11: | ||
foundUser = _context3.sent; | ||
_context3.next = 18; | ||
break; | ||
case 18: | ||
case 14: | ||
if (!email) { | ||
_context.next = 22; | ||
_context3.next = 18; | ||
break; | ||
} | ||
_context.next = 21; | ||
_context3.next = 17; | ||
return this.db.findUserByEmail(email); | ||
case 21: | ||
foundUser = _context.sent; | ||
case 17: | ||
foundUser = _context3.sent; | ||
case 22: | ||
case 18: | ||
if (foundUser) { | ||
_context.next = 24; | ||
_context3.next = 20; | ||
break; | ||
} | ||
throw new _common.AccountsError({ message: 'User not found [403]' }); | ||
throw new _common.AccountsError('User not found', user, 403); | ||
case 24: | ||
_context.next = 26; | ||
case 20: | ||
_context3.next = 22; | ||
return this.db.findPasswordHash(foundUser.id); | ||
case 26: | ||
hash = _context.sent; | ||
case 22: | ||
hash = _context3.sent; | ||
if (hash) { | ||
_context.next = 29; | ||
_context3.next = 25; | ||
break; | ||
} | ||
throw new _common.AccountsError({ message: 'User has no password set [403]' }); | ||
throw new _common.AccountsError('User has no password set', user, 403); | ||
case 29: | ||
_context.next = 31; | ||
case 25: | ||
_context3.next = 27; | ||
return (0, _encryption.verifyPassword)(password, hash); | ||
case 31: | ||
isValidPassword = _context.sent; | ||
case 27: | ||
isPasswordValid = _context3.sent; | ||
if (isValidPassword) { | ||
_context.next = 34; | ||
if (isPasswordValid) { | ||
_context3.next = 30; | ||
break; | ||
} | ||
throw new _common.AccountsError({ message: 'Incorrect password [403]' }); | ||
throw new _common.AccountsError('Incorrect password', user, 403); | ||
case 34: | ||
_context.next = 36; | ||
return this.db.createSession(foundUser.id, ip, userAgent); | ||
case 30: | ||
return _context3.abrupt('return', foundUser); | ||
case 36: | ||
sessionId = _context.sent; | ||
_createTokens = this.createTokens(sessionId), accessToken = _createTokens.accessToken, refreshToken = _createTokens.refreshToken; | ||
return _context.abrupt('return', { | ||
sessionId: sessionId, | ||
user: foundUser, | ||
tokens: { | ||
refreshToken: refreshToken, | ||
accessToken: accessToken | ||
} | ||
}); | ||
case 39: | ||
case 31: | ||
case 'end': | ||
return _context.stop(); | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee, this); | ||
}, _callee3, this); | ||
})); | ||
function loginWithPassword(_x, _x2, _x3, _x4) { | ||
return _ref.apply(this, arguments); | ||
function _defaultPasswordAuthenticator(_x8, _x9) { | ||
return _ref3.apply(this, arguments); | ||
} | ||
return loginWithPassword; | ||
return _defaultPasswordAuthenticator; | ||
}() | ||
@@ -192,65 +305,64 @@ }, { | ||
value: function () { | ||
var _ref3 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(user) { | ||
var _ref5 = _asyncToGenerator(regeneratorRuntime.mark(function _callee4(user) { | ||
var userId; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
return regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
if (!(!_common.validators.validateUsername(user.username) && !_common.validators.validateEmail(user.email))) { | ||
_context2.next = 2; | ||
_context4.next = 2; | ||
break; | ||
} | ||
throw new _common.AccountsError({ message: 'Username or Email is required' }); | ||
throw new _common.AccountsError('Username or Email is required', { | ||
username: user && user.username, | ||
email: user && user.email | ||
}); | ||
case 2: | ||
_context2.t0 = user.username; | ||
_context4.t0 = user.username; | ||
if (!_context2.t0) { | ||
_context2.next = 7; | ||
if (!_context4.t0) { | ||
_context4.next = 7; | ||
break; | ||
} | ||
_context2.next = 6; | ||
_context4.next = 6; | ||
return this.db.findUserByUsername(user.username); | ||
case 6: | ||
_context2.t0 = _context2.sent; | ||
_context4.t0 = _context4.sent; | ||
case 7: | ||
if (!_context2.t0) { | ||
_context2.next = 9; | ||
if (!_context4.t0) { | ||
_context4.next = 9; | ||
break; | ||
} | ||
throw new _common.AccountsError({ | ||
message: 'Username already exists' | ||
}); | ||
throw new _common.AccountsError('Username already exists', { username: user.username }); | ||
case 9: | ||
_context2.t1 = user.email; | ||
_context4.t1 = user.email; | ||
if (!_context2.t1) { | ||
_context2.next = 14; | ||
if (!_context4.t1) { | ||
_context4.next = 14; | ||
break; | ||
} | ||
_context2.next = 13; | ||
_context4.next = 13; | ||
return this.db.findUserByEmail(user.email); | ||
case 13: | ||
_context2.t1 = _context2.sent; | ||
_context4.t1 = _context4.sent; | ||
case 14: | ||
if (!_context2.t1) { | ||
_context2.next = 16; | ||
if (!_context4.t1) { | ||
_context4.next = 16; | ||
break; | ||
} | ||
throw new _common.AccountsError({ | ||
message: 'Email already exists' | ||
}); | ||
throw new _common.AccountsError('Email already exists', { email: user.email }); | ||
case 16: | ||
_context2.next = 18; | ||
_context4.next = 18; | ||
return this.db.createUser({ | ||
@@ -264,15 +376,15 @@ username: user.username, | ||
case 18: | ||
userId = _context2.sent; | ||
return _context2.abrupt('return', userId); | ||
userId = _context4.sent; | ||
return _context4.abrupt('return', userId); | ||
case 20: | ||
case 'end': | ||
return _context2.stop(); | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee2, this); | ||
}, _callee4, this); | ||
})); | ||
function createUser(_x5) { | ||
return _ref3.apply(this, arguments); | ||
function createUser(_x10) { | ||
return _ref5.apply(this, arguments); | ||
} | ||
@@ -282,2 +394,3 @@ | ||
}() | ||
// eslint-disable-next-line max-len | ||
@@ -288,23 +401,21 @@ | ||
value: function () { | ||
var _ref4 = _asyncToGenerator(regeneratorRuntime.mark(function _callee3(accessToken, refreshToken, ip, userAgent) { | ||
var _ref6 = _asyncToGenerator(regeneratorRuntime.mark(function _callee5(accessToken, refreshToken, ip, userAgent) { | ||
var sessionId, decodedAccessToken, session, user, tokens; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
return regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
if (!(!(0, _lodash.isString)(accessToken) || !(0, _lodash.isString)(refreshToken))) { | ||
_context3.next = 2; | ||
_context5.next = 2; | ||
break; | ||
} | ||
throw new _common.AccountsError({ | ||
message: 'An accessToken and refreshToken are required' | ||
}); | ||
throw new _common.AccountsError('An accessToken and refreshToken are required'); | ||
case 2: | ||
sessionId = void 0; | ||
_context3.prev = 3; | ||
_context5.prev = 3; | ||
_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 | ||
@@ -314,56 +425,50 @@ }); | ||
sessionId = decodedAccessToken.data.sessionId; | ||
_context3.next = 12; | ||
_context5.next = 12; | ||
break; | ||
case 9: | ||
_context3.prev = 9; | ||
_context3.t0 = _context3['catch'](3); | ||
throw new _common.AccountsError({ | ||
message: 'Tokens are not valid' | ||
}); | ||
_context5.prev = 9; | ||
_context5.t0 = _context5['catch'](3); | ||
throw new _common.AccountsError('Tokens are not valid'); | ||
case 12: | ||
_context3.next = 14; | ||
_context5.next = 14; | ||
return this.db.findSessionById(sessionId); | ||
case 14: | ||
session = _context3.sent; | ||
session = _context5.sent; | ||
if (session) { | ||
_context3.next = 17; | ||
_context5.next = 17; | ||
break; | ||
} | ||
throw new _common.AccountsError({ | ||
message: 'Session not found' | ||
}); | ||
throw new _common.AccountsError('Session not found'); | ||
case 17: | ||
if (!session.valid) { | ||
_context3.next = 29; | ||
_context5.next = 29; | ||
break; | ||
} | ||
_context3.next = 20; | ||
_context5.next = 20; | ||
return this.db.findUserById(session.userId); | ||
case 20: | ||
user = _context3.sent; | ||
user = _context5.sent; | ||
if (user) { | ||
_context3.next = 23; | ||
_context5.next = 23; | ||
break; | ||
} | ||
throw new _common.AccountsError({ | ||
message: 'User not found' | ||
}); | ||
throw new _common.AccountsError('User not found', { id: session.userId }); | ||
case 23: | ||
tokens = this.createTokens(sessionId); | ||
_context3.next = 26; | ||
_context5.next = 26; | ||
return this.db.updateSession(sessionId, ip, userAgent); | ||
case 26: | ||
return _context3.abrupt('return', { | ||
return _context5.abrupt('return', { | ||
sessionId: sessionId, | ||
@@ -375,16 +480,14 @@ user: user, | ||
case 29: | ||
throw new _common.AccountsError({ | ||
message: 'Session is no longer valid' | ||
}); | ||
throw new _common.AccountsError('Session is no longer valid', { id: session.userId }); | ||
case 30: | ||
case 'end': | ||
return _context3.stop(); | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee3, this, [[3, 9]]); | ||
}, _callee5, this, [[3, 9]]); | ||
})); | ||
function refreshTokens(_x6, _x7, _x8, _x9) { | ||
return _ref4.apply(this, arguments); | ||
function refreshTokens(_x11, _x12, _x13, _x14) { | ||
return _ref6.apply(this, arguments); | ||
} | ||
@@ -397,3 +500,3 @@ | ||
value: function createTokens(sessionId) { | ||
var _options = this.options, | ||
var _options = this._options, | ||
tokenSecret = _options.tokenSecret, | ||
@@ -418,57 +521,53 @@ tokenConfigs = _options.tokenConfigs; | ||
value: function () { | ||
var _ref5 = _asyncToGenerator(regeneratorRuntime.mark(function _callee4(accessToken) { | ||
var _ref7 = _asyncToGenerator(regeneratorRuntime.mark(function _callee6(accessToken) { | ||
var session, user; | ||
return regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
return regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
_context4.next = 2; | ||
_context6.next = 2; | ||
return this.findSessionByAccessToken(accessToken); | ||
case 2: | ||
session = _context4.sent; | ||
session = _context6.sent; | ||
if (!session.valid) { | ||
_context4.next = 13; | ||
_context6.next = 13; | ||
break; | ||
} | ||
_context4.next = 6; | ||
_context6.next = 6; | ||
return this.db.findUserById(session.userId); | ||
case 6: | ||
user = _context4.sent; | ||
user = _context6.sent; | ||
if (user) { | ||
_context4.next = 9; | ||
_context6.next = 9; | ||
break; | ||
} | ||
throw new _common.AccountsError({ | ||
message: 'User not found' | ||
}); | ||
throw new _common.AccountsError('User not found', { id: session.userId }); | ||
case 9: | ||
_context4.next = 11; | ||
_context6.next = 11; | ||
return this.db.invalidateSession(session.sessionId); | ||
case 11: | ||
_context4.next = 14; | ||
_context6.next = 14; | ||
break; | ||
case 13: | ||
throw new _common.AccountsError({ | ||
message: 'Session is no longer valid' | ||
}); | ||
throw new _common.AccountsError('Session is no longer valid', { id: session.userId }); | ||
case 14: | ||
case 'end': | ||
return _context4.stop(); | ||
return _context6.stop(); | ||
} | ||
} | ||
}, _callee4, this); | ||
}, _callee6, this); | ||
})); | ||
function logout(_x10) { | ||
return _ref5.apply(this, arguments); | ||
function logout(_x15) { | ||
return _ref7.apply(this, arguments); | ||
} | ||
@@ -481,50 +580,67 @@ | ||
value: function () { | ||
var _ref6 = _asyncToGenerator(regeneratorRuntime.mark(function _callee5(accessToken) { | ||
var _ref8 = _asyncToGenerator(regeneratorRuntime.mark(function _callee7(accessToken) { | ||
var session, user; | ||
return regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
return regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
switch (_context7.prev = _context7.next) { | ||
case 0: | ||
_context5.next = 2; | ||
_context7.next = 2; | ||
return this.findSessionByAccessToken(accessToken); | ||
case 2: | ||
session = _context5.sent; | ||
session = _context7.sent; | ||
if (!session.valid) { | ||
_context5.next = 10; | ||
_context7.next = 19; | ||
break; | ||
} | ||
_context5.next = 6; | ||
_context7.next = 6; | ||
return this.db.findUserById(session.userId); | ||
case 6: | ||
user = _context5.sent; | ||
user = _context7.sent; | ||
if (user) { | ||
_context5.next = 9; | ||
_context7.next = 9; | ||
break; | ||
} | ||
throw new _common.AccountsError({ | ||
message: 'User not found' | ||
}); | ||
throw new _common.AccountsError('User not found', { id: session.userId }); | ||
case 9: | ||
return _context5.abrupt('return', user); | ||
if (!this._options.resumeSessionValidator) { | ||
_context7.next = 18; | ||
break; | ||
} | ||
case 10: | ||
return _context5.abrupt('return', null); | ||
_context7.prev = 10; | ||
_context7.next = 13; | ||
return this._options.resumeSessionValidator(user, session); | ||
case 11: | ||
case 13: | ||
_context7.next = 18; | ||
break; | ||
case 15: | ||
_context7.prev = 15; | ||
_context7.t0 = _context7['catch'](10); | ||
throw new _common.AccountsError(_context7.t0, { id: session.userId }, 403); | ||
case 18: | ||
return _context7.abrupt('return', user); | ||
case 19: | ||
return _context7.abrupt('return', null); | ||
case 20: | ||
case 'end': | ||
return _context5.stop(); | ||
return _context7.stop(); | ||
} | ||
} | ||
}, _callee5, this); | ||
}, _callee7, this, [[10, 15]]); | ||
})); | ||
function resumeSession(_x11) { | ||
return _ref6.apply(this, arguments); | ||
function resumeSession(_x16) { | ||
return _ref8.apply(this, arguments); | ||
} | ||
@@ -537,62 +653,56 @@ | ||
value: function () { | ||
var _ref7 = _asyncToGenerator(regeneratorRuntime.mark(function _callee6(accessToken) { | ||
var _ref9 = _asyncToGenerator(regeneratorRuntime.mark(function _callee8(accessToken) { | ||
var sessionId, decodedAccessToken, session; | ||
return regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
return regeneratorRuntime.wrap(function _callee8$(_context8) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
switch (_context8.prev = _context8.next) { | ||
case 0: | ||
if ((0, _lodash.isString)(accessToken)) { | ||
_context6.next = 2; | ||
_context8.next = 2; | ||
break; | ||
} | ||
throw new _common.AccountsError({ | ||
message: 'An accessToken is required' | ||
}); | ||
throw new _common.AccountsError('An accessToken is required'); | ||
case 2: | ||
sessionId = void 0; | ||
_context6.prev = 3; | ||
decodedAccessToken = _jsonwebtoken2.default.verify(accessToken, this.options.tokenSecret); | ||
_context8.prev = 3; | ||
decodedAccessToken = _jsonwebtoken2.default.verify(accessToken, this._options.tokenSecret); | ||
sessionId = decodedAccessToken.data.sessionId; | ||
_context6.next = 11; | ||
_context8.next = 11; | ||
break; | ||
case 8: | ||
_context6.prev = 8; | ||
_context6.t0 = _context6['catch'](3); | ||
throw new _common.AccountsError({ | ||
message: 'Tokens are not valid' | ||
}); | ||
_context8.prev = 8; | ||
_context8.t0 = _context8['catch'](3); | ||
throw new _common.AccountsError('Tokens are not valid'); | ||
case 11: | ||
_context6.next = 13; | ||
_context8.next = 13; | ||
return this.db.findSessionById(sessionId); | ||
case 13: | ||
session = _context6.sent; | ||
session = _context8.sent; | ||
if (session) { | ||
_context6.next = 16; | ||
_context8.next = 16; | ||
break; | ||
} | ||
throw new _common.AccountsError({ | ||
message: 'Session not found' | ||
}); | ||
throw new _common.AccountsError('Session not found'); | ||
case 16: | ||
return _context6.abrupt('return', session); | ||
return _context8.abrupt('return', session); | ||
case 17: | ||
case 'end': | ||
return _context6.stop(); | ||
return _context8.stop(); | ||
} | ||
} | ||
}, _callee6, this, [[3, 8]]); | ||
}, _callee8, this, [[3, 8]]); | ||
})); | ||
function findSessionByAccessToken(_x12) { | ||
return _ref7.apply(this, arguments); | ||
function findSessionByAccessToken(_x17) { | ||
return _ref9.apply(this, arguments); | ||
} | ||
@@ -629,6 +739,137 @@ | ||
key: 'verifyEmail', | ||
value: function verifyEmail(userId, email) { | ||
return this.db.verifyEmail(userId, email); | ||
} | ||
value: function () { | ||
var _ref10 = _asyncToGenerator(regeneratorRuntime.mark(function _callee9(token) { | ||
var user, verificationTokens, tokenRecord, emailRecord; | ||
return regeneratorRuntime.wrap(function _callee9$(_context9) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
case 0: | ||
_context9.next = 2; | ||
return this.db.findUserByEmailVerificationToken(token); | ||
case 2: | ||
user = _context9.sent; | ||
if (user) { | ||
_context9.next = 5; | ||
break; | ||
} | ||
throw new _common.AccountsError('Verify email link expired'); | ||
case 5: | ||
verificationTokens = (0, _lodash.get)(user, ['services', 'email', 'verificationTokens'], []); | ||
tokenRecord = (0, _lodash.find)(verificationTokens, function (t) { | ||
return t.token === token; | ||
}); | ||
if (tokenRecord) { | ||
_context9.next = 9; | ||
break; | ||
} | ||
throw new _common.AccountsError('Verify email link expired'); | ||
case 9: | ||
// TODO check time for expiry date | ||
emailRecord = (0, _lodash.find)(user.emails, function (e) { | ||
return e.address === tokenRecord.address; | ||
}); | ||
if (emailRecord) { | ||
_context9.next = 12; | ||
break; | ||
} | ||
throw new _common.AccountsError('Verify email link is for unknown address'); | ||
case 12: | ||
_context9.next = 14; | ||
return this.db.verifyEmail(user.id, emailRecord.address); | ||
case 14: | ||
case 'end': | ||
return _context9.stop(); | ||
} | ||
} | ||
}, _callee9, this); | ||
})); | ||
function verifyEmail(_x18) { | ||
return _ref10.apply(this, arguments); | ||
} | ||
return verifyEmail; | ||
}() | ||
}, { | ||
key: 'resetPassword', | ||
value: function () { | ||
var _ref11 = _asyncToGenerator(regeneratorRuntime.mark(function _callee10(token, newPassword) { | ||
var user, resetTokens, resetTokenRecord, emails; | ||
return regeneratorRuntime.wrap(function _callee10$(_context10) { | ||
while (1) { | ||
switch (_context10.prev = _context10.next) { | ||
case 0: | ||
_context10.next = 2; | ||
return this.db.findUserByResetPasswordToken(token); | ||
case 2: | ||
user = _context10.sent; | ||
if (user) { | ||
_context10.next = 5; | ||
break; | ||
} | ||
throw new _common.AccountsError('Reset password link expired'); | ||
case 5: | ||
resetTokens = (0, _lodash.get)(user, ['services', 'password', 'resetTokens']); | ||
resetTokenRecord = (0, _lodash.find)(resetTokens, function (t) { | ||
return t.token === token; | ||
}); | ||
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': | ||
return _context10.stop(); | ||
} | ||
} | ||
}, _callee10, this); | ||
})); | ||
function resetPassword(_x19, _x20) { | ||
return _ref11.apply(this, arguments); | ||
} | ||
return resetPassword; | ||
}() | ||
}, { | ||
key: 'setPassword', | ||
@@ -641,25 +882,23 @@ value: function setPassword(userId, newPassword) { | ||
value: function () { | ||
var _ref8 = _asyncToGenerator(regeneratorRuntime.mark(function _callee7(userId, profile) { | ||
var _ref12 = _asyncToGenerator(regeneratorRuntime.mark(function _callee11(userId, profile) { | ||
var user; | ||
return regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
return regeneratorRuntime.wrap(function _callee11$(_context11) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
switch (_context11.prev = _context11.next) { | ||
case 0: | ||
_context7.next = 2; | ||
_context11.next = 2; | ||
return this.db.findUserById(userId); | ||
case 2: | ||
user = _context7.sent; | ||
user = _context11.sent; | ||
if (user) { | ||
_context7.next = 5; | ||
_context11.next = 5; | ||
break; | ||
} | ||
throw new _common.AccountsError({ | ||
message: 'User not found' | ||
}); | ||
throw new _common.AccountsError('User not found', { id: userId }); | ||
case 5: | ||
_context7.next = 7; | ||
_context11.next = 7; | ||
return this.db.setProfile(userId, profile); | ||
@@ -669,10 +908,10 @@ | ||
case 'end': | ||
return _context7.stop(); | ||
return _context11.stop(); | ||
} | ||
} | ||
}, _callee7, this); | ||
}, _callee11, this); | ||
})); | ||
function setProfile(_x13, _x14) { | ||
return _ref8.apply(this, arguments); | ||
function setProfile(_x21, _x22) { | ||
return _ref12.apply(this, arguments); | ||
} | ||
@@ -685,41 +924,39 @@ | ||
value: function () { | ||
var _ref9 = _asyncToGenerator(regeneratorRuntime.mark(function _callee8(userId, profile) { | ||
var _ref13 = _asyncToGenerator(regeneratorRuntime.mark(function _callee12(userId, profile) { | ||
var user, res; | ||
return regeneratorRuntime.wrap(function _callee8$(_context8) { | ||
return regeneratorRuntime.wrap(function _callee12$(_context12) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
switch (_context12.prev = _context12.next) { | ||
case 0: | ||
_context8.next = 2; | ||
_context12.next = 2; | ||
return this.db.findUserById(userId); | ||
case 2: | ||
user = _context8.sent; | ||
user = _context12.sent; | ||
if (user) { | ||
_context8.next = 5; | ||
_context12.next = 5; | ||
break; | ||
} | ||
throw new _common.AccountsError({ | ||
message: 'User not found' | ||
}); | ||
throw new _common.AccountsError('User not found', { id: userId }); | ||
case 5: | ||
_context8.next = 7; | ||
_context12.next = 7; | ||
return this.db.setProfile(userId, _extends({}, user.profile, profile)); | ||
case 7: | ||
res = _context8.sent; | ||
return _context8.abrupt('return', res); | ||
res = _context12.sent; | ||
return _context12.abrupt('return', res); | ||
case 9: | ||
case 'end': | ||
return _context8.stop(); | ||
return _context12.stop(); | ||
} | ||
} | ||
}, _callee8, this); | ||
}, _callee12, this); | ||
})); | ||
function updateProfile(_x15, _x16) { | ||
return _ref9.apply(this, arguments); | ||
function updateProfile(_x23, _x24) { | ||
return _ref13.apply(this, arguments); | ||
} | ||
@@ -729,2 +966,142 @@ | ||
}() | ||
}, { | ||
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; | ||
}() | ||
}, { | ||
key: 'sendResetPasswordEmail', | ||
value: function () { | ||
var _ref15 = _asyncToGenerator(regeneratorRuntime.mark(function _callee14(userId, address) { | ||
var user, emails, 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: | ||
// 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 | ||
emails = user.emails || []; | ||
if (!(!address || !(0, _lodash.includes)(emails.map(function (email) { | ||
return email.address; | ||
}), address))) { | ||
_context14.next = 9; | ||
break; | ||
} | ||
throw new _common.AccountsError('No such email address for user'); | ||
case 9: | ||
token = (0, _tokens.generateRandomToken)(); | ||
_context14.next = 12; | ||
return this.db.addResetPasswordToken(userId, address, token); | ||
case 12: | ||
resetPasswordUrl = this._options.siteUrl + '/reset-password/' + token; | ||
_context14.next = 15; | ||
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 15: | ||
case 'end': | ||
return _context14.stop(); | ||
} | ||
} | ||
}, _callee14, this); | ||
})); | ||
function sendResetPasswordEmail(_x27, _x28) { | ||
return _ref15.apply(this, arguments); | ||
} | ||
return sendResetPasswordEmail; | ||
}() | ||
}]); | ||
@@ -735,54 +1112,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(userId, email) { | ||
return this.instance.verifyEmail(userId, email); | ||
}, | ||
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,14 +18,16 @@ | ||
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) { | ||
var _err$serialize = err.serialize(), | ||
message = _err$serialize.message; | ||
var message = err.message; | ||
@@ -37,8 +37,30 @@ expect(message).toEqual('A database driver is required'); | ||
}); | ||
it('sets the db driver', function () { | ||
var db = {}; | ||
_AccountsServer2.default.config({}, db); | ||
expect(_AccountsServer2.default.instance.db).toEqual(db); | ||
Accounts.config({}, db); | ||
expect(Accounts.db).toEqual(db); | ||
}); | ||
it('set custom password authenticator', function () { | ||
var db = {}; | ||
Accounts.config({ passwordAuthenticator: function passwordAuthenticator() {} }, db); | ||
expect(Accounts._options.passwordAuthenticator).toBeDefined(); | ||
}); | ||
it('use default password authenticator', function () { | ||
var db = {}; | ||
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 = { | ||
@@ -53,2 +75,5 @@ findUserByUsername: function findUserByUsername() { | ||
return Promise.resolve(); | ||
}, | ||
createSession: function createSession() { | ||
return Promise.resolve(); | ||
} | ||
@@ -58,7 +83,6 @@ }; | ||
beforeEach(function () { | ||
_AccountsServer2.default.config({}, db); | ||
Accounts.config({}, db); | ||
}); | ||
it('requires username or an email', _asyncToGenerator(regeneratorRuntime.mark(function _callee() { | ||
var _err$serialize2, message; | ||
var message; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
@@ -68,6 +92,6 @@ while (1) { | ||
case 0: | ||
_AccountsServer2.default.config({}, db); | ||
Accounts.config({}, db); | ||
_context.prev = 1; | ||
_context.next = 4; | ||
return _AccountsServer2.default.createUser({ | ||
return Accounts.createUser({ | ||
password: '123456', | ||
@@ -84,3 +108,3 @@ username: '', | ||
_context.t0 = _context['catch'](1); | ||
_err$serialize2 = _context.t0.serialize(), message = _err$serialize2.message; | ||
message = _context.t0.message; | ||
@@ -97,4 +121,3 @@ expect(message).toEqual('Username or Email is required'); | ||
it('throws error if username exists', _asyncToGenerator(regeneratorRuntime.mark(function _callee2() { | ||
var _err$serialize3, message; | ||
var message; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
@@ -104,3 +127,3 @@ while (1) { | ||
case 0: | ||
_AccountsServer2.default.config({}, _extends({}, db, { | ||
Accounts.config({}, _extends({}, db, { | ||
findUserByUsername: function findUserByUsername() { | ||
@@ -112,3 +135,3 @@ return Promise.resolve('user'); | ||
_context2.next = 4; | ||
return _AccountsServer2.default.createUser({ | ||
return Accounts.createUser({ | ||
password: '123456', | ||
@@ -125,3 +148,3 @@ username: 'user1', | ||
_context2.t0 = _context2['catch'](1); | ||
_err$serialize3 = _context2.t0.serialize(), message = _err$serialize3.message; | ||
message = _context2.t0.message; | ||
@@ -138,4 +161,3 @@ expect(message).toEqual('Username already exists'); | ||
it('throws error if email exists', _asyncToGenerator(regeneratorRuntime.mark(function _callee3() { | ||
var _err$serialize4, message; | ||
var message; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
@@ -145,3 +167,3 @@ while (1) { | ||
case 0: | ||
_AccountsServer2.default.config({}, _extends({}, db, { | ||
Accounts.config({}, _extends({}, db, { | ||
findUserByEmail: function findUserByEmail() { | ||
@@ -153,3 +175,3 @@ return Promise.resolve('user'); | ||
_context3.next = 4; | ||
return _AccountsServer2.default.createUser({ | ||
return Accounts.createUser({ | ||
password: '123456', | ||
@@ -166,3 +188,3 @@ username: '', | ||
_context3.t0 = _context3['catch'](1); | ||
_err$serialize4 = _context3.t0.serialize(), message = _err$serialize4.message; | ||
message = _context3.t0.message; | ||
@@ -184,3 +206,3 @@ expect(message).toEqual('Email already exists'); | ||
case 0: | ||
_AccountsServer2.default.config({}, _extends({}, db, { | ||
Accounts.config({}, _extends({}, db, { | ||
createUser: function createUser() { | ||
@@ -191,3 +213,3 @@ return Promise.resolve('123'); | ||
_context4.next = 3; | ||
return _AccountsServer2.default.createUser({ | ||
return Accounts.createUser({ | ||
password: '123456', | ||
@@ -212,4 +234,3 @@ username: 'user1' | ||
it('throws error if user is undefined', _asyncToGenerator(regeneratorRuntime.mark(function _callee5() { | ||
var _err$serialize5, message; | ||
var message; | ||
return regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
@@ -221,3 +242,3 @@ while (1) { | ||
_context5.next = 3; | ||
return _AccountsServer2.default.loginWithPassword(null, '123456'); | ||
return Accounts.loginWithPassword(null, '123456'); | ||
@@ -230,5 +251,5 @@ case 3: | ||
_context5.t0 = _context5['catch'](0); | ||
_err$serialize5 = _context5.t0.serialize(), message = _err$serialize5.message; | ||
message = _context5.t0.message; | ||
expect(message).toEqual('Unrecognized options for login request [400]'); | ||
expect(message).toEqual('Unrecognized options for login request'); | ||
@@ -243,4 +264,3 @@ case 10: | ||
it('throws error if password is undefined', _asyncToGenerator(regeneratorRuntime.mark(function _callee6() { | ||
var _err$serialize6, message; | ||
var message; | ||
return regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
@@ -252,3 +272,3 @@ while (1) { | ||
_context6.next = 3; | ||
return _AccountsServer2.default.loginWithPassword('username', null); | ||
return Accounts.loginWithPassword('username', null); | ||
@@ -261,5 +281,5 @@ case 3: | ||
_context6.t0 = _context6['catch'](0); | ||
_err$serialize6 = _context6.t0.serialize(), message = _err$serialize6.message; | ||
message = _context6.t0.message; | ||
expect(message).toEqual('Unrecognized options for login request [400]'); | ||
expect(message).toEqual('Unrecognized options for login request'); | ||
@@ -274,4 +294,3 @@ case 10: | ||
it('throws error if user is not a string or an object', _asyncToGenerator(regeneratorRuntime.mark(function _callee7() { | ||
var _err$serialize7, message; | ||
var message; | ||
return regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
@@ -283,3 +302,3 @@ while (1) { | ||
_context7.next = 3; | ||
return _AccountsServer2.default.loginWithPassword(1, '123456'); | ||
return Accounts.loginWithPassword(1, '123456'); | ||
@@ -292,5 +311,5 @@ case 3: | ||
_context7.t0 = _context7['catch'](0); | ||
_err$serialize7 = _context7.t0.serialize(), message = _err$serialize7.message; | ||
message = _context7.t0.message; | ||
expect(message).toEqual('Match failed [400]'); | ||
expect(message).toEqual('Match failed'); | ||
@@ -305,4 +324,3 @@ case 10: | ||
it('throws error if password is not a string', _asyncToGenerator(regeneratorRuntime.mark(function _callee8() { | ||
var _err$serialize8, message; | ||
var message; | ||
return regeneratorRuntime.wrap(function _callee8$(_context8) { | ||
@@ -314,3 +332,3 @@ while (1) { | ||
_context8.next = 3; | ||
return _AccountsServer2.default.loginWithPassword('username', {}); | ||
return Accounts.loginWithPassword('username', {}); | ||
@@ -323,5 +341,5 @@ case 3: | ||
_context8.t0 = _context8['catch'](0); | ||
_err$serialize8 = _context8.t0.serialize(), message = _err$serialize8.message; | ||
message = _context8.t0.message; | ||
expect(message).toEqual('Match failed [400]'); | ||
expect(message).toEqual('Match failed'); | ||
@@ -336,4 +354,3 @@ case 10: | ||
it('throws error if user is not found', _asyncToGenerator(regeneratorRuntime.mark(function _callee9() { | ||
var _err$serialize9, message; | ||
var message; | ||
return regeneratorRuntime.wrap(function _callee9$(_context9) { | ||
@@ -343,3 +360,3 @@ while (1) { | ||
case 0: | ||
_AccountsServer2.default.config({}, { | ||
Accounts.config({}, { | ||
findUserByUsername: function findUserByUsername() { | ||
@@ -354,3 +371,3 @@ return Promise.resolve(null); | ||
_context9.next = 4; | ||
return _AccountsServer2.default.loginWithPassword('username', '123456'); | ||
return Accounts.loginWithPassword('username', '123456'); | ||
@@ -363,5 +380,5 @@ case 4: | ||
_context9.t0 = _context9['catch'](1); | ||
_err$serialize9 = _context9.t0.serialize(), message = _err$serialize9.message; | ||
message = _context9.t0.message; | ||
expect(message).toEqual('User not found [403]'); | ||
expect(message).toEqual('User not found'); | ||
@@ -376,4 +393,3 @@ case 11: | ||
it('throws error if password not set', _asyncToGenerator(regeneratorRuntime.mark(function _callee10() { | ||
var _err$serialize10, message; | ||
var message; | ||
return regeneratorRuntime.wrap(function _callee10$(_context10) { | ||
@@ -383,3 +399,3 @@ while (1) { | ||
case 0: | ||
_AccountsServer2.default.config({}, { | ||
Accounts.config({}, { | ||
findUserByUsername: function findUserByUsername() { | ||
@@ -397,3 +413,3 @@ return Promise.resolve('123'); | ||
_context10.next = 4; | ||
return _AccountsServer2.default.loginWithPassword('username', '123456'); | ||
return Accounts.loginWithPassword('username', '123456'); | ||
@@ -406,5 +422,5 @@ case 4: | ||
_context10.t0 = _context10['catch'](1); | ||
_err$serialize10 = _context10.t0.serialize(), message = _err$serialize10.message; | ||
message = _context10.t0.message; | ||
expect(message).toEqual('User has no password set [403]'); | ||
expect(message).toEqual('User has no password set'); | ||
@@ -419,4 +435,3 @@ case 11: | ||
it('throws error if password is incorrect', _asyncToGenerator(regeneratorRuntime.mark(function _callee11() { | ||
var _err$serialize11, message; | ||
var message; | ||
return regeneratorRuntime.wrap(function _callee11$(_context11) { | ||
@@ -426,3 +441,3 @@ while (1) { | ||
case 0: | ||
_AccountsServer2.default.config({}, { | ||
Accounts.config({}, { | ||
findUserByUsername: function findUserByUsername() { | ||
@@ -443,3 +458,3 @@ return Promise.resolve('123'); | ||
_context11.next = 4; | ||
return _AccountsServer2.default.loginWithPassword('username', '123456'); | ||
return Accounts.loginWithPassword('username', '123456'); | ||
@@ -452,5 +467,5 @@ case 4: | ||
_context11.t0 = _context11['catch'](1); | ||
_err$serialize11 = _context11.t0.serialize(), message = _err$serialize11.message; | ||
message = _context11.t0.message; | ||
expect(message).toEqual('Incorrect password [403]'); | ||
expect(message).toEqual('Incorrect password'); | ||
@@ -464,9 +479,47 @@ case 11: | ||
}))); | ||
it('should use custom password authenticator when specified', _asyncToGenerator(regeneratorRuntime.mark(function _callee12() { | ||
var user, authenticator, result; | ||
return regeneratorRuntime.wrap(function _callee12$(_context12) { | ||
while (1) { | ||
switch (_context12.prev = _context12.next) { | ||
case 0: | ||
user = { | ||
id: '123', | ||
username: 'username', | ||
email: 'email@email.com', | ||
profile: { | ||
bio: 'bio' | ||
} | ||
}; | ||
authenticator = jest.fn(function () { | ||
return Promise.resolve(user); | ||
}); | ||
Accounts.config({ passwordAuthenticator: authenticator }, db); | ||
_context12.next = 5; | ||
return Accounts.loginWithPassword('username', '123456'); | ||
case 5: | ||
result = _context12.sent; | ||
expect(result).toBeDefined(); | ||
expect(authenticator.mock.calls.length).toEqual(1); | ||
case 8: | ||
case 'end': | ||
return _context12.stop(); | ||
} | ||
} | ||
}, _callee12, undefined); | ||
}))); | ||
describe('loginWithUser', function () { | ||
it('login using id', _asyncToGenerator(regeneratorRuntime.mark(function _callee12() { | ||
it('login using id', _asyncToGenerator(regeneratorRuntime.mark(function _callee13() { | ||
var hash, user, findUserById, res, _res$tokens, accessToken, refreshToken, decodedAccessToken; | ||
return regeneratorRuntime.wrap(function _callee12$(_context12) { | ||
return regeneratorRuntime.wrap(function _callee13$(_context13) { | ||
while (1) { | ||
switch (_context12.prev = _context12.next) { | ||
switch (_context13.prev = _context13.next) { | ||
case 0: | ||
@@ -486,3 +539,3 @@ hash = (0, _encryption.hashPassword)('1234567'); | ||
_AccountsServer2.default.config({}, { | ||
Accounts.config({}, { | ||
findUserById: findUserById, | ||
@@ -496,7 +549,7 @@ findPasswordHash: function findPasswordHash() { | ||
}); | ||
_context12.next = 6; | ||
return _AccountsServer2.default.loginWithPassword({ id: '123' }, '1234567'); | ||
_context13.next = 6; | ||
return Accounts.loginWithPassword({ id: '123' }, '1234567'); | ||
case 6: | ||
res = _context12.sent; | ||
res = _context13.sent; | ||
@@ -514,15 +567,15 @@ expect(findUserById.mock.calls[0][0]).toEqual('123'); | ||
case 'end': | ||
return _context12.stop(); | ||
return _context13.stop(); | ||
} | ||
} | ||
}, _callee12, undefined); | ||
}, _callee13, undefined); | ||
}))); | ||
}); | ||
describe('refreshTokens', function () { | ||
it('updates session and returns new tokens and user', _asyncToGenerator(regeneratorRuntime.mark(function _callee13() { | ||
var updateSession, user, _Accounts$instance$cr, accessToken, refreshToken, res; | ||
it('updates session and returns new tokens and user', _asyncToGenerator(regeneratorRuntime.mark(function _callee14() { | ||
var updateSession, user, _Accounts$createToken, accessToken, refreshToken, res; | ||
return regeneratorRuntime.wrap(function _callee13$(_context13) { | ||
return regeneratorRuntime.wrap(function _callee14$(_context14) { | ||
while (1) { | ||
switch (_context13.prev = _context13.next) { | ||
switch (_context14.prev = _context14.next) { | ||
case 0: | ||
@@ -537,3 +590,3 @@ updateSession = jest.fn(function () { | ||
_AccountsServer2.default.config({}, { | ||
Accounts.config({}, { | ||
findSessionById: function findSessionById() { | ||
@@ -551,5 +604,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 { | ||
@@ -560,7 +613,7 @@ accessToken: 'newAccessToken', | ||
}; | ||
_context13.next = 7; | ||
return _AccountsServer2.default.refreshTokens(accessToken, refreshToken, 'ip', 'user agent'); | ||
_context14.next = 7; | ||
return Accounts.refreshTokens(accessToken, refreshToken, 'ip', 'user agent'); | ||
case 7: | ||
res = _context13.sent; | ||
res = _context14.sent; | ||
@@ -575,17 +628,17 @@ expect(updateSession.mock.calls[0]).toEqual(['456', 'ip', 'user agent']); | ||
case 'end': | ||
return _context13.stop(); | ||
return _context14.stop(); | ||
} | ||
} | ||
}, _callee13, undefined); | ||
}, _callee14, undefined); | ||
}))); | ||
it('requires access and refresh tokens', _asyncToGenerator(regeneratorRuntime.mark(function _callee14() { | ||
return regeneratorRuntime.wrap(function _callee14$(_context14) { | ||
it('requires access and refresh tokens', _asyncToGenerator(regeneratorRuntime.mark(function _callee15() { | ||
return regeneratorRuntime.wrap(function _callee15$(_context15) { | ||
while (1) { | ||
switch (_context14.prev = _context14.next) { | ||
switch (_context15.prev = _context15.next) { | ||
case 0: | ||
_AccountsServer2.default.config({}, {}); | ||
_context14.prev = 1; | ||
_context14.next = 4; | ||
return _AccountsServer2.default.refreshTokens(); | ||
Accounts.config({}, {}); | ||
_context15.prev = 1; | ||
_context15.next = 4; | ||
return Accounts.refreshTokens(); | ||
@@ -596,23 +649,23 @@ case 4: | ||
case 7: | ||
_context14.prev = 7; | ||
_context14.t0 = _context14['catch'](1); | ||
_context15.prev = 7; | ||
_context15.t0 = _context15['catch'](1); | ||
expect(_context14.t0.serialize().message).toEqual('An accessToken and refreshToken are required'); | ||
expect(_context15.t0.message).toEqual('An accessToken and refreshToken are required'); | ||
case 10: | ||
case 'end': | ||
return _context14.stop(); | ||
return _context15.stop(); | ||
} | ||
} | ||
}, _callee14, undefined, [[1, 7]]); | ||
}, _callee15, undefined, [[1, 7]]); | ||
}))); | ||
it('throws error if tokens are not valid', _asyncToGenerator(regeneratorRuntime.mark(function _callee15() { | ||
return regeneratorRuntime.wrap(function _callee15$(_context15) { | ||
it('throws error if tokens are not valid', _asyncToGenerator(regeneratorRuntime.mark(function _callee16() { | ||
return regeneratorRuntime.wrap(function _callee16$(_context16) { | ||
while (1) { | ||
switch (_context15.prev = _context15.next) { | ||
switch (_context16.prev = _context16.next) { | ||
case 0: | ||
_AccountsServer2.default.config({}, {}); | ||
_context15.prev = 1; | ||
_context15.next = 4; | ||
return _AccountsServer2.default.refreshTokens('bad access token', 'bad refresh token'); | ||
Accounts.config({}, {}); | ||
_context16.prev = 1; | ||
_context16.next = 4; | ||
return Accounts.refreshTokens('bad access token', 'bad refresh token'); | ||
@@ -623,22 +676,22 @@ case 4: | ||
case 7: | ||
_context15.prev = 7; | ||
_context15.t0 = _context15['catch'](1); | ||
_context16.prev = 7; | ||
_context16.t0 = _context16['catch'](1); | ||
expect(_context15.t0.serialize().message).toEqual('Tokens are not valid'); | ||
expect(_context16.t0.message).toEqual('Tokens are not valid'); | ||
case 10: | ||
case 'end': | ||
return _context15.stop(); | ||
return _context16.stop(); | ||
} | ||
} | ||
}, _callee15, undefined, [[1, 7]]); | ||
}, _callee16, undefined, [[1, 7]]); | ||
}))); | ||
it('throws error if session not found', _asyncToGenerator(regeneratorRuntime.mark(function _callee16() { | ||
var _Accounts$instance$cr2, accessToken, refreshToken; | ||
it('throws error if session not found', _asyncToGenerator(regeneratorRuntime.mark(function _callee17() { | ||
var _Accounts$createToken2, accessToken, refreshToken; | ||
return regeneratorRuntime.wrap(function _callee16$(_context16) { | ||
return regeneratorRuntime.wrap(function _callee17$(_context17) { | ||
while (1) { | ||
switch (_context16.prev = _context16.next) { | ||
switch (_context17.prev = _context17.next) { | ||
case 0: | ||
_AccountsServer2.default.config({}, { | ||
Accounts.config({}, { | ||
findSessionById: function findSessionById() { | ||
@@ -648,6 +701,6 @@ return Promise.resolve(null); | ||
}); | ||
_context16.prev = 1; | ||
_Accounts$instance$cr2 = _AccountsServer2.default.instance.createTokens(), accessToken = _Accounts$instance$cr2.accessToken, refreshToken = _Accounts$instance$cr2.refreshToken; | ||
_context16.next = 5; | ||
return _AccountsServer2.default.refreshTokens(accessToken, refreshToken); | ||
_context17.prev = 1; | ||
_Accounts$createToken2 = Accounts.createTokens(), accessToken = _Accounts$createToken2.accessToken, refreshToken = _Accounts$createToken2.refreshToken; | ||
_context17.next = 5; | ||
return Accounts.refreshTokens(accessToken, refreshToken); | ||
@@ -658,22 +711,22 @@ case 5: | ||
case 8: | ||
_context16.prev = 8; | ||
_context16.t0 = _context16['catch'](1); | ||
_context17.prev = 8; | ||
_context17.t0 = _context17['catch'](1); | ||
expect(_context16.t0.serialize().message).toEqual('Session not found'); | ||
expect(_context17.t0.message).toEqual('Session not found'); | ||
case 11: | ||
case 'end': | ||
return _context16.stop(); | ||
return _context17.stop(); | ||
} | ||
} | ||
}, _callee16, undefined, [[1, 8]]); | ||
}, _callee17, undefined, [[1, 8]]); | ||
}))); | ||
it('throws error if session not valid', _asyncToGenerator(regeneratorRuntime.mark(function _callee17() { | ||
var _Accounts$instance$cr3, accessToken, refreshToken; | ||
it('throws error if session not valid', _asyncToGenerator(regeneratorRuntime.mark(function _callee18() { | ||
var _Accounts$createToken3, accessToken, refreshToken; | ||
return regeneratorRuntime.wrap(function _callee17$(_context17) { | ||
return regeneratorRuntime.wrap(function _callee18$(_context18) { | ||
while (1) { | ||
switch (_context17.prev = _context17.next) { | ||
switch (_context18.prev = _context18.next) { | ||
case 0: | ||
_AccountsServer2.default.config({}, { | ||
Accounts.config({}, { | ||
findSessionById: function findSessionById() { | ||
@@ -685,6 +738,6 @@ return Promise.resolve({ | ||
}); | ||
_context17.prev = 1; | ||
_Accounts$instance$cr3 = _AccountsServer2.default.instance.createTokens(), accessToken = _Accounts$instance$cr3.accessToken, refreshToken = _Accounts$instance$cr3.refreshToken; | ||
_context17.next = 5; | ||
return _AccountsServer2.default.refreshTokens(accessToken, refreshToken); | ||
_context18.prev = 1; | ||
_Accounts$createToken3 = Accounts.createTokens(), accessToken = _Accounts$createToken3.accessToken, refreshToken = _Accounts$createToken3.refreshToken; | ||
_context18.next = 5; | ||
return Accounts.refreshTokens(accessToken, refreshToken); | ||
@@ -695,23 +748,65 @@ case 5: | ||
case 8: | ||
_context17.prev = 8; | ||
_context17.t0 = _context17['catch'](1); | ||
_context18.prev = 8; | ||
_context18.t0 = _context18['catch'](1); | ||
expect(_context17.t0.serialize().message).toEqual('Session is no longer valid'); | ||
expect(_context18.t0.message).toEqual('Session is no longer valid'); | ||
case 11: | ||
case 'end': | ||
return _context17.stop(); | ||
return _context18.stop(); | ||
} | ||
} | ||
}, _callee17, undefined, [[1, 8]]); | ||
}, _callee18, undefined, [[1, 8]]); | ||
}))); | ||
}); | ||
describe('logout', function () { | ||
it('invalidates session', _asyncToGenerator(regeneratorRuntime.mark(function _callee18() { | ||
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; | ||
return regeneratorRuntime.wrap(function _callee18$(_context18) { | ||
return regeneratorRuntime.wrap(function _callee19$(_context19) { | ||
while (1) { | ||
switch (_context18.prev = _context18.next) { | ||
switch (_context19.prev = _context19.next) { | ||
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 () { | ||
@@ -725,3 +820,3 @@ return Promise.resolve(); | ||
_AccountsServer2.default.config({}, { | ||
Accounts.config({}, { | ||
findSessionById: function findSessionById() { | ||
@@ -739,5 +834,5 @@ return Promise.resolve({ | ||
}); | ||
_Accounts$instance$cr4 = _AccountsServer2.default.instance.createTokens('456'), accessToken = _Accounts$instance$cr4.accessToken; | ||
_context18.next = 6; | ||
return _AccountsServer2.default.logout(accessToken); | ||
_Accounts$createToken5 = Accounts.createTokens('456'), accessToken = _Accounts$createToken5.accessToken; | ||
_context20.next = 6; | ||
return Accounts.logout(accessToken); | ||
@@ -749,18 +844,18 @@ case 6: | ||
case 'end': | ||
return _context18.stop(); | ||
return _context20.stop(); | ||
} | ||
} | ||
}, _callee18, undefined); | ||
}, _callee20, undefined); | ||
}))); | ||
}); | ||
describe('findSessionByAccessToken', function () { | ||
it('requires access token', _asyncToGenerator(regeneratorRuntime.mark(function _callee19() { | ||
return regeneratorRuntime.wrap(function _callee19$(_context19) { | ||
it('requires access token', _asyncToGenerator(regeneratorRuntime.mark(function _callee21() { | ||
return regeneratorRuntime.wrap(function _callee21$(_context21) { | ||
while (1) { | ||
switch (_context19.prev = _context19.next) { | ||
switch (_context21.prev = _context21.next) { | ||
case 0: | ||
_AccountsServer2.default.config({}, {}); | ||
_context19.prev = 1; | ||
_context19.next = 4; | ||
return _AccountsServer2.default.logout(); | ||
Accounts.config({}, {}); | ||
_context21.prev = 1; | ||
_context21.next = 4; | ||
return Accounts.logout(); | ||
@@ -771,23 +866,23 @@ case 4: | ||
case 7: | ||
_context19.prev = 7; | ||
_context19.t0 = _context19['catch'](1); | ||
_context21.prev = 7; | ||
_context21.t0 = _context21['catch'](1); | ||
expect(_context19.t0.serialize().message).toEqual('An accessToken is required'); | ||
expect(_context21.t0.message).toEqual('An accessToken is required'); | ||
case 10: | ||
case 'end': | ||
return _context19.stop(); | ||
return _context21.stop(); | ||
} | ||
} | ||
}, _callee19, undefined, [[1, 7]]); | ||
}, _callee21, undefined, [[1, 7]]); | ||
}))); | ||
it('throws error if tokens are not valid', _asyncToGenerator(regeneratorRuntime.mark(function _callee20() { | ||
return regeneratorRuntime.wrap(function _callee20$(_context20) { | ||
it('throws error if tokens are not valid', _asyncToGenerator(regeneratorRuntime.mark(function _callee22() { | ||
return regeneratorRuntime.wrap(function _callee22$(_context22) { | ||
while (1) { | ||
switch (_context20.prev = _context20.next) { | ||
switch (_context22.prev = _context22.next) { | ||
case 0: | ||
_AccountsServer2.default.config({}, {}); | ||
_context20.prev = 1; | ||
_context20.next = 4; | ||
return _AccountsServer2.default.logout('bad access token'); | ||
Accounts.config({}, {}); | ||
_context22.prev = 1; | ||
_context22.next = 4; | ||
return Accounts.logout('bad access token'); | ||
@@ -798,22 +893,22 @@ case 4: | ||
case 7: | ||
_context20.prev = 7; | ||
_context20.t0 = _context20['catch'](1); | ||
_context22.prev = 7; | ||
_context22.t0 = _context22['catch'](1); | ||
expect(_context20.t0.serialize().message).toEqual('Tokens are not valid'); | ||
expect(_context22.t0.message).toEqual('Tokens are not valid'); | ||
case 10: | ||
case 'end': | ||
return _context20.stop(); | ||
return _context22.stop(); | ||
} | ||
} | ||
}, _callee20, undefined, [[1, 7]]); | ||
}, _callee22, undefined, [[1, 7]]); | ||
}))); | ||
it('throws error if session not found', _asyncToGenerator(regeneratorRuntime.mark(function _callee21() { | ||
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 _callee21$(_context21) { | ||
return regeneratorRuntime.wrap(function _callee23$(_context23) { | ||
while (1) { | ||
switch (_context21.prev = _context21.next) { | ||
switch (_context23.prev = _context23.next) { | ||
case 0: | ||
_AccountsServer2.default.config({}, { | ||
Accounts.config({}, { | ||
findSessionById: function findSessionById() { | ||
@@ -823,6 +918,6 @@ return Promise.resolve(null); | ||
}); | ||
_context21.prev = 1; | ||
_Accounts$instance$cr5 = _AccountsServer2.default.instance.createTokens(), accessToken = _Accounts$instance$cr5.accessToken; | ||
_context21.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); | ||
@@ -833,22 +928,22 @@ case 5: | ||
case 8: | ||
_context21.prev = 8; | ||
_context21.t0 = _context21['catch'](1); | ||
_context23.prev = 8; | ||
_context23.t0 = _context23['catch'](1); | ||
expect(_context21.t0.serialize().message).toEqual('Session not found'); | ||
expect(_context23.t0.message).toEqual('Session not found'); | ||
case 11: | ||
case 'end': | ||
return _context21.stop(); | ||
return _context23.stop(); | ||
} | ||
} | ||
}, _callee21, undefined, [[1, 8]]); | ||
}, _callee23, undefined, [[1, 8]]); | ||
}))); | ||
it('throws error if session not valid', _asyncToGenerator(regeneratorRuntime.mark(function _callee22() { | ||
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 _callee22$(_context22) { | ||
return regeneratorRuntime.wrap(function _callee24$(_context24) { | ||
while (1) { | ||
switch (_context22.prev = _context22.next) { | ||
switch (_context24.prev = _context24.next) { | ||
case 0: | ||
_AccountsServer2.default.config({}, { | ||
Accounts.config({}, { | ||
findSessionById: function findSessionById() { | ||
@@ -860,6 +955,6 @@ return Promise.resolve({ | ||
}); | ||
_context22.prev = 1; | ||
_Accounts$instance$cr6 = _AccountsServer2.default.instance.createTokens(), accessToken = _Accounts$instance$cr6.accessToken; | ||
_context22.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); | ||
@@ -870,23 +965,210 @@ case 5: | ||
case 8: | ||
_context22.prev = 8; | ||
_context22.t0 = _context22['catch'](1); | ||
_context24.prev = 8; | ||
_context24.t0 = _context24['catch'](1); | ||
expect(_context22.t0.serialize().message).toEqual('Session is no longer valid'); | ||
expect(_context24.t0.message).toEqual('Session is no longer valid'); | ||
case 11: | ||
case 'end': | ||
return _context22.stop(); | ||
return _context24.stop(); | ||
} | ||
} | ||
}, _callee22, 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 _callee23() { | ||
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 _callee23$(_context23) { | ||
return regeneratorRuntime.wrap(function _callee30$(_context30) { | ||
while (1) { | ||
switch (_context23.prev = _context23.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 = { | ||
@@ -897,6 +1179,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, | ||
@@ -910,8 +1233,8 @@ userId: '123' | ||
}); | ||
_Accounts$instance$cr7 = _AccountsServer2.default.instance.createTokens('456'), accessToken = _Accounts$instance$cr7.accessToken; | ||
_context23.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 = _context23.sent; | ||
foundUser = _context32.sent; | ||
@@ -922,14 +1245,143 @@ expect(foundUser).toEqual(user); | ||
case 'end': | ||
return _context23.stop(); | ||
return _context32.stop(); | ||
} | ||
} | ||
}, _callee23, undefined); | ||
}, _callee32, undefined); | ||
}))); | ||
}); | ||
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 (_context33.prev = _context33.next) { | ||
case 0: | ||
resumeSessionValidator = jest.fn(function () { | ||
return Promise.resolve({}); | ||
}); | ||
user = { | ||
userId: '123', | ||
username: 'username' | ||
}; | ||
Accounts.config({ resumeSessionValidator: resumeSessionValidator }, { | ||
findSessionById: function findSessionById() { | ||
return Promise.resolve({ | ||
sessionId: '456', | ||
valid: true, | ||
userId: '123' | ||
}); | ||
}, | ||
findUserById: function findUserById() { | ||
return Promise.resolve(user); | ||
} | ||
}); | ||
_Accounts$createToken11 = Accounts.createTokens('456'), accessToken = _Accounts$createToken11.accessToken; | ||
_context33.next = 6; | ||
return Accounts.resumeSession(accessToken); | ||
case 6: | ||
expect(resumeSessionValidator.mock.calls.length).toBe(1); | ||
case 7: | ||
case 'end': | ||
return _context33.stop(); | ||
} | ||
} | ||
}, _callee33, undefined); | ||
}))); | ||
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 (_context34.prev = _context34.next) { | ||
case 0: | ||
resumeSessionValidator = jest.fn(function () { | ||
return Promise.reject('Custom session error'); | ||
}); | ||
user = { | ||
userId: '123', | ||
username: 'username' | ||
}; | ||
Accounts.config({ resumeSessionValidator: resumeSessionValidator }, { | ||
findSessionById: function findSessionById() { | ||
return Promise.resolve({ | ||
sessionId: '456', | ||
valid: true, | ||
userId: '123' | ||
}); | ||
}, | ||
findUserById: function findUserById() { | ||
return Promise.resolve(user); | ||
} | ||
}); | ||
_Accounts$createToken12 = Accounts.createTokens('456'), accessToken = _Accounts$createToken12.accessToken; | ||
_context34.prev = 4; | ||
_context34.next = 7; | ||
return Accounts.resumeSession(accessToken); | ||
case 7: | ||
throw new Error(); | ||
case 10: | ||
_context34.prev = 10; | ||
_context34.t0 = _context34['catch'](4); | ||
expect(resumeSessionValidator.mock.calls.length).toBe(1); | ||
expect(_context34.t0.message).toEqual('Custom session error'); | ||
case 14: | ||
case 'end': | ||
return _context34.stop(); | ||
} | ||
} | ||
}, _callee34, undefined, [[4, 10]]); | ||
}))); | ||
describe('setProfile', function () { | ||
it('calls set profile on db interface', _asyncToGenerator(regeneratorRuntime.mark(function _callee24() { | ||
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 _callee24$(_context24) { | ||
return regeneratorRuntime.wrap(function _callee36$(_context36) { | ||
while (1) { | ||
switch (_context24.prev = _context24.next) { | ||
switch (_context36.prev = _context36.next) { | ||
case 0: | ||
@@ -945,3 +1397,3 @@ user = { | ||
_AccountsServer2.default.config({}, { | ||
Accounts.config({}, { | ||
findUserById: function findUserById() { | ||
@@ -952,4 +1404,4 @@ return Promise.resolve(user); | ||
}); | ||
_context24.next = 6; | ||
return _AccountsServer2.default.setProfile('123', profile); | ||
_context36.next = 6; | ||
return Accounts.setProfile('123', profile); | ||
@@ -963,12 +1415,13 @@ case 6: | ||
case 'end': | ||
return _context24.stop(); | ||
return _context36.stop(); | ||
} | ||
} | ||
}, _callee24, undefined); | ||
}, _callee36, undefined); | ||
}))); | ||
it('merges profile and calls set profile on db interface', _asyncToGenerator(regeneratorRuntime.mark(function _callee25() { | ||
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 _callee25$(_context25) { | ||
return regeneratorRuntime.wrap(function _callee37$(_context37) { | ||
while (1) { | ||
switch (_context25.prev = _context25.next) { | ||
switch (_context37.prev = _context37.next) { | ||
case 0: | ||
@@ -993,3 +1446,3 @@ user = { | ||
_AccountsServer2.default.config({}, { | ||
Accounts.config({}, { | ||
findUserById: function findUserById() { | ||
@@ -1000,7 +1453,7 @@ return Promise.resolve(user); | ||
}); | ||
_context25.next = 7; | ||
return _AccountsServer2.default.updateProfile('123', profile); | ||
_context37.next = 7; | ||
return Accounts.updateProfile('123', profile); | ||
case 7: | ||
res = _context25.sent; | ||
res = _context37.sent; | ||
@@ -1014,9 +1467,295 @@ expect(setProfile.mock.calls.length).toEqual(1); | ||
case 'end': | ||
return _context25.stop(); | ||
return _context37.stop(); | ||
} | ||
} | ||
}, _callee25, 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); | ||
}))); | ||
}); | ||
}); | ||
}); |
@@ -6,3 +6,3 @@ 'use strict'; | ||
}); | ||
exports.generateRefreshToken = exports.generateAccessToken = undefined; | ||
exports.generateRefreshToken = exports.generateAccessToken = exports.generateRandomToken = undefined; | ||
@@ -13,4 +13,13 @@ var _jsonwebtoken = require('jsonwebtoken'); | ||
var _crypto = require('crypto'); | ||
var _crypto2 = _interopRequireDefault(_crypto); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var generateRandomToken = exports.generateRandomToken = function generateRandomToken() { | ||
var length = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 43; | ||
return _crypto2.default.randomBytes(length).toString('hex'); | ||
}; | ||
var generateAccessToken = exports.generateAccessToken = function generateAccessToken(_ref) { | ||
@@ -24,2 +33,3 @@ var secret = _ref.secret, | ||
}; | ||
var generateRefreshToken = exports.generateRefreshToken = function generateRefreshToken(_ref2) { | ||
@@ -26,0 +36,0 @@ var secret = _ref2.secret, |
{ | ||
"name": "@accounts/server", | ||
"version": "0.0.3", | ||
"version": "0.0.4-alpha.98931215", | ||
"description": "Fullstack authentication and accounts-management", | ||
@@ -12,3 +12,3 @@ "main": "lib/index.js", | ||
"compile": "babel ./src --out-dir ./lib", | ||
"flow:check": "flow check", | ||
"postcompile": "npm run flow:prepublish", | ||
"prepublish": "npm run compile", | ||
@@ -18,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", | ||
@@ -24,3 +26,6 @@ "coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" | ||
"testEnvironment": "node", | ||
"testRegex": "(/.*.(test|spec)).(js|jsx)$" | ||
"testRegex": "(/.*.(test|spec)).(js|jsx)$", | ||
"testPathDirs": [ | ||
"src" | ||
] | ||
}, | ||
@@ -44,5 +49,6 @@ "repository": { | ||
"dependencies": { | ||
"@accounts/common": "^0.0.3", | ||
"apollo-errors": "^1.2.1", | ||
"@accounts/common": "^0.0.4-alpha.98931215", | ||
"bcryptjs": "^2.4.0", | ||
"crypto": "^0.0.3", | ||
"emailjs": "^1.0.8", | ||
"jsonwebtoken": "^7.2.1", | ||
@@ -64,3 +70,3 @@ "jwt-decode": "^2.1.0", | ||
"coveralls": "^2.11.14", | ||
"flow-bin": "^0.37.0", | ||
"flow-bin": "^0.39.0", | ||
"jest": "^18.0.0", | ||
@@ -67,0 +73,0 @@ "localstorage-polyfill": "^1.0.1", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
323535
31
3950
7
1
+ Addedcrypto@^0.0.3
+ Addedemailjs@^1.0.8
+ Added@accounts/common@0.0.4(transitive)
+ Addedaddressparser@0.2.10.3.2(transitive)
+ Addedbufferjs@1.1.0(transitive)
+ Addedcrypto@0.0.3(transitive)
+ Addedemailjs@1.0.12(transitive)
+ Addedencoding@0.1.13(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedmimelib@0.2.14(transitive)
+ Addedmoment@2.15.2(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedstarttls@1.0.1(transitive)
- Removedapollo-errors@^1.2.1
- Removed@accounts/common@0.0.3(transitive)
- Removedapollo-errors@1.9.0(transitive)
- Removedassert@1.5.1(transitive)
- Removedcall-bind@1.0.8(transitive)
- Removedcall-bind-apply-helpers@1.0.1(transitive)
- Removedcall-bound@1.0.3(transitive)
- Removeddefine-data-property@1.1.4(transitive)
- Removeddefine-properties@1.2.1(transitive)
- Removeddunder-proto@1.0.1(transitive)
- Removedes-define-property@1.0.1(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedes-object-atoms@1.1.1(transitive)
- Removedextendable-error@0.1.7(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedget-intrinsic@1.2.7(transitive)
- Removedget-proto@1.0.1(transitive)
- Removedgopd@1.2.0(transitive)
- Removedhas-property-descriptors@1.0.2(transitive)
- Removedhas-symbols@1.1.0(transitive)
- Removedhasown@2.0.2(transitive)
- Removedinherits@2.0.3(transitive)
- Removedmath-intrinsics@1.1.0(transitive)
- Removedmoment@2.30.1(transitive)
- Removedobject-keys@1.1.1(transitive)
- Removedobject.assign@4.1.7(transitive)
- Removedset-function-length@1.2.2(transitive)
- Removedutil@0.10.4(transitive)