@accounts/client
Advanced tools
Comparing version 0.0.10-alpha.18d76699 to 0.0.10-alpha.359b8b92
@@ -158,11 +158,6 @@ 'use strict'; | ||
}, { | ||
key: 'user', | ||
value: function user() { | ||
var user = this.getState().get('user'); | ||
return user ? user.toJS() : null; | ||
} | ||
}, { | ||
key: 'tokens', | ||
key: 'loadTokensFromStorage', | ||
value: function () { | ||
var _ref4 = _asyncToGenerator(regeneratorRuntime.mark(function _callee4() { | ||
var tokens; | ||
return regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
@@ -182,8 +177,10 @@ while (1) { | ||
_context4.t1 = _context4.sent; | ||
return _context4.abrupt('return', { | ||
tokens = { | ||
accessToken: _context4.t0, | ||
refreshToken: _context4.t1 | ||
}); | ||
}; | ||
case 7: | ||
this.store.dispatch((0, _module.setTokens)(tokens)); | ||
case 8: | ||
case 'end': | ||
@@ -196,12 +193,21 @@ return _context4.stop(); | ||
function tokens() { | ||
function loadTokensFromStorage() { | ||
return _ref4.apply(this, arguments); | ||
} | ||
return tokens; | ||
return loadTokensFromStorage; | ||
}() | ||
}, { | ||
key: 'clearTokens', | ||
key: 'user', | ||
value: function user() { | ||
var user = this.getState().get('user'); | ||
return user ? user.toJS() : null; | ||
} | ||
}, { | ||
key: 'impersonate', | ||
value: function () { | ||
var _ref5 = _asyncToGenerator(regeneratorRuntime.mark(function _callee5() { | ||
var _ref5 = _asyncToGenerator(regeneratorRuntime.mark(function _callee5(username) { | ||
var _ref6, accessToken, refreshToken, res; | ||
return regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
@@ -211,8 +217,144 @@ while (1) { | ||
case 0: | ||
if ((0, _lodash.isString)(username)) { | ||
_context5.next = 2; | ||
break; | ||
} | ||
throw new _common.AccountsError('Username is required'); | ||
case 2: | ||
if (!this.isImpersonated()) { | ||
_context5.next = 4; | ||
break; | ||
} | ||
throw new _common.AccountsError('User already impersonating'); | ||
case 4: | ||
_context5.next = 6; | ||
return this.tokens(); | ||
case 6: | ||
_ref6 = _context5.sent; | ||
accessToken = _ref6.accessToken; | ||
refreshToken = _ref6.refreshToken; | ||
if (accessToken) { | ||
_context5.next = 11; | ||
break; | ||
} | ||
throw new _common.AccountsError('There is no access tokens available'); | ||
case 11: | ||
_context5.next = 13; | ||
return this.transport.impersonate(accessToken, username); | ||
case 13: | ||
res = _context5.sent; | ||
if (res.authorized) { | ||
_context5.next = 18; | ||
break; | ||
} | ||
throw new _common.AccountsError('User unauthorized to impersonate ' + username); | ||
case 18: | ||
this.store.dispatch((0, _module.setImpersonated)(true)); | ||
this.store.dispatch((0, _module.setOriginalTokens)({ accessToken: accessToken, refreshToken: refreshToken })); | ||
_context5.next = 22; | ||
return this.storeTokens(res.tokens); | ||
case 22: | ||
this.store.dispatch((0, _module.setTokens)(res.tokens)); | ||
this.store.dispatch((0, _module.setUser)(res.user)); | ||
return _context5.abrupt('return', res); | ||
case 25: | ||
case 'end': | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee5, this); | ||
})); | ||
function impersonate(_x5) { | ||
return _ref5.apply(this, arguments); | ||
} | ||
return impersonate; | ||
}() | ||
}, { | ||
key: 'stopImpersonation', | ||
value: function () { | ||
var _ref7 = _asyncToGenerator(regeneratorRuntime.mark(function _callee6() { | ||
return regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
if (!this.isImpersonated()) { | ||
_context6.next = 6; | ||
break; | ||
} | ||
this.store.dispatch((0, _module.setTokens)(this.originalTokens())); | ||
this.store.dispatch((0, _module.clearOriginalTokens)()); | ||
this.store.dispatch((0, _module.setImpersonated)(false)); | ||
_context6.next = 6; | ||
return this.refreshSession(); | ||
case 6: | ||
case 'end': | ||
return _context6.stop(); | ||
} | ||
} | ||
}, _callee6, this); | ||
})); | ||
function stopImpersonation() { | ||
return _ref7.apply(this, arguments); | ||
} | ||
return stopImpersonation; | ||
}() | ||
}, { | ||
key: 'isImpersonated', | ||
value: function isImpersonated() { | ||
return this.getState().get('isImpersonated'); | ||
} | ||
}, { | ||
key: 'originalTokens', | ||
value: function originalTokens() { | ||
var tokens = this.getState().get('originalTokens'); | ||
return tokens ? tokens.toJS() : { | ||
accessToken: null, | ||
refreshToken: null | ||
}; | ||
} | ||
}, { | ||
key: 'tokens', | ||
value: function tokens() { | ||
var tokens = this.getState().get('tokens'); | ||
return tokens ? tokens.toJS() : { | ||
accessToken: null, | ||
refreshToken: null | ||
}; | ||
} | ||
}, { | ||
key: 'clearTokens', | ||
value: function () { | ||
var _ref8 = _asyncToGenerator(regeneratorRuntime.mark(function _callee7() { | ||
return regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
case 0: | ||
this.store.dispatch((0, _module.clearTokens)()); | ||
_context5.next = 3; | ||
_context7.next = 3; | ||
return this.removeStorageData(getTokenKey(ACCESS_TOKEN, this.options)); | ||
case 3: | ||
_context5.next = 5; | ||
_context7.next = 5; | ||
return this.removeStorageData(getTokenKey(REFRESH_TOKEN, this.options)); | ||
@@ -222,10 +364,10 @@ | ||
case 'end': | ||
return _context5.stop(); | ||
return _context7.stop(); | ||
} | ||
} | ||
}, _callee5, this); | ||
}, _callee7, this); | ||
})); | ||
function clearTokens() { | ||
return _ref5.apply(this, arguments); | ||
return _ref8.apply(this, arguments); | ||
} | ||
@@ -238,39 +380,44 @@ | ||
value: function () { | ||
var _ref6 = _asyncToGenerator(regeneratorRuntime.mark(function _callee6(loginResponse) { | ||
var _ref9 = _asyncToGenerator(regeneratorRuntime.mark(function _callee8(tokens) { | ||
var newAccessToken, newRefreshToken; | ||
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: | ||
newAccessToken = loginResponse.tokens.accessToken; | ||
if (!tokens) { | ||
_context8.next = 9; | ||
break; | ||
} | ||
newAccessToken = tokens.accessToken; | ||
if (!newAccessToken) { | ||
_context6.next = 4; | ||
_context8.next = 5; | ||
break; | ||
} | ||
_context6.next = 4; | ||
_context8.next = 5; | ||
return this.setStorageData(getTokenKey(ACCESS_TOKEN, this.options), newAccessToken); | ||
case 4: | ||
newRefreshToken = loginResponse.tokens.refreshToken; | ||
case 5: | ||
newRefreshToken = tokens.refreshToken; | ||
if (!newRefreshToken) { | ||
_context6.next = 8; | ||
_context8.next = 9; | ||
break; | ||
} | ||
_context6.next = 8; | ||
_context8.next = 9; | ||
return this.setStorageData(getTokenKey(REFRESH_TOKEN, this.options), newRefreshToken); | ||
case 8: | ||
case 9: | ||
case 'end': | ||
return _context6.stop(); | ||
return _context8.stop(); | ||
} | ||
} | ||
}, _callee6, this); | ||
}, _callee8, this); | ||
})); | ||
function storeTokens(_x5) { | ||
return _ref6.apply(this, arguments); | ||
function storeTokens(_x6) { | ||
return _ref9.apply(this, arguments); | ||
} | ||
@@ -294,23 +441,23 @@ | ||
value: function () { | ||
var _ref7 = _asyncToGenerator(regeneratorRuntime.mark(function _callee7() { | ||
var _ref8, accessToken, refreshToken, decodedRefreshToken, currentTime, refreshedSession; | ||
var _ref10 = _asyncToGenerator(regeneratorRuntime.mark(function _callee9() { | ||
var _ref11, accessToken, refreshToken, decodedRefreshToken, currentTime, refreshedSession; | ||
return regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
return regeneratorRuntime.wrap(function _callee9$(_context9) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
switch (_context9.prev = _context9.next) { | ||
case 0: | ||
_context7.next = 2; | ||
_context9.next = 2; | ||
return this.tokens(); | ||
case 2: | ||
_ref8 = _context7.sent; | ||
accessToken = _ref8.accessToken; | ||
refreshToken = _ref8.refreshToken; | ||
_ref11 = _context9.sent; | ||
accessToken = _ref11.accessToken; | ||
refreshToken = _ref11.refreshToken; | ||
if (!(accessToken && refreshToken)) { | ||
_context7.next = 33; | ||
_context9.next = 33; | ||
break; | ||
} | ||
_context7.prev = 6; | ||
_context9.prev = 6; | ||
@@ -323,3 +470,3 @@ this.store.dispatch((0, _module.loggingIn)(true)); | ||
if (!(decodedRefreshToken.exp < currentTime)) { | ||
_context7.next = 15; | ||
_context9.next = 15; | ||
break; | ||
@@ -330,16 +477,16 @@ } | ||
this.clearUser(); | ||
_context7.next = 23; | ||
_context9.next = 23; | ||
break; | ||
case 15: | ||
_context7.next = 17; | ||
_context9.next = 17; | ||
return this.transport.refreshTokens(accessToken, refreshToken); | ||
case 17: | ||
refreshedSession = _context7.sent; | ||
refreshedSession = _context9.sent; | ||
this.store.dispatch((0, _module.loggingIn)(false)); | ||
_context7.next = 21; | ||
return this.storeTokens(refreshedSession); | ||
_context9.next = 21; | ||
return this.storeTokens(refreshedSession.tokens); | ||
@@ -351,8 +498,8 @@ case 21: | ||
case 23: | ||
_context7.next = 31; | ||
_context9.next = 31; | ||
break; | ||
case 25: | ||
_context7.prev = 25; | ||
_context7.t0 = _context7['catch'](6); | ||
_context9.prev = 25; | ||
_context9.t0 = _context9['catch'](6); | ||
@@ -365,3 +512,3 @@ this.store.dispatch((0, _module.loggingIn)(false)); | ||
case 31: | ||
_context7.next = 36; | ||
_context9.next = 36; | ||
break; | ||
@@ -376,10 +523,10 @@ | ||
case 'end': | ||
return _context7.stop(); | ||
return _context9.stop(); | ||
} | ||
} | ||
}, _callee7, this, [[6, 25]]); | ||
}, _callee9, this, [[6, 25]]); | ||
})); | ||
function refreshSession() { | ||
return _ref7.apply(this, arguments); | ||
return _ref10.apply(this, arguments); | ||
} | ||
@@ -392,10 +539,10 @@ | ||
value: function () { | ||
var _ref9 = _asyncToGenerator(regeneratorRuntime.mark(function _callee8(user, callback) { | ||
var userId; | ||
return regeneratorRuntime.wrap(function _callee8$(_context8) { | ||
var _ref12 = _asyncToGenerator(regeneratorRuntime.mark(function _callee10(user, callback) { | ||
var hashAlgorithm, password, userToCreate, userId; | ||
return regeneratorRuntime.wrap(function _callee10$(_context10) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
switch (_context10.prev = _context10.next) { | ||
case 0: | ||
if (!(!user || user.password === undefined)) { | ||
_context8.next = 2; | ||
_context10.next = 2; | ||
break; | ||
@@ -410,4 +557,4 @@ } | ||
case 2: | ||
if (_common.validators.validatePassword(user.password)) { | ||
_context8.next = 4; | ||
if (!(!user.password || (0, _lodash.isString)(user.password) && !_common.validators.validatePassword(user.password))) { | ||
_context10.next = 4; | ||
break; | ||
@@ -420,3 +567,3 @@ } | ||
if (!(!_common.validators.validateUsername(user.username) && !_common.validators.validateEmail(user.email))) { | ||
_context8.next = 6; | ||
_context10.next = 6; | ||
break; | ||
@@ -428,8 +575,11 @@ } | ||
case 6: | ||
_context8.prev = 6; | ||
_context8.next = 9; | ||
return this.transport.createUser(user); | ||
hashAlgorithm = this.options.passwordHashAlgorithm; | ||
password = user.password && hashAlgorithm ? (0, _encryption.hashPassword)(user.password, hashAlgorithm) : user.password; | ||
userToCreate = _extends({}, user, { password: password }); | ||
_context10.prev = 9; | ||
_context10.next = 12; | ||
return this.transport.createUser(userToCreate); | ||
case 9: | ||
userId = _context8.sent; | ||
case 12: | ||
userId = _context10.sent; | ||
@@ -439,28 +589,29 @@ if (callback && (0, _lodash.isFunction)(callback)) { | ||
} | ||
_context8.next = 13; | ||
_context10.next = 16; | ||
return this.loginWithPassword({ id: userId }, user.password); | ||
case 13: | ||
_context8.next = 19; | ||
case 16: | ||
_context10.next = 22; | ||
break; | ||
case 15: | ||
_context8.prev = 15; | ||
_context8.t0 = _context8['catch'](6); | ||
case 18: | ||
_context10.prev = 18; | ||
_context10.t0 = _context10['catch'](9); | ||
if (callback && (0, _lodash.isFunction)(callback)) { | ||
callback(_context8.t0); | ||
callback(_context10.t0); | ||
} | ||
throw new _common.AccountsError(_context8.t0.message); | ||
throw new _common.AccountsError(_context10.t0.message); | ||
case 19: | ||
case 22: | ||
case 'end': | ||
return _context8.stop(); | ||
return _context10.stop(); | ||
} | ||
} | ||
}, _callee8, this, [[6, 15]]); | ||
}, _callee10, this, [[9, 18]]); | ||
})); | ||
function createUser(_x6, _x7) { | ||
return _ref9.apply(this, arguments); | ||
function createUser(_x7, _x8) { | ||
return _ref12.apply(this, arguments); | ||
} | ||
@@ -473,10 +624,10 @@ | ||
value: function () { | ||
var _ref10 = _asyncToGenerator(regeneratorRuntime.mark(function _callee9(user, password, callback) { | ||
var _ref13 = _asyncToGenerator(regeneratorRuntime.mark(function _callee11(user, password, callback) { | ||
var hashAlgorithm, pass, res; | ||
return regeneratorRuntime.wrap(function _callee9$(_context9) { | ||
return regeneratorRuntime.wrap(function _callee11$(_context11) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
switch (_context11.prev = _context11.next) { | ||
case 0: | ||
if (!(!password || !user)) { | ||
_context9.next = 2; | ||
_context11.next = 2; | ||
break; | ||
@@ -489,3 +640,3 @@ } | ||
if (!(!(0, _lodash.isString)(user) && !isValidUserObject(user) || !(0, _lodash.isString)(password))) { | ||
_context9.next = 4; | ||
_context11.next = 4; | ||
break; | ||
@@ -499,15 +650,15 @@ } | ||
this.store.dispatch((0, _module.loggingIn)(true)); | ||
_context9.prev = 5; | ||
_context11.prev = 5; | ||
hashAlgorithm = this.options.passwordHashAlgorithm; | ||
pass = hashAlgorithm ? (0, _encryption.hashPassword)(password, hashAlgorithm) : password; | ||
_context9.next = 10; | ||
_context11.next = 10; | ||
return this.transport.loginWithPassword(user, pass); | ||
case 10: | ||
res = _context9.sent; | ||
res = _context11.sent; | ||
this.store.dispatch((0, _module.loggingIn)(false)); | ||
_context9.next = 14; | ||
return this.storeTokens(res); | ||
_context11.next = 14; | ||
return this.storeTokens(res.tokens); | ||
@@ -526,24 +677,24 @@ case 14: | ||
return _context9.abrupt('return', res); | ||
return _context11.abrupt('return', res); | ||
case 21: | ||
_context9.prev = 21; | ||
_context9.t0 = _context9['catch'](5); | ||
_context11.prev = 21; | ||
_context11.t0 = _context11['catch'](5); | ||
this.store.dispatch((0, _module.loggingIn)(false)); | ||
if (callback && (0, _lodash.isFunction)(callback)) { | ||
callback(_context9.t0, null); | ||
callback(_context11.t0, null); | ||
} | ||
throw new _common.AccountsError(_context9.t0.message); | ||
throw new _common.AccountsError(_context11.t0.message); | ||
case 26: | ||
case 'end': | ||
return _context9.stop(); | ||
return _context11.stop(); | ||
} | ||
} | ||
}, _callee9, this, [[5, 21]]); | ||
}, _callee11, this, [[5, 21]]); | ||
})); | ||
function loginWithPassword(_x8, _x9, _x10) { | ||
return _ref10.apply(this, arguments); | ||
function loginWithPassword(_x9, _x10, _x11) { | ||
return _ref13.apply(this, arguments); | ||
} | ||
@@ -566,23 +717,23 @@ | ||
value: function () { | ||
var _ref11 = _asyncToGenerator(regeneratorRuntime.mark(function _callee10(callback) { | ||
var _ref12, accessToken; | ||
var _ref14 = _asyncToGenerator(regeneratorRuntime.mark(function _callee12(callback) { | ||
var _ref15, accessToken; | ||
return regeneratorRuntime.wrap(function _callee10$(_context10) { | ||
return regeneratorRuntime.wrap(function _callee12$(_context12) { | ||
while (1) { | ||
switch (_context10.prev = _context10.next) { | ||
switch (_context12.prev = _context12.next) { | ||
case 0: | ||
_context10.prev = 0; | ||
_context10.next = 3; | ||
_context12.prev = 0; | ||
_context12.next = 3; | ||
return this.tokens(); | ||
case 3: | ||
_ref12 = _context10.sent; | ||
accessToken = _ref12.accessToken; | ||
_ref15 = _context12.sent; | ||
accessToken = _ref15.accessToken; | ||
if (!accessToken) { | ||
_context10.next = 8; | ||
_context12.next = 8; | ||
break; | ||
} | ||
_context10.next = 8; | ||
_context12.next = 8; | ||
return this.transport.logout(accessToken); | ||
@@ -601,24 +752,26 @@ | ||
} | ||
_context10.next = 18; | ||
_context12.next = 20; | ||
break; | ||
case 14: | ||
_context10.prev = 14; | ||
_context10.t0 = _context10['catch'](0); | ||
_context12.prev = 14; | ||
_context12.t0 = _context12['catch'](0); | ||
this.clearTokens(); | ||
this.store.dispatch((0, _module.clearUser)()); | ||
if (callback && (0, _lodash.isFunction)(callback)) { | ||
callback(_context10.t0); | ||
callback(_context12.t0); | ||
} | ||
throw new _common.AccountsError(_context10.t0.message); | ||
throw new _common.AccountsError(_context12.t0.message); | ||
case 18: | ||
case 20: | ||
case 'end': | ||
return _context10.stop(); | ||
return _context12.stop(); | ||
} | ||
} | ||
}, _callee10, this, [[0, 14]]); | ||
}, _callee12, this, [[0, 14]]); | ||
})); | ||
function logout(_x11) { | ||
return _ref11.apply(this, arguments); | ||
function logout(_x12) { | ||
return _ref14.apply(this, arguments); | ||
} | ||
@@ -631,30 +784,30 @@ | ||
value: function () { | ||
var _ref13 = _asyncToGenerator(regeneratorRuntime.mark(function _callee11(token) { | ||
return regeneratorRuntime.wrap(function _callee11$(_context11) { | ||
var _ref16 = _asyncToGenerator(regeneratorRuntime.mark(function _callee13(token) { | ||
return regeneratorRuntime.wrap(function _callee13$(_context13) { | ||
while (1) { | ||
switch (_context11.prev = _context11.next) { | ||
switch (_context13.prev = _context13.next) { | ||
case 0: | ||
_context11.prev = 0; | ||
_context11.next = 3; | ||
_context13.prev = 0; | ||
_context13.next = 3; | ||
return this.transport.verifyEmail(token); | ||
case 3: | ||
_context11.next = 8; | ||
_context13.next = 8; | ||
break; | ||
case 5: | ||
_context11.prev = 5; | ||
_context11.t0 = _context11['catch'](0); | ||
throw new _common.AccountsError(_context11.t0.message); | ||
_context13.prev = 5; | ||
_context13.t0 = _context13['catch'](0); | ||
throw new _common.AccountsError(_context13.t0.message); | ||
case 8: | ||
case 'end': | ||
return _context11.stop(); | ||
return _context13.stop(); | ||
} | ||
} | ||
}, _callee11, this, [[0, 5]]); | ||
}, _callee13, this, [[0, 5]]); | ||
})); | ||
function verifyEmail(_x12) { | ||
return _ref13.apply(this, arguments); | ||
function verifyEmail(_x13) { | ||
return _ref16.apply(this, arguments); | ||
} | ||
@@ -667,30 +820,41 @@ | ||
value: function () { | ||
var _ref14 = _asyncToGenerator(regeneratorRuntime.mark(function _callee12(token, newPassword) { | ||
return regeneratorRuntime.wrap(function _callee12$(_context12) { | ||
var _ref17 = _asyncToGenerator(regeneratorRuntime.mark(function _callee14(token, newPassword) { | ||
var hashAlgorithm, password; | ||
return regeneratorRuntime.wrap(function _callee14$(_context14) { | ||
while (1) { | ||
switch (_context12.prev = _context12.next) { | ||
switch (_context14.prev = _context14.next) { | ||
case 0: | ||
_context12.prev = 0; | ||
_context12.next = 3; | ||
return this.transport.resetPassword(token, newPassword); | ||
if (_common.validators.validatePassword(newPassword)) { | ||
_context14.next = 2; | ||
break; | ||
} | ||
case 3: | ||
_context12.next = 8; | ||
throw new _common.AccountsError('Password is invalid!'); | ||
case 2: | ||
hashAlgorithm = this.options.passwordHashAlgorithm; | ||
password = hashAlgorithm ? (0, _encryption.hashPassword)(newPassword, hashAlgorithm) : newPassword; | ||
_context14.prev = 4; | ||
_context14.next = 7; | ||
return this.transport.resetPassword(token, password); | ||
case 7: | ||
_context14.next = 12; | ||
break; | ||
case 5: | ||
_context12.prev = 5; | ||
_context12.t0 = _context12['catch'](0); | ||
throw new _common.AccountsError(_context12.t0.message); | ||
case 9: | ||
_context14.prev = 9; | ||
_context14.t0 = _context14['catch'](4); | ||
throw new _common.AccountsError(_context14.t0.message); | ||
case 8: | ||
case 12: | ||
case 'end': | ||
return _context12.stop(); | ||
return _context14.stop(); | ||
} | ||
} | ||
}, _callee12, this, [[0, 5]]); | ||
}, _callee14, this, [[4, 9]]); | ||
})); | ||
function resetPassword(_x13, _x14) { | ||
return _ref14.apply(this, arguments); | ||
function resetPassword(_x14, _x15) { | ||
return _ref17.apply(this, arguments); | ||
} | ||
@@ -703,9 +867,9 @@ | ||
value: function () { | ||
var _ref15 = _asyncToGenerator(regeneratorRuntime.mark(function _callee13(email) { | ||
return regeneratorRuntime.wrap(function _callee13$(_context13) { | ||
var _ref18 = _asyncToGenerator(regeneratorRuntime.mark(function _callee15(email) { | ||
return regeneratorRuntime.wrap(function _callee15$(_context15) { | ||
while (1) { | ||
switch (_context13.prev = _context13.next) { | ||
switch (_context15.prev = _context15.next) { | ||
case 0: | ||
if (_common.validators.validateEmail(email)) { | ||
_context13.next = 2; | ||
_context15.next = 2; | ||
break; | ||
@@ -717,25 +881,25 @@ } | ||
case 2: | ||
_context13.prev = 2; | ||
_context13.next = 5; | ||
_context15.prev = 2; | ||
_context15.next = 5; | ||
return this.transport.sendResetPasswordEmail(email); | ||
case 5: | ||
_context13.next = 10; | ||
_context15.next = 10; | ||
break; | ||
case 7: | ||
_context13.prev = 7; | ||
_context13.t0 = _context13['catch'](2); | ||
throw new _common.AccountsError(_context13.t0.message); | ||
_context15.prev = 7; | ||
_context15.t0 = _context15['catch'](2); | ||
throw new _common.AccountsError(_context15.t0.message); | ||
case 10: | ||
case 'end': | ||
return _context13.stop(); | ||
return _context15.stop(); | ||
} | ||
} | ||
}, _callee13, this, [[2, 7]]); | ||
}, _callee15, this, [[2, 7]]); | ||
})); | ||
function requestPasswordReset(_x15) { | ||
return _ref15.apply(this, arguments); | ||
function requestPasswordReset(_x16) { | ||
return _ref18.apply(this, arguments); | ||
} | ||
@@ -748,9 +912,9 @@ | ||
value: function () { | ||
var _ref16 = _asyncToGenerator(regeneratorRuntime.mark(function _callee14(email) { | ||
return regeneratorRuntime.wrap(function _callee14$(_context14) { | ||
var _ref19 = _asyncToGenerator(regeneratorRuntime.mark(function _callee16(email) { | ||
return regeneratorRuntime.wrap(function _callee16$(_context16) { | ||
while (1) { | ||
switch (_context14.prev = _context14.next) { | ||
switch (_context16.prev = _context16.next) { | ||
case 0: | ||
if (_common.validators.validateEmail(email)) { | ||
_context14.next = 2; | ||
_context16.next = 2; | ||
break; | ||
@@ -762,25 +926,25 @@ } | ||
case 2: | ||
_context14.prev = 2; | ||
_context14.next = 5; | ||
_context16.prev = 2; | ||
_context16.next = 5; | ||
return this.transport.sendVerificationEmail(email); | ||
case 5: | ||
_context14.next = 10; | ||
_context16.next = 10; | ||
break; | ||
case 7: | ||
_context14.prev = 7; | ||
_context14.t0 = _context14['catch'](2); | ||
throw new _common.AccountsError(_context14.t0.message); | ||
_context16.prev = 7; | ||
_context16.t0 = _context16['catch'](2); | ||
throw new _common.AccountsError(_context16.t0.message); | ||
case 10: | ||
case 'end': | ||
return _context14.stop(); | ||
return _context16.stop(); | ||
} | ||
} | ||
}, _callee14, this, [[2, 7]]); | ||
}, _callee16, this, [[2, 7]]); | ||
})); | ||
function requestVerificationEmail(_x16) { | ||
return _ref16.apply(this, arguments); | ||
function requestVerificationEmail(_x17) { | ||
return _ref19.apply(this, arguments); | ||
} | ||
@@ -799,3 +963,22 @@ | ||
config: function config(options, transport) { | ||
this.instance = new AccountsClient(_extends({}, _config3.default, options), transport); | ||
var _this = this; | ||
return _asyncToGenerator(regeneratorRuntime.mark(function _callee17() { | ||
return regeneratorRuntime.wrap(function _callee17$(_context17) { | ||
while (1) { | ||
switch (_context17.prev = _context17.next) { | ||
case 0: | ||
_this.instance = new AccountsClient(_extends({}, _config3.default, options), transport); | ||
return _context17.abrupt('return', _this.instance.loadTokensFromStorage().then(function () { | ||
return _this.instance; | ||
})); | ||
case 2: | ||
case 'end': | ||
return _context17.stop(); | ||
} | ||
} | ||
}, _callee17, _this); | ||
}))(); | ||
}, | ||
@@ -843,2 +1026,14 @@ user: function user() { | ||
return this.instance.requestVerificationEmail(email); | ||
}, | ||
impersonate: function impersonate(username) { | ||
return this.instance.impersonate(username); | ||
}, | ||
stopImpersonation: function stopImpersonation() { | ||
return this.instance.stopImpersonation(); | ||
}, | ||
isImpersonated: function isImpersonated() { | ||
return this.instance.isImpersonated(); | ||
}, | ||
originalTokens: function originalTokens() { | ||
return this.instance.originalTokens(); | ||
} | ||
@@ -845,0 +1040,0 @@ }; |
@@ -50,14 +50,65 @@ 'use strict'; | ||
describe('config', function () { | ||
it('requires a transport', function () { | ||
try { | ||
_AccountsClient2.default.config({ | ||
history: history | ||
}); | ||
throw new Error(); | ||
} catch (err) { | ||
var message = err.message; | ||
it('requires a transport', _asyncToGenerator(regeneratorRuntime.mark(function _callee() { | ||
var message; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.prev = 0; | ||
_context.next = 3; | ||
return _AccountsClient2.default.config({ | ||
history: history | ||
}); | ||
expect(message).toEqual('A REST or GraphQL transport is required'); | ||
} | ||
}); | ||
case 3: | ||
throw new Error(); | ||
case 6: | ||
_context.prev = 6; | ||
_context.t0 = _context['catch'](0); | ||
message = _context.t0.message; | ||
expect(message).toEqual('A REST or GraphQL transport is required'); | ||
case 10: | ||
case 'end': | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, undefined, [[0, 6]]); | ||
}))); | ||
it('should eagerly load tokens from storage after using config', _asyncToGenerator(regeneratorRuntime.mark(function _callee2() { | ||
var transport, tokens; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
transport = { | ||
loginWithPassword: function loginWithPassword() { | ||
return Promise.resolve(loggedInUser); | ||
} | ||
}; | ||
_context2.next = 3; | ||
return _AccountsClient2.default.config({ | ||
history: history, | ||
tokenStorage: { | ||
getItem: function getItem() { | ||
return Promise.resolve('testValue'); | ||
} | ||
} | ||
}, transport); | ||
case 3: | ||
tokens = _AccountsClient2.default.tokens(); | ||
expect(tokens.accessToken).toBeDefined(); | ||
expect(tokens.refreshToken).toBeDefined(); | ||
case 6: | ||
case 'end': | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, undefined); | ||
}))); | ||
it('sets the transport', function () { | ||
@@ -72,7 +123,7 @@ var transport = {}; | ||
describe('createUser', function () { | ||
it('requires user object', _asyncToGenerator(regeneratorRuntime.mark(function _callee() { | ||
it('requires user object', _asyncToGenerator(regeneratorRuntime.mark(function _callee3() { | ||
var message; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
@@ -84,4 +135,4 @@ _AccountsClient2.default.config({ | ||
}); | ||
_context.prev = 1; | ||
_context.next = 4; | ||
_context3.prev = 1; | ||
_context3.next = 4; | ||
return _AccountsClient2.default.createUser(); | ||
@@ -93,5 +144,5 @@ | ||
case 7: | ||
_context.prev = 7; | ||
_context.t0 = _context['catch'](1); | ||
message = _context.t0.message; | ||
_context3.prev = 7; | ||
_context3.t0 = _context3['catch'](1); | ||
message = _context3.t0.message; | ||
@@ -102,14 +153,15 @@ expect(message).toEqual('Unrecognized options for create user request'); | ||
case 'end': | ||
return _context.stop(); | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee, undefined, [[1, 7]]); | ||
}, _callee3, undefined, [[1, 7]]); | ||
}))); | ||
it('requires password', _asyncToGenerator(regeneratorRuntime.mark(function _callee2() { | ||
it('requires password', _asyncToGenerator(regeneratorRuntime.mark(function _callee4() { | ||
var message; | ||
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: | ||
_AccountsClient2.default.config({ | ||
_context4.next = 2; | ||
return _AccountsClient2.default.config({ | ||
history: history | ||
@@ -119,4 +171,6 @@ }, { | ||
}); | ||
_context2.prev = 1; | ||
_context2.next = 4; | ||
case 2: | ||
_context4.prev = 2; | ||
_context4.next = 5; | ||
return _AccountsClient2.default.createUser({ | ||
@@ -126,24 +180,24 @@ password: null | ||
case 4: | ||
case 5: | ||
throw new Error(); | ||
case 7: | ||
_context2.prev = 7; | ||
_context2.t0 = _context2['catch'](1); | ||
message = _context2.t0.message; | ||
case 8: | ||
_context4.prev = 8; | ||
_context4.t0 = _context4['catch'](2); | ||
message = _context4.t0.message; | ||
expect(message).toEqual('Password is required'); | ||
case 11: | ||
case 12: | ||
case 'end': | ||
return _context2.stop(); | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee2, undefined, [[1, 7]]); | ||
}, _callee4, undefined, [[2, 8]]); | ||
}))); | ||
it('requires username or an email', _asyncToGenerator(regeneratorRuntime.mark(function _callee3() { | ||
it('requires username or an email', _asyncToGenerator(regeneratorRuntime.mark(function _callee5() { | ||
var message; | ||
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: | ||
@@ -153,4 +207,4 @@ _AccountsClient2.default.config({ history: history }, { | ||
}); | ||
_context3.prev = 1; | ||
_context3.next = 4; | ||
_context5.prev = 1; | ||
_context5.next = 4; | ||
return _AccountsClient2.default.createUser({ | ||
@@ -166,5 +220,5 @@ password: '123456', | ||
case 7: | ||
_context3.prev = 7; | ||
_context3.t0 = _context3['catch'](1); | ||
message = _context3.t0.message; | ||
_context5.prev = 7; | ||
_context5.t0 = _context5['catch'](1); | ||
message = _context5.t0.message; | ||
@@ -175,12 +229,12 @@ expect(message).toEqual('Username or Email is required'); | ||
case 'end': | ||
return _context3.stop(); | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee3, undefined, [[1, 7]]); | ||
}, _callee5, undefined, [[1, 7]]); | ||
}))); | ||
it('calls callback on succesfull user creation', _asyncToGenerator(regeneratorRuntime.mark(function _callee4() { | ||
it('calls callback on succesfull user creation', _asyncToGenerator(regeneratorRuntime.mark(function _callee6() { | ||
var callback, transport; | ||
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: | ||
@@ -196,5 +250,7 @@ callback = jest.fn(); | ||
}; | ||
_context6.next = 4; | ||
return _AccountsClient2.default.config({ history: history }, transport); | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
_context4.next = 5; | ||
case 4: | ||
_context6.next = 6; | ||
return _AccountsClient2.default.createUser({ | ||
@@ -205,18 +261,18 @@ password: '123456', | ||
case 5: | ||
case 6: | ||
expect(callback.mock.calls.length).toEqual(1); | ||
case 6: | ||
case 7: | ||
case 'end': | ||
return _context4.stop(); | ||
return _context6.stop(); | ||
} | ||
} | ||
}, _callee4, undefined); | ||
}, _callee6, undefined); | ||
}))); | ||
it('calls callback on failure with error message', _asyncToGenerator(regeneratorRuntime.mark(function _callee5() { | ||
it('calls callback on failure with error message', _asyncToGenerator(regeneratorRuntime.mark(function _callee7() { | ||
var transport, callback; | ||
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: | ||
@@ -228,9 +284,9 @@ transport = { | ||
}; | ||
_context7.next = 3; | ||
return _AccountsClient2.default.config({ history: history }, transport); | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
case 3: | ||
callback = jest.fn(); | ||
_context5.prev = 3; | ||
_context5.next = 6; | ||
_context7.prev = 4; | ||
_context7.next = 7; | ||
return _AccountsClient2.default.createUser({ | ||
@@ -241,8 +297,8 @@ password: '123456', | ||
case 6: | ||
case 7: | ||
throw new Error(); | ||
case 9: | ||
_context5.prev = 9; | ||
_context5.t0 = _context5['catch'](3); | ||
case 10: | ||
_context7.prev = 10; | ||
_context7.t0 = _context7['catch'](4); | ||
@@ -252,14 +308,14 @@ expect(callback.mock.calls.length).toEqual(1); | ||
case 13: | ||
case 14: | ||
case 'end': | ||
return _context5.stop(); | ||
return _context7.stop(); | ||
} | ||
} | ||
}, _callee5, undefined, [[3, 9]]); | ||
}, _callee7, undefined, [[4, 10]]); | ||
}))); | ||
it('calls login function with user id and password of created user', _asyncToGenerator(regeneratorRuntime.mark(function _callee6() { | ||
it('calls login function with user id and password of created user', _asyncToGenerator(regeneratorRuntime.mark(function _callee8() { | ||
var transport; | ||
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: | ||
@@ -278,3 +334,3 @@ transport = { | ||
_context6.next = 5; | ||
_context8.next = 5; | ||
return _AccountsClient2.default.createUser({ | ||
@@ -292,14 +348,14 @@ password: '123456', | ||
case 'end': | ||
return _context6.stop(); | ||
return _context8.stop(); | ||
} | ||
} | ||
}, _callee6, undefined); | ||
}, _callee8, undefined); | ||
}))); | ||
}); | ||
describe('loginWithPassword', function () { | ||
it('throws error if password is undefined', _asyncToGenerator(regeneratorRuntime.mark(function _callee7() { | ||
it('throws error if password is undefined', _asyncToGenerator(regeneratorRuntime.mark(function _callee9() { | ||
var transport, message; | ||
return regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
return regeneratorRuntime.wrap(function _callee9$(_context9) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
switch (_context9.prev = _context9.next) { | ||
case 0: | ||
@@ -313,4 +369,4 @@ transport = { | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
_context7.prev = 2; | ||
_context7.next = 5; | ||
_context9.prev = 2; | ||
_context9.next = 5; | ||
return _AccountsClient2.default.loginWithPassword(); | ||
@@ -322,5 +378,5 @@ | ||
case 8: | ||
_context7.prev = 8; | ||
_context7.t0 = _context7['catch'](2); | ||
message = _context7.t0.message; | ||
_context9.prev = 8; | ||
_context9.t0 = _context9['catch'](2); | ||
message = _context9.t0.message; | ||
@@ -331,12 +387,12 @@ expect(message).toEqual('Unrecognized options for login request'); | ||
case 'end': | ||
return _context7.stop(); | ||
return _context9.stop(); | ||
} | ||
} | ||
}, _callee7, undefined, [[2, 8]]); | ||
}, _callee9, undefined, [[2, 8]]); | ||
}))); | ||
it('throws error if user is undefined', _asyncToGenerator(regeneratorRuntime.mark(function _callee8() { | ||
it('throws error if user is undefined', _asyncToGenerator(regeneratorRuntime.mark(function _callee10() { | ||
var transport, message; | ||
return regeneratorRuntime.wrap(function _callee8$(_context8) { | ||
return regeneratorRuntime.wrap(function _callee10$(_context10) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
switch (_context10.prev = _context10.next) { | ||
case 0: | ||
@@ -350,4 +406,4 @@ transport = { | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
_context8.prev = 2; | ||
_context8.next = 5; | ||
_context10.prev = 2; | ||
_context10.next = 5; | ||
return _AccountsClient2.default.loginWithPassword(); | ||
@@ -359,5 +415,5 @@ | ||
case 8: | ||
_context8.prev = 8; | ||
_context8.t0 = _context8['catch'](2); | ||
message = _context8.t0.message; | ||
_context10.prev = 8; | ||
_context10.t0 = _context10['catch'](2); | ||
message = _context10.t0.message; | ||
@@ -368,12 +424,12 @@ expect(message).toEqual('Unrecognized options for login request'); | ||
case 'end': | ||
return _context8.stop(); | ||
return _context10.stop(); | ||
} | ||
} | ||
}, _callee8, undefined, [[2, 8]]); | ||
}, _callee10, undefined, [[2, 8]]); | ||
}))); | ||
it('throws error user is not a string or is an empty object', _asyncToGenerator(regeneratorRuntime.mark(function _callee9() { | ||
it('throws error user is not a string or is an empty object', _asyncToGenerator(regeneratorRuntime.mark(function _callee11() { | ||
var transport, message; | ||
return regeneratorRuntime.wrap(function _callee9$(_context9) { | ||
return regeneratorRuntime.wrap(function _callee11$(_context11) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
switch (_context11.prev = _context11.next) { | ||
case 0: | ||
@@ -387,4 +443,4 @@ transport = { | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
_context9.prev = 2; | ||
_context9.next = 5; | ||
_context11.prev = 2; | ||
_context11.next = 5; | ||
return _AccountsClient2.default.loginWithPassword({}, 'password'); | ||
@@ -396,5 +452,5 @@ | ||
case 8: | ||
_context9.prev = 8; | ||
_context9.t0 = _context9['catch'](2); | ||
message = _context9.t0.message; | ||
_context11.prev = 8; | ||
_context11.t0 = _context11['catch'](2); | ||
message = _context11.t0.message; | ||
@@ -405,12 +461,12 @@ expect(message).toEqual('Match failed'); | ||
case 'end': | ||
return _context9.stop(); | ||
return _context11.stop(); | ||
} | ||
} | ||
}, _callee9, undefined, [[2, 8]]); | ||
}, _callee11, undefined, [[2, 8]]); | ||
}))); | ||
it('throws error password is not a string', _asyncToGenerator(regeneratorRuntime.mark(function _callee10() { | ||
it('throws error password is not a string', _asyncToGenerator(regeneratorRuntime.mark(function _callee12() { | ||
var transport, message; | ||
return regeneratorRuntime.wrap(function _callee10$(_context10) { | ||
return regeneratorRuntime.wrap(function _callee12$(_context12) { | ||
while (1) { | ||
switch (_context10.prev = _context10.next) { | ||
switch (_context12.prev = _context12.next) { | ||
case 0: | ||
@@ -424,4 +480,4 @@ transport = { | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
_context10.prev = 2; | ||
_context10.next = 5; | ||
_context12.prev = 2; | ||
_context12.next = 5; | ||
return _AccountsClient2.default.loginWithPassword({ user: 'username' }, {}); | ||
@@ -433,5 +489,5 @@ | ||
case 8: | ||
_context10.prev = 8; | ||
_context10.t0 = _context10['catch'](2); | ||
message = _context10.t0.message; | ||
_context12.prev = 8; | ||
_context12.t0 = _context12['catch'](2); | ||
message = _context12.t0.message; | ||
@@ -442,12 +498,12 @@ expect(message).toEqual('Match failed'); | ||
case 'end': | ||
return _context10.stop(); | ||
return _context12.stop(); | ||
} | ||
} | ||
}, _callee10, undefined, [[2, 8]]); | ||
}, _callee12, undefined, [[2, 8]]); | ||
}))); | ||
it('calls callback on successful login', _asyncToGenerator(regeneratorRuntime.mark(function _callee11() { | ||
it('calls callback on successful login', _asyncToGenerator(regeneratorRuntime.mark(function _callee13() { | ||
var transport, callback; | ||
return regeneratorRuntime.wrap(function _callee11$(_context11) { | ||
return regeneratorRuntime.wrap(function _callee13$(_context13) { | ||
while (1) { | ||
switch (_context11.prev = _context11.next) { | ||
switch (_context13.prev = _context13.next) { | ||
case 0: | ||
@@ -462,3 +518,3 @@ transport = { | ||
callback = jest.fn(); | ||
_context11.next = 5; | ||
_context13.next = 5; | ||
return _AccountsClient2.default.loginWithPassword('username', 'password', callback); | ||
@@ -472,12 +528,12 @@ | ||
case 'end': | ||
return _context11.stop(); | ||
return _context13.stop(); | ||
} | ||
} | ||
}, _callee11, undefined); | ||
}, _callee13, undefined); | ||
}))); | ||
it('calls transport', _asyncToGenerator(regeneratorRuntime.mark(function _callee12() { | ||
it('calls transport', _asyncToGenerator(regeneratorRuntime.mark(function _callee14() { | ||
var loginWithPassword, transport; | ||
return regeneratorRuntime.wrap(function _callee12$(_context12) { | ||
return regeneratorRuntime.wrap(function _callee14$(_context14) { | ||
while (1) { | ||
switch (_context12.prev = _context12.next) { | ||
switch (_context14.prev = _context14.next) { | ||
case 0: | ||
@@ -492,3 +548,3 @@ loginWithPassword = jest.fn(function () { | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
_context12.next = 5; | ||
_context14.next = 5; | ||
return _AccountsClient2.default.loginWithPassword('username', 'password'); | ||
@@ -503,12 +559,12 @@ | ||
case 'end': | ||
return _context12.stop(); | ||
return _context14.stop(); | ||
} | ||
} | ||
}, _callee12, undefined); | ||
}, _callee14, undefined); | ||
}))); | ||
it('calls callback with error on failed login', _asyncToGenerator(regeneratorRuntime.mark(function _callee13() { | ||
it('calls callback with error on failed login', _asyncToGenerator(regeneratorRuntime.mark(function _callee15() { | ||
var transport, callback; | ||
return regeneratorRuntime.wrap(function _callee13$(_context13) { | ||
return regeneratorRuntime.wrap(function _callee15$(_context15) { | ||
while (1) { | ||
switch (_context13.prev = _context13.next) { | ||
switch (_context15.prev = _context15.next) { | ||
case 0: | ||
@@ -523,4 +579,4 @@ transport = { | ||
callback = jest.fn(); | ||
_context13.prev = 3; | ||
_context13.next = 6; | ||
_context15.prev = 3; | ||
_context15.next = 6; | ||
return _AccountsClient2.default.loginWithPassword('username', 'password', callback); | ||
@@ -532,4 +588,4 @@ | ||
case 9: | ||
_context13.prev = 9; | ||
_context13.t0 = _context13['catch'](3); | ||
_context15.prev = 9; | ||
_context15.t0 = _context15['catch'](3); | ||
@@ -541,12 +597,12 @@ expect(callback.mock.calls.length).toEqual(1); | ||
case 'end': | ||
return _context13.stop(); | ||
return _context15.stop(); | ||
} | ||
} | ||
}, _callee13, undefined, [[3, 9]]); | ||
}, _callee15, undefined, [[3, 9]]); | ||
}))); | ||
it('sets loggingIn flag to false on failed login', _asyncToGenerator(regeneratorRuntime.mark(function _callee14() { | ||
it('sets loggingIn flag to false on failed login', _asyncToGenerator(regeneratorRuntime.mark(function _callee16() { | ||
var transport; | ||
return regeneratorRuntime.wrap(function _callee14$(_context14) { | ||
return regeneratorRuntime.wrap(function _callee16$(_context16) { | ||
while (1) { | ||
switch (_context14.prev = _context14.next) { | ||
switch (_context16.prev = _context16.next) { | ||
case 0: | ||
@@ -560,4 +616,4 @@ transport = { | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
_context14.prev = 2; | ||
_context14.next = 5; | ||
_context16.prev = 2; | ||
_context16.next = 5; | ||
return _AccountsClient2.default.loginWithPassword('username', 'password'); | ||
@@ -569,4 +625,4 @@ | ||
case 8: | ||
_context14.prev = 8; | ||
_context14.t0 = _context14['catch'](2); | ||
_context16.prev = 8; | ||
_context16.t0 = _context16['catch'](2); | ||
@@ -577,12 +633,12 @@ expect(_AccountsClient2.default.loggingIn()).toBe(false); | ||
case 'end': | ||
return _context14.stop(); | ||
return _context16.stop(); | ||
} | ||
} | ||
}, _callee14, undefined, [[2, 8]]); | ||
}, _callee16, undefined, [[2, 8]]); | ||
}))); | ||
it('stores tokens in local storage', _asyncToGenerator(regeneratorRuntime.mark(function _callee15() { | ||
it('stores tokens in local storage', _asyncToGenerator(regeneratorRuntime.mark(function _callee17() { | ||
var transport; | ||
return regeneratorRuntime.wrap(function _callee15$(_context15) { | ||
return regeneratorRuntime.wrap(function _callee17$(_context17) { | ||
while (1) { | ||
switch (_context15.prev = _context15.next) { | ||
switch (_context17.prev = _context17.next) { | ||
case 0: | ||
@@ -596,3 +652,3 @@ transport = { | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
_context15.next = 4; | ||
_context17.next = 4; | ||
return _AccountsClient2.default.loginWithPassword('username', 'password'); | ||
@@ -606,13 +662,13 @@ | ||
case 'end': | ||
return _context15.stop(); | ||
return _context17.stop(); | ||
} | ||
} | ||
}, _callee15, undefined); | ||
}, _callee17, undefined); | ||
}))); | ||
it('fetch tokens from storage when storage is async', _asyncToGenerator(regeneratorRuntime.mark(function _callee16() { | ||
it('should return tokens in a sync return value', _asyncToGenerator(regeneratorRuntime.mark(function _callee18() { | ||
var transport, tokens; | ||
return regeneratorRuntime.wrap(function _callee16$(_context16) { | ||
return regeneratorRuntime.wrap(function _callee18$(_context18) { | ||
while (1) { | ||
switch (_context16.prev = _context16.next) { | ||
switch (_context18.prev = _context18.next) { | ||
case 0: | ||
@@ -624,5 +680,4 @@ transport = { | ||
}; | ||
_AccountsClient2.default.config({ | ||
_context18.next = 3; | ||
return _AccountsClient2.default.config({ | ||
history: history, | ||
@@ -636,63 +691,20 @@ tokenStorage: { | ||
_context16.next = 4; | ||
return _AccountsClient2.default.tokens(); | ||
case 3: | ||
tokens = _AccountsClient2.default.tokens(); | ||
case 4: | ||
tokens = _context16.sent; | ||
expect(tokens.accessToken).toBe('testValue'); | ||
expect(tokens.refreshToken).toBe('testValue'); | ||
case 7: | ||
case 6: | ||
case 'end': | ||
return _context16.stop(); | ||
return _context18.stop(); | ||
} | ||
} | ||
}, _callee16, undefined); | ||
}, _callee18, undefined); | ||
}))); | ||
it('fetch tokens from storage when storage is sync', _asyncToGenerator(regeneratorRuntime.mark(function _callee17() { | ||
var transport, tokens; | ||
return regeneratorRuntime.wrap(function _callee17$(_context17) { | ||
while (1) { | ||
switch (_context17.prev = _context17.next) { | ||
case 0: | ||
transport = { | ||
loginWithPassword: function loginWithPassword() { | ||
return Promise.resolve(loggedInUser); | ||
} | ||
}; | ||
_AccountsClient2.default.config({ | ||
history: history, | ||
tokenStorage: { | ||
getItem: function getItem() { | ||
return 'testValue'; | ||
} | ||
} | ||
}, transport); | ||
_context17.next = 4; | ||
return _AccountsClient2.default.tokens(); | ||
case 4: | ||
tokens = _context17.sent; | ||
expect(tokens.accessToken).toBe('testValue'); | ||
expect(tokens.refreshToken).toBe('testValue'); | ||
case 7: | ||
case 'end': | ||
return _context17.stop(); | ||
} | ||
} | ||
}, _callee17, undefined); | ||
}))); | ||
it('stores user in redux', _asyncToGenerator(regeneratorRuntime.mark(function _callee18() { | ||
it('stores user in redux', _asyncToGenerator(regeneratorRuntime.mark(function _callee19() { | ||
var transport; | ||
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: | ||
@@ -706,3 +718,3 @@ transport = { | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
_context18.next = 4; | ||
_context19.next = 4; | ||
return _AccountsClient2.default.loginWithPassword('username', 'password'); | ||
@@ -715,12 +727,12 @@ | ||
case 'end': | ||
return _context18.stop(); | ||
return _context19.stop(); | ||
} | ||
} | ||
}, _callee18, undefined); | ||
}, _callee19, undefined); | ||
}))); | ||
it('stores tokens in redux', _asyncToGenerator(regeneratorRuntime.mark(function _callee19() { | ||
it('stores tokens in redux', _asyncToGenerator(regeneratorRuntime.mark(function _callee20() { | ||
var transport; | ||
return regeneratorRuntime.wrap(function _callee19$(_context19) { | ||
return regeneratorRuntime.wrap(function _callee20$(_context20) { | ||
while (1) { | ||
switch (_context19.prev = _context19.next) { | ||
switch (_context20.prev = _context20.next) { | ||
case 0: | ||
@@ -734,3 +746,3 @@ transport = { | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
_context19.next = 4; | ||
_context20.next = 4; | ||
return _AccountsClient2.default.loginWithPassword('username', 'password'); | ||
@@ -743,13 +755,13 @@ | ||
case 'end': | ||
return _context19.stop(); | ||
return _context20.stop(); | ||
} | ||
} | ||
}, _callee19, undefined); | ||
}, _callee20, undefined); | ||
}))); | ||
it('can hash password with specified algorithm', _asyncToGenerator(regeneratorRuntime.mark(function _callee20() { | ||
var loginWithPassword, transport, hashDigest; | ||
return regeneratorRuntime.wrap(function _callee20$(_context20) { | ||
it('can hash password with specified algorithm', _asyncToGenerator(regeneratorRuntime.mark(function _callee21() { | ||
var loginWithPassword, transport, hashed; | ||
return regeneratorRuntime.wrap(function _callee21$(_context21) { | ||
while (1) { | ||
switch (_context20.prev = _context20.next) { | ||
switch (_context21.prev = _context21.next) { | ||
case 0: | ||
@@ -767,4 +779,7 @@ loginWithPassword = jest.fn(function () { | ||
}, transport); | ||
hashDigest = _crypto2.default.createHash('sha256').update('password').digest('hex'); | ||
_context20.next = 6; | ||
hashed = { | ||
digest: _crypto2.default.createHash('sha256').update('password').digest('hex'), | ||
algorithm: 'sha256' | ||
}; | ||
_context21.next = 6; | ||
return _AccountsClient2.default.loginWithPassword('username', 'password'); | ||
@@ -774,3 +789,3 @@ | ||
expect(loginWithPassword.mock.calls[0][0]).toEqual('username'); | ||
expect(loginWithPassword.mock.calls[0][1]).toEqual(hashDigest); | ||
expect(loginWithPassword.mock.calls[0][1]).toEqual(hashed); | ||
expect(loginWithPassword.mock.calls.length).toEqual(1); | ||
@@ -780,14 +795,14 @@ | ||
case 'end': | ||
return _context20.stop(); | ||
return _context21.stop(); | ||
} | ||
} | ||
}, _callee20, undefined); | ||
}, _callee21, undefined); | ||
}))); | ||
}); | ||
describe('logout', function () { | ||
it('calls callback on successful logout', _asyncToGenerator(regeneratorRuntime.mark(function _callee21() { | ||
it('calls callback on successful logout', _asyncToGenerator(regeneratorRuntime.mark(function _callee22() { | ||
var transport, callback; | ||
return regeneratorRuntime.wrap(function _callee21$(_context21) { | ||
return regeneratorRuntime.wrap(function _callee22$(_context22) { | ||
while (1) { | ||
switch (_context21.prev = _context21.next) { | ||
switch (_context22.prev = _context22.next) { | ||
case 0: | ||
@@ -802,3 +817,3 @@ transport = { | ||
callback = jest.fn(); | ||
_context21.next = 5; | ||
_context22.next = 5; | ||
return _AccountsClient2.default.logout(callback); | ||
@@ -811,12 +826,12 @@ | ||
case 'end': | ||
return _context21.stop(); | ||
return _context22.stop(); | ||
} | ||
} | ||
}, _callee21, undefined); | ||
}, _callee22, undefined); | ||
}))); | ||
it('calls onLogout on successful logout', _asyncToGenerator(regeneratorRuntime.mark(function _callee22() { | ||
it('calls onLogout on successful logout', _asyncToGenerator(regeneratorRuntime.mark(function _callee23() { | ||
var onSignedOutHook, transport; | ||
return regeneratorRuntime.wrap(function _callee22$(_context22) { | ||
return regeneratorRuntime.wrap(function _callee23$(_context23) { | ||
while (1) { | ||
switch (_context22.prev = _context22.next) { | ||
switch (_context23.prev = _context23.next) { | ||
case 0: | ||
@@ -831,3 +846,3 @@ onSignedOutHook = jest.fn(); | ||
_AccountsClient2.default.config({ history: history, onSignedOutHook: onSignedOutHook }, transport); | ||
_context22.next = 5; | ||
_context23.next = 5; | ||
return _AccountsClient2.default.logout(); | ||
@@ -840,13 +855,13 @@ | ||
case 'end': | ||
return _context22.stop(); | ||
return _context23.stop(); | ||
} | ||
} | ||
}, _callee22, undefined); | ||
}, _callee23, undefined); | ||
}))); | ||
it('calls callback on failure with error message', _asyncToGenerator(regeneratorRuntime.mark(function _callee23() { | ||
it('calls callback on failure with error message', _asyncToGenerator(regeneratorRuntime.mark(function _callee24() { | ||
var transport, callback; | ||
return regeneratorRuntime.wrap(function _callee23$(_context23) { | ||
return regeneratorRuntime.wrap(function _callee24$(_context24) { | ||
while (1) { | ||
switch (_context23.prev = _context23.next) { | ||
switch (_context24.prev = _context24.next) { | ||
case 0: | ||
@@ -858,34 +873,39 @@ transport = { | ||
}; | ||
_context24.next = 3; | ||
return _AccountsClient2.default.instance.storeTokens({ accessToken: '1' }); | ||
_AccountsClient2.default.instance.storeTokens({ tokens: { accessToken: '1' } }); | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
case 3: | ||
_context24.next = 5; | ||
return _AccountsClient2.default.config({ history: history }, transport); | ||
case 5: | ||
callback = jest.fn(); | ||
_context23.prev = 4; | ||
_context23.next = 7; | ||
_context24.prev = 6; | ||
_context24.next = 9; | ||
return _AccountsClient2.default.logout(callback); | ||
case 7: | ||
case 9: | ||
throw new Error(); | ||
case 10: | ||
_context23.prev = 10; | ||
_context23.t0 = _context23['catch'](4); | ||
case 12: | ||
_context24.prev = 12; | ||
_context24.t0 = _context24['catch'](6); | ||
expect(_context23.t0.message).toEqual('error message'); | ||
expect(_context24.t0.message).toEqual('error message'); | ||
expect(callback.mock.calls.length).toEqual(1); | ||
expect(callback.mock.calls[0][0]).toEqual({ message: 'error message' }); | ||
case 15: | ||
case 17: | ||
case 'end': | ||
return _context23.stop(); | ||
return _context24.stop(); | ||
} | ||
} | ||
}, _callee23, undefined, [[4, 10]]); | ||
}, _callee24, undefined, [[6, 12]]); | ||
}))); | ||
it('clear tokens in redux', _asyncToGenerator(regeneratorRuntime.mark(function _callee24() { | ||
it('clear tokens in redux', _asyncToGenerator(regeneratorRuntime.mark(function _callee25() { | ||
var transport, callback; | ||
return regeneratorRuntime.wrap(function _callee24$(_context24) { | ||
return regeneratorRuntime.wrap(function _callee25$(_context25) { | ||
while (1) { | ||
switch (_context24.prev = _context24.next) { | ||
switch (_context25.prev = _context25.next) { | ||
case 0: | ||
@@ -898,30 +918,37 @@ transport = { | ||
_AccountsClient2.default.instance.storeTokens({ tokens: { accessToken: '1' } }); | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
_context25.next = 4; | ||
return _AccountsClient2.default.instance.storeTokens({ tokens: { accessToken: '1' } }); | ||
case 4: | ||
_context25.next = 6; | ||
return _AccountsClient2.default.instance.loadTokensFromStorage(); | ||
case 6: | ||
callback = jest.fn(); | ||
_context24.prev = 4; | ||
_context24.next = 7; | ||
_context25.prev = 7; | ||
_context25.next = 10; | ||
return _AccountsClient2.default.logout(callback); | ||
case 7: | ||
case 10: | ||
throw new Error(); | ||
case 10: | ||
_context24.prev = 10; | ||
_context24.t0 = _context24['catch'](4); | ||
case 13: | ||
_context25.prev = 13; | ||
_context25.t0 = _context25['catch'](7); | ||
expect(_AccountsClient2.default.instance.getState().get('tokens')).toEqual(null); | ||
case 13: | ||
case 16: | ||
case 'end': | ||
return _context24.stop(); | ||
return _context25.stop(); | ||
} | ||
} | ||
}, _callee24, undefined, [[4, 10]]); | ||
}, _callee25, undefined, [[7, 13]]); | ||
}))); | ||
it('clear user in redux', _asyncToGenerator(regeneratorRuntime.mark(function _callee25() { | ||
it('clear user in redux', _asyncToGenerator(regeneratorRuntime.mark(function _callee26() { | ||
var transport, callback; | ||
return regeneratorRuntime.wrap(function _callee25$(_context25) { | ||
return regeneratorRuntime.wrap(function _callee26$(_context26) { | ||
while (1) { | ||
switch (_context25.prev = _context25.next) { | ||
switch (_context26.prev = _context26.next) { | ||
case 0: | ||
@@ -937,4 +964,4 @@ transport = { | ||
callback = jest.fn(); | ||
_context25.prev = 4; | ||
_context25.next = 7; | ||
_context26.prev = 4; | ||
_context26.next = 7; | ||
return _AccountsClient2.default.logout(callback); | ||
@@ -946,4 +973,4 @@ | ||
case 10: | ||
_context25.prev = 10; | ||
_context25.t0 = _context25['catch'](4); | ||
_context26.prev = 10; | ||
_context26.t0 = _context26['catch'](4); | ||
@@ -954,18 +981,18 @@ expect(_AccountsClient2.default.instance.getState().get('user')).toEqual(null); | ||
case 'end': | ||
return _context25.stop(); | ||
return _context26.stop(); | ||
} | ||
} | ||
}, _callee25, undefined, [[4, 10]]); | ||
}, _callee26, undefined, [[4, 10]]); | ||
}))); | ||
}); | ||
describe('refreshSession', _asyncToGenerator(regeneratorRuntime.mark(function _callee28() { | ||
return regeneratorRuntime.wrap(function _callee28$(_context28) { | ||
describe('refreshSession', _asyncToGenerator(regeneratorRuntime.mark(function _callee29() { | ||
return regeneratorRuntime.wrap(function _callee29$(_context29) { | ||
while (1) { | ||
switch (_context28.prev = _context28.next) { | ||
switch (_context29.prev = _context29.next) { | ||
case 0: | ||
// TODO test that user and tokens are cleared if refreshToken is expired | ||
it('clears tokens and user if tokens are not set', _asyncToGenerator(regeneratorRuntime.mark(function _callee26() { | ||
return regeneratorRuntime.wrap(function _callee26$(_context26) { | ||
it('clears tokens and user if tokens are not set', _asyncToGenerator(regeneratorRuntime.mark(function _callee27() { | ||
return regeneratorRuntime.wrap(function _callee27$(_context27) { | ||
while (1) { | ||
switch (_context26.prev = _context26.next) { | ||
switch (_context27.prev = _context27.next) { | ||
case 0: | ||
@@ -979,15 +1006,15 @@ _AccountsClient2.default.config({}, {}); | ||
}); | ||
_context26.prev = 3; | ||
_context26.next = 6; | ||
_context27.prev = 3; | ||
_context27.next = 6; | ||
return _AccountsClient2.default.refreshSession(); | ||
case 6: | ||
_context26.next = 13; | ||
_context27.next = 13; | ||
break; | ||
case 8: | ||
_context26.prev = 8; | ||
_context26.t0 = _context26['catch'](3); | ||
_context27.prev = 8; | ||
_context27.t0 = _context27['catch'](3); | ||
expect(_context26.t0.message).toEqual('no tokens provided'); | ||
expect(_context27.t0.message).toEqual('no tokens provided'); | ||
expect(_AccountsClient2.default.instance.clearTokens.mock.calls.length).toEqual(1); | ||
@@ -998,16 +1025,19 @@ expect(_AccountsClient2.default.instance.clearUser.mock.calls.length).toEqual(1); | ||
case 'end': | ||
return _context26.stop(); | ||
return _context27.stop(); | ||
} | ||
} | ||
}, _callee26, undefined, [[3, 8]]); | ||
}, _callee27, undefined, [[3, 8]]); | ||
}))); | ||
it('clears tokens, users and throws error if bad refresh token provided', _asyncToGenerator(regeneratorRuntime.mark(function _callee27() { | ||
it('clears tokens, users and throws error if bad refresh token provided', _asyncToGenerator(regeneratorRuntime.mark(function _callee28() { | ||
var message; | ||
return regeneratorRuntime.wrap(function _callee27$(_context27) { | ||
return regeneratorRuntime.wrap(function _callee28$(_context28) { | ||
while (1) { | ||
switch (_context27.prev = _context27.next) { | ||
switch (_context28.prev = _context28.next) { | ||
case 0: | ||
_AccountsClient2.default.config({}, {}); | ||
localStorage.setItem('accounts:refreshToken', 'bad token'); | ||
localStorage.setItem('accounts:accessToken', 'bad token'); | ||
_context28.next = 4; | ||
return _AccountsClient2.default.config({}, {}); | ||
case 4: | ||
_AccountsClient2.default.instance.clearTokens = jest.fn(function () { | ||
@@ -1019,22 +1049,22 @@ return _AccountsClient2.default.instance.clearTokens; | ||
}); | ||
_context27.prev = 5; | ||
_context27.next = 8; | ||
_context28.prev = 6; | ||
_context28.next = 9; | ||
return _AccountsClient2.default.refreshSession(); | ||
case 8: | ||
case 9: | ||
throw new Error(); | ||
case 11: | ||
_context27.prev = 11; | ||
_context27.t0 = _context27['catch'](5); | ||
message = _context27.t0.message; | ||
case 12: | ||
_context28.prev = 12; | ||
_context28.t0 = _context28['catch'](6); | ||
message = _context28.t0.message; | ||
expect(message).toEqual('falsy token provided'); | ||
case 15: | ||
case 16: | ||
case 'end': | ||
return _context27.stop(); | ||
return _context28.stop(); | ||
} | ||
} | ||
}, _callee27, undefined, [[5, 11]]); | ||
}, _callee28, undefined, [[6, 12]]); | ||
}))); | ||
@@ -1078,14 +1108,14 @@ // it('requests a new token pair, sets the tokens and the user', async () => { | ||
case 'end': | ||
return _context28.stop(); | ||
return _context29.stop(); | ||
} | ||
} | ||
}, _callee28, undefined); | ||
}, _callee29, undefined); | ||
}))); | ||
describe('verifyEmail', function () { | ||
it('should return an AccountsError', _asyncToGenerator(regeneratorRuntime.mark(function _callee29() { | ||
it('should return an AccountsError', _asyncToGenerator(regeneratorRuntime.mark(function _callee30() { | ||
var error; | ||
return regeneratorRuntime.wrap(function _callee29$(_context29) { | ||
return regeneratorRuntime.wrap(function _callee30$(_context30) { | ||
while (1) { | ||
switch (_context29.prev = _context29.next) { | ||
switch (_context30.prev = _context30.next) { | ||
case 0: | ||
@@ -1097,4 +1127,4 @@ error = 'something bad'; | ||
} }); | ||
_context29.prev = 2; | ||
_context29.next = 5; | ||
_context30.prev = 2; | ||
_context30.next = 5; | ||
return _AccountsClient2.default.verifyEmail(); | ||
@@ -1106,20 +1136,20 @@ | ||
case 8: | ||
_context29.prev = 8; | ||
_context29.t0 = _context29['catch'](2); | ||
_context30.prev = 8; | ||
_context30.t0 = _context30['catch'](2); | ||
expect(_context29.t0.message).toEqual(error); | ||
expect(_context30.t0.message).toEqual(error); | ||
case 11: | ||
case 'end': | ||
return _context29.stop(); | ||
return _context30.stop(); | ||
} | ||
} | ||
}, _callee29, undefined, [[2, 8]]); | ||
}, _callee30, undefined, [[2, 8]]); | ||
}))); | ||
it('should call transport.verifyEmail', _asyncToGenerator(regeneratorRuntime.mark(function _callee30() { | ||
it('should call transport.verifyEmail', _asyncToGenerator(regeneratorRuntime.mark(function _callee31() { | ||
var mock; | ||
return regeneratorRuntime.wrap(function _callee30$(_context30) { | ||
return regeneratorRuntime.wrap(function _callee31$(_context31) { | ||
while (1) { | ||
switch (_context30.prev = _context30.next) { | ||
switch (_context31.prev = _context31.next) { | ||
case 0: | ||
@@ -1131,3 +1161,3 @@ mock = jest.fn(function () { | ||
_AccountsClient2.default.config({}, { verifyEmail: mock }); | ||
_context30.next = 4; | ||
_context31.next = 4; | ||
return _AccountsClient2.default.verifyEmail('token'); | ||
@@ -1141,6 +1171,6 @@ | ||
case 'end': | ||
return _context30.stop(); | ||
return _context31.stop(); | ||
} | ||
} | ||
}, _callee30, undefined); | ||
}, _callee31, undefined); | ||
}))); | ||
@@ -1150,15 +1180,47 @@ }); | ||
describe('resetPassword', function () { | ||
it('should return an AccountsError', _asyncToGenerator(regeneratorRuntime.mark(function _callee31() { | ||
it('should return an AccountsError', _asyncToGenerator(regeneratorRuntime.mark(function _callee32() { | ||
var error; | ||
return regeneratorRuntime.wrap(function _callee31$(_context31) { | ||
return regeneratorRuntime.wrap(function _callee32$(_context32) { | ||
while (1) { | ||
switch (_context31.prev = _context31.next) { | ||
switch (_context32.prev = _context32.next) { | ||
case 0: | ||
error = 'something bad'; | ||
_AccountsClient2.default.config({}, { resetPassword: function resetPassword() { | ||
_context32.next = 3; | ||
return _AccountsClient2.default.config({}, { resetPassword: function resetPassword() { | ||
return Promise.reject({ message: error }); | ||
} }); | ||
_context31.prev = 2; | ||
_context31.next = 5; | ||
case 3: | ||
_context32.prev = 3; | ||
_context32.next = 6; | ||
return _AccountsClient2.default.resetPassword('badtoken', 'password'); | ||
case 6: | ||
throw new Error(); | ||
case 9: | ||
_context32.prev = 9; | ||
_context32.t0 = _context32['catch'](3); | ||
expect(_context32.t0.message).toEqual(error); | ||
case 12: | ||
case 'end': | ||
return _context32.stop(); | ||
} | ||
} | ||
}, _callee32, undefined, [[3, 9]]); | ||
}))); | ||
it('throws if password is invalid', _asyncToGenerator(regeneratorRuntime.mark(function _callee33() { | ||
return regeneratorRuntime.wrap(function _callee33$(_context33) { | ||
while (1) { | ||
switch (_context33.prev = _context33.next) { | ||
case 0: | ||
_context33.next = 2; | ||
return _AccountsClient2.default.config({}, {}); | ||
case 2: | ||
_context33.prev = 2; | ||
_context33.next = 5; | ||
return _AccountsClient2.default.resetPassword(); | ||
@@ -1170,20 +1232,20 @@ | ||
case 8: | ||
_context31.prev = 8; | ||
_context31.t0 = _context31['catch'](2); | ||
_context33.prev = 8; | ||
_context33.t0 = _context33['catch'](2); | ||
expect(_context31.t0.message).toEqual(error); | ||
expect(_context33.t0.message).toEqual('Password is invalid!'); | ||
case 11: | ||
case 'end': | ||
return _context31.stop(); | ||
return _context33.stop(); | ||
} | ||
} | ||
}, _callee31, undefined, [[2, 8]]); | ||
}, _callee33, undefined, [[2, 8]]); | ||
}))); | ||
it('should call transport.resetPassword', _asyncToGenerator(regeneratorRuntime.mark(function _callee32() { | ||
it('should call transport.resetPassword', _asyncToGenerator(regeneratorRuntime.mark(function _callee34() { | ||
var mock; | ||
return regeneratorRuntime.wrap(function _callee32$(_context32) { | ||
return regeneratorRuntime.wrap(function _callee34$(_context34) { | ||
while (1) { | ||
switch (_context32.prev = _context32.next) { | ||
switch (_context34.prev = _context34.next) { | ||
case 0: | ||
@@ -1195,3 +1257,3 @@ mock = jest.fn(function () { | ||
_AccountsClient2.default.config({}, { resetPassword: mock }); | ||
_context32.next = 4; | ||
_context34.next = 4; | ||
return _AccountsClient2.default.resetPassword('token', 'newPassword'); | ||
@@ -1206,6 +1268,6 @@ | ||
case 'end': | ||
return _context32.stop(); | ||
return _context34.stop(); | ||
} | ||
} | ||
}, _callee32, undefined); | ||
}, _callee34, undefined); | ||
}))); | ||
@@ -1215,7 +1277,7 @@ }); | ||
describe('requestPasswordReset', function () { | ||
it('should return an AccountsError', _asyncToGenerator(regeneratorRuntime.mark(function _callee33() { | ||
it('should return an AccountsError', _asyncToGenerator(regeneratorRuntime.mark(function _callee35() { | ||
var error; | ||
return regeneratorRuntime.wrap(function _callee33$(_context33) { | ||
return regeneratorRuntime.wrap(function _callee35$(_context35) { | ||
while (1) { | ||
switch (_context33.prev = _context33.next) { | ||
switch (_context35.prev = _context35.next) { | ||
case 0: | ||
@@ -1227,4 +1289,4 @@ error = 'something bad'; | ||
} }); | ||
_context33.prev = 2; | ||
_context33.next = 5; | ||
_context35.prev = 2; | ||
_context35.next = 5; | ||
return _AccountsClient2.default.requestPasswordReset('email@g.co'); | ||
@@ -1236,20 +1298,20 @@ | ||
case 8: | ||
_context33.prev = 8; | ||
_context33.t0 = _context33['catch'](2); | ||
_context35.prev = 8; | ||
_context35.t0 = _context35['catch'](2); | ||
expect(_context33.t0.message).toEqual(error); | ||
expect(_context35.t0.message).toEqual(error); | ||
case 11: | ||
case 'end': | ||
return _context33.stop(); | ||
return _context35.stop(); | ||
} | ||
} | ||
}, _callee33, undefined, [[2, 8]]); | ||
}, _callee35, undefined, [[2, 8]]); | ||
}))); | ||
it('should call transport.sendResetPasswordEmail', _asyncToGenerator(regeneratorRuntime.mark(function _callee34() { | ||
it('should call transport.sendResetPasswordEmail', _asyncToGenerator(regeneratorRuntime.mark(function _callee36() { | ||
var mock; | ||
return regeneratorRuntime.wrap(function _callee34$(_context34) { | ||
return regeneratorRuntime.wrap(function _callee36$(_context36) { | ||
while (1) { | ||
switch (_context34.prev = _context34.next) { | ||
switch (_context36.prev = _context36.next) { | ||
case 0: | ||
@@ -1261,3 +1323,3 @@ mock = jest.fn(function () { | ||
_AccountsClient2.default.config({}, { sendResetPasswordEmail: mock }); | ||
_context34.next = 4; | ||
_context36.next = 4; | ||
return _AccountsClient2.default.requestPasswordReset('email@g.co'); | ||
@@ -1271,13 +1333,13 @@ | ||
case 'end': | ||
return _context34.stop(); | ||
return _context36.stop(); | ||
} | ||
} | ||
}, _callee34, undefined); | ||
}, _callee36, undefined); | ||
}))); | ||
it('should throw if an invalid email is provided', _asyncToGenerator(regeneratorRuntime.mark(function _callee35() { | ||
it('should throw if an invalid email is provided', _asyncToGenerator(regeneratorRuntime.mark(function _callee37() { | ||
var mock; | ||
return regeneratorRuntime.wrap(function _callee35$(_context35) { | ||
return regeneratorRuntime.wrap(function _callee37$(_context37) { | ||
while (1) { | ||
switch (_context35.prev = _context35.next) { | ||
switch (_context37.prev = _context37.next) { | ||
case 0: | ||
@@ -1287,4 +1349,4 @@ mock = jest.fn(); | ||
_AccountsClient2.default.config({}, { sendResetPasswordEmail: mock }); | ||
_context35.prev = 2; | ||
_context35.next = 5; | ||
_context37.prev = 2; | ||
_context37.next = 5; | ||
return _AccountsClient2.default.requestPasswordReset('email'); | ||
@@ -1296,6 +1358,6 @@ | ||
case 8: | ||
_context35.prev = 8; | ||
_context35.t0 = _context35['catch'](2); | ||
_context37.prev = 8; | ||
_context37.t0 = _context37['catch'](2); | ||
expect(_context35.t0.message).toEqual('Valid email must be provided'); | ||
expect(_context37.t0.message).toEqual('Valid email must be provided'); | ||
expect(mock.mock.calls.length).toEqual(0); | ||
@@ -1305,6 +1367,6 @@ | ||
case 'end': | ||
return _context35.stop(); | ||
return _context37.stop(); | ||
} | ||
} | ||
}, _callee35, undefined, [[2, 8]]); | ||
}, _callee37, undefined, [[2, 8]]); | ||
}))); | ||
@@ -1314,7 +1376,7 @@ }); | ||
describe('requestVerificationEmail', function () { | ||
it('should return an AccountsError', _asyncToGenerator(regeneratorRuntime.mark(function _callee36() { | ||
it('should return an AccountsError', _asyncToGenerator(regeneratorRuntime.mark(function _callee38() { | ||
var error; | ||
return regeneratorRuntime.wrap(function _callee36$(_context36) { | ||
return regeneratorRuntime.wrap(function _callee38$(_context38) { | ||
while (1) { | ||
switch (_context36.prev = _context36.next) { | ||
switch (_context38.prev = _context38.next) { | ||
case 0: | ||
@@ -1326,4 +1388,4 @@ error = 'something bad'; | ||
} }); | ||
_context36.prev = 2; | ||
_context36.next = 5; | ||
_context38.prev = 2; | ||
_context38.next = 5; | ||
return _AccountsClient2.default.requestVerificationEmail('email@g.co'); | ||
@@ -1335,20 +1397,20 @@ | ||
case 8: | ||
_context36.prev = 8; | ||
_context36.t0 = _context36['catch'](2); | ||
_context38.prev = 8; | ||
_context38.t0 = _context38['catch'](2); | ||
expect(_context36.t0.message).toEqual(error); | ||
expect(_context38.t0.message).toEqual(error); | ||
case 11: | ||
case 'end': | ||
return _context36.stop(); | ||
return _context38.stop(); | ||
} | ||
} | ||
}, _callee36, undefined, [[2, 8]]); | ||
}, _callee38, undefined, [[2, 8]]); | ||
}))); | ||
it('should call transport.sendVerificationEmail', _asyncToGenerator(regeneratorRuntime.mark(function _callee37() { | ||
it('should call transport.sendVerificationEmail', _asyncToGenerator(regeneratorRuntime.mark(function _callee39() { | ||
var mock; | ||
return regeneratorRuntime.wrap(function _callee37$(_context37) { | ||
return regeneratorRuntime.wrap(function _callee39$(_context39) { | ||
while (1) { | ||
switch (_context37.prev = _context37.next) { | ||
switch (_context39.prev = _context39.next) { | ||
case 0: | ||
@@ -1360,3 +1422,3 @@ mock = jest.fn(function () { | ||
_AccountsClient2.default.config({}, { sendVerificationEmail: mock }); | ||
_context37.next = 4; | ||
_context39.next = 4; | ||
return _AccountsClient2.default.requestVerificationEmail('email@g.co'); | ||
@@ -1370,13 +1432,13 @@ | ||
case 'end': | ||
return _context37.stop(); | ||
return _context39.stop(); | ||
} | ||
} | ||
}, _callee37, undefined); | ||
}, _callee39, undefined); | ||
}))); | ||
it('should throw if an invalid email is provided', _asyncToGenerator(regeneratorRuntime.mark(function _callee38() { | ||
it('should throw if an invalid email is provided', _asyncToGenerator(regeneratorRuntime.mark(function _callee40() { | ||
var mock; | ||
return regeneratorRuntime.wrap(function _callee38$(_context38) { | ||
return regeneratorRuntime.wrap(function _callee40$(_context40) { | ||
while (1) { | ||
switch (_context38.prev = _context38.next) { | ||
switch (_context40.prev = _context40.next) { | ||
case 0: | ||
@@ -1386,4 +1448,4 @@ mock = jest.fn(); | ||
_AccountsClient2.default.config({}, { sendVerificationEmail: mock }); | ||
_context38.prev = 2; | ||
_context38.next = 5; | ||
_context40.prev = 2; | ||
_context40.next = 5; | ||
return _AccountsClient2.default.requestVerificationEmail('email'); | ||
@@ -1395,6 +1457,6 @@ | ||
case 8: | ||
_context38.prev = 8; | ||
_context38.t0 = _context38['catch'](2); | ||
_context40.prev = 8; | ||
_context40.t0 = _context40['catch'](2); | ||
expect(_context38.t0.message).toEqual('Valid email must be provided'); | ||
expect(_context40.t0.message).toEqual('Valid email must be provided'); | ||
expect(mock.mock.calls.length).toEqual(0); | ||
@@ -1404,8 +1466,349 @@ | ||
case 'end': | ||
return _context38.stop(); | ||
return _context40.stop(); | ||
} | ||
} | ||
}, _callee38, undefined, [[2, 8]]); | ||
}, _callee40, undefined, [[2, 8]]); | ||
}))); | ||
}); | ||
describe('impersonate', function () { | ||
it('should throw error if username is not provided', _asyncToGenerator(regeneratorRuntime.mark(function _callee41() { | ||
return regeneratorRuntime.wrap(function _callee41$(_context41) { | ||
while (1) { | ||
switch (_context41.prev = _context41.next) { | ||
case 0: | ||
_context41.next = 2; | ||
return _AccountsClient2.default.config({ history: history }, {}); | ||
case 2: | ||
_context41.prev = 2; | ||
_context41.next = 5; | ||
return _AccountsClient2.default.impersonate(); | ||
case 5: | ||
_context41.next = 10; | ||
break; | ||
case 7: | ||
_context41.prev = 7; | ||
_context41.t0 = _context41['catch'](2); | ||
expect(_context41.t0.message).toEqual('Username is required'); | ||
case 10: | ||
case 'end': | ||
return _context41.stop(); | ||
} | ||
} | ||
}, _callee41, undefined, [[2, 7]]); | ||
}))); | ||
it('should throw error if already impersonating', _asyncToGenerator(regeneratorRuntime.mark(function _callee42() { | ||
return regeneratorRuntime.wrap(function _callee42$(_context42) { | ||
while (1) { | ||
switch (_context42.prev = _context42.next) { | ||
case 0: | ||
_context42.next = 2; | ||
return _AccountsClient2.default.config({ history: history }, {}); | ||
case 2: | ||
_AccountsClient2.default.instance.isImpersonated = function () { | ||
return true; | ||
}; | ||
_context42.prev = 3; | ||
_context42.next = 6; | ||
return _AccountsClient2.default.impersonate('user'); | ||
case 6: | ||
_context42.next = 11; | ||
break; | ||
case 8: | ||
_context42.prev = 8; | ||
_context42.t0 = _context42['catch'](3); | ||
expect(_context42.t0.message).toEqual('User already impersonating'); | ||
case 11: | ||
case 'end': | ||
return _context42.stop(); | ||
} | ||
} | ||
}, _callee42, undefined, [[3, 8]]); | ||
}))); | ||
it('should throw if no access token is present', _asyncToGenerator(regeneratorRuntime.mark(function _callee43() { | ||
var transport; | ||
return regeneratorRuntime.wrap(function _callee43$(_context43) { | ||
while (1) { | ||
switch (_context43.prev = _context43.next) { | ||
case 0: | ||
transport = { | ||
impersonate: jest.fn(function () { | ||
return Promise.resolve({ authorized: false }); | ||
}) | ||
}; | ||
_context43.next = 3; | ||
return _AccountsClient2.default.config({ history: history }, transport); | ||
case 3: | ||
_context43.prev = 3; | ||
_context43.next = 6; | ||
return _AccountsClient2.default.impersonate('user'); | ||
case 6: | ||
_context43.next = 12; | ||
break; | ||
case 8: | ||
_context43.prev = 8; | ||
_context43.t0 = _context43['catch'](3); | ||
expect(_context43.t0.message).toEqual('There is no access tokens available'); | ||
expect(transport.impersonate.mock.calls.length).toEqual(0); | ||
case 12: | ||
case 'end': | ||
return _context43.stop(); | ||
} | ||
} | ||
}, _callee43, undefined, [[3, 8]]); | ||
}))); | ||
it('should throw if server return unauthorized', _asyncToGenerator(regeneratorRuntime.mark(function _callee44() { | ||
var transport; | ||
return regeneratorRuntime.wrap(function _callee44$(_context44) { | ||
while (1) { | ||
switch (_context44.prev = _context44.next) { | ||
case 0: | ||
transport = { | ||
impersonate: function impersonate() { | ||
return Promise.resolve({ authorized: false }); | ||
} | ||
}; | ||
_context44.next = 3; | ||
return _AccountsClient2.default.instance.storeTokens({ accessToken: '1' }); | ||
case 3: | ||
_context44.next = 5; | ||
return _AccountsClient2.default.config({ history: history }, transport); | ||
case 5: | ||
_context44.prev = 5; | ||
_context44.next = 8; | ||
return _AccountsClient2.default.impersonate('user'); | ||
case 8: | ||
_context44.next = 13; | ||
break; | ||
case 10: | ||
_context44.prev = 10; | ||
_context44.t0 = _context44['catch'](5); | ||
expect(_context44.t0.message).toEqual('User unauthorized to impersonate user'); | ||
case 13: | ||
case 'end': | ||
return _context44.stop(); | ||
} | ||
} | ||
}, _callee44, undefined, [[5, 10]]); | ||
}))); | ||
it('should set state correctly if impersonation was authorized', _asyncToGenerator(regeneratorRuntime.mark(function _callee45() { | ||
var impersonatedUser, impersonateResult, transport, result, tokens; | ||
return regeneratorRuntime.wrap(function _callee45$(_context45) { | ||
while (1) { | ||
switch (_context45.prev = _context45.next) { | ||
case 0: | ||
impersonatedUser = { id: 2, username: 'impUser' }; | ||
impersonateResult = { | ||
authorized: true, | ||
tokens: { accessToken: 'newAccessToken', refreshToken: 'newRefreshToken' }, | ||
user: impersonatedUser | ||
}; | ||
transport = { | ||
loginWithPassword: function loginWithPassword() { | ||
return Promise.resolve(loggedInUser); | ||
}, | ||
impersonate: function impersonate() { | ||
return Promise.resolve(impersonateResult); | ||
} | ||
}; | ||
_context45.next = 5; | ||
return _AccountsClient2.default.config({ history: history }, transport); | ||
case 5: | ||
_context45.next = 7; | ||
return _AccountsClient2.default.loginWithPassword('username', 'password'); | ||
case 7: | ||
_context45.next = 9; | ||
return _AccountsClient2.default.impersonate('impUser'); | ||
case 9: | ||
result = _context45.sent; | ||
tokens = _AccountsClient2.default.tokens(); | ||
expect(tokens).toEqual({ | ||
accessToken: 'newAccessToken', | ||
refreshToken: 'newRefreshToken' | ||
}); | ||
expect(_AccountsClient2.default.user()).toEqual(impersonatedUser); | ||
expect(_AccountsClient2.default.isImpersonated()).toBe(true); | ||
expect(_AccountsClient2.default.tokens()); | ||
expect(_AccountsClient2.default.originalTokens()).toEqual({ | ||
accessToken: 'accessToken', | ||
refreshToken: 'refreshToken' | ||
}); | ||
expect(result).toBe(impersonateResult); | ||
case 17: | ||
case 'end': | ||
return _context45.stop(); | ||
} | ||
} | ||
}, _callee45, undefined); | ||
}))); | ||
}); | ||
describe('stopImpersonation', function () { | ||
it('should not replace tokens if not impersonating', _asyncToGenerator(regeneratorRuntime.mark(function _callee46() { | ||
var transport; | ||
return regeneratorRuntime.wrap(function _callee46$(_context46) { | ||
while (1) { | ||
switch (_context46.prev = _context46.next) { | ||
case 0: | ||
transport = { | ||
loginWithPassword: function loginWithPassword() { | ||
return Promise.resolve(loggedInUser); | ||
} | ||
}; | ||
_context46.next = 3; | ||
return _AccountsClient2.default.config({ history: history }, transport); | ||
case 3: | ||
_context46.next = 5; | ||
return _AccountsClient2.default.loginWithPassword('username', 'password'); | ||
case 5: | ||
expect(_AccountsClient2.default.originalTokens()).toEqual({ accessToken: null, refreshToken: null }); | ||
expect(_AccountsClient2.default.tokens()).toEqual(loggedInUser.tokens); | ||
_context46.next = 9; | ||
return _AccountsClient2.default.stopImpersonation(); | ||
case 9: | ||
expect(_AccountsClient2.default.originalTokens()).toEqual({ accessToken: null, refreshToken: null }); | ||
expect(_AccountsClient2.default.tokens()).toEqual(loggedInUser.tokens); | ||
case 11: | ||
case 'end': | ||
return _context46.stop(); | ||
} | ||
} | ||
}, _callee46, undefined); | ||
}))); | ||
it('should set impersonated state to false', _asyncToGenerator(regeneratorRuntime.mark(function _callee47() { | ||
var impersonateResult, transport; | ||
return regeneratorRuntime.wrap(function _callee47$(_context47) { | ||
while (1) { | ||
switch (_context47.prev = _context47.next) { | ||
case 0: | ||
impersonateResult = { | ||
authorized: true, | ||
tokens: { accessToken: 'newAccessToken', refreshToken: 'newRefreshToken' }, | ||
user: { id: 2, username: 'impUser' } | ||
}; | ||
transport = { | ||
impersonate: function impersonate() { | ||
return Promise.resolve(impersonateResult); | ||
} | ||
}; | ||
_context47.next = 4; | ||
return _AccountsClient2.default.instance.storeTokens({ accessToken: '1' }); | ||
case 4: | ||
_context47.next = 6; | ||
return _AccountsClient2.default.config({ history: history }, transport); | ||
case 6: | ||
_AccountsClient2.default.instance.refreshSession = function () { | ||
return Promise.resolve(); | ||
}; | ||
_context47.next = 9; | ||
return _AccountsClient2.default.impersonate('impUser'); | ||
case 9: | ||
expect(_AccountsClient2.default.isImpersonated()).toBe(true); | ||
_context47.next = 12; | ||
return _AccountsClient2.default.stopImpersonation(); | ||
case 12: | ||
expect(_AccountsClient2.default.isImpersonated()).toBe(false); | ||
case 13: | ||
case 'end': | ||
return _context47.stop(); | ||
} | ||
} | ||
}, _callee47, undefined); | ||
}))); | ||
it('should set the original tokens as current tokens and delete original tokens', _asyncToGenerator(regeneratorRuntime.mark(function _callee48() { | ||
var impersonateResult, transport, tokens; | ||
return regeneratorRuntime.wrap(function _callee48$(_context48) { | ||
while (1) { | ||
switch (_context48.prev = _context48.next) { | ||
case 0: | ||
impersonateResult = { | ||
authorized: true, | ||
tokens: { accessToken: 'newAccessToken', refreshToken: 'newRefreshToken' }, | ||
user: { id: 2, username: 'impUser' } | ||
}; | ||
transport = { | ||
loginWithPassword: function loginWithPassword() { | ||
return Promise.resolve(loggedInUser); | ||
}, | ||
impersonate: function impersonate() { | ||
return Promise.resolve(impersonateResult); | ||
} | ||
}; | ||
_context48.next = 4; | ||
return _AccountsClient2.default.config({ history: history }, transport); | ||
case 4: | ||
_AccountsClient2.default.instance.refreshSession = function () { | ||
return Promise.resolve(); | ||
}; | ||
_context48.next = 7; | ||
return _AccountsClient2.default.loginWithPassword('username', 'password'); | ||
case 7: | ||
tokens = _AccountsClient2.default.tokens(); | ||
_context48.next = 10; | ||
return _AccountsClient2.default.impersonate('impUser'); | ||
case 10: | ||
expect(_AccountsClient2.default.tokens()).toEqual({ accessToken: 'newAccessToken', refreshToken: 'newRefreshToken' }); | ||
_context48.next = 13; | ||
return _AccountsClient2.default.stopImpersonation(); | ||
case 13: | ||
expect(_AccountsClient2.default.tokens()).toEqual(tokens); | ||
expect(_AccountsClient2.default.originalTokens()).toEqual({ accessToken: null, refreshToken: null }); | ||
case 15: | ||
case 'end': | ||
return _context48.stop(); | ||
} | ||
} | ||
}, _callee48, undefined); | ||
}))); | ||
}); | ||
}); |
@@ -8,2 +8,6 @@ 'use strict'; | ||
var _isString = require('lodash/isString'); | ||
var _isString2 = _interopRequireDefault(_isString); | ||
var _cryptoJs = require('crypto-js'); | ||
@@ -25,7 +29,14 @@ | ||
}; | ||
var hashPassword = exports.hashPassword = function hashPassword(password, algorithm) { | ||
if ((0, _isString2.default)(password)) { | ||
var cryptoAlgoKey = mapHashConstant[algorithm]; | ||
var cryptoFunction = _cryptoJs2.default[cryptoAlgoKey]; | ||
return { | ||
digest: cryptoFunction(password).toString(), | ||
algorithm: algorithm | ||
}; | ||
} | ||
var hashPassword = exports.hashPassword = function hashPassword(password, algorithm) { | ||
var cryptoAlgoKey = mapHashConstant[algorithm]; | ||
var cryptoFunction = _cryptoJs2.default[cryptoAlgoKey]; | ||
return cryptoFunction(password).toString(); | ||
// Prehashed password object | ||
return password; | ||
}; |
@@ -6,3 +6,3 @@ 'use strict'; | ||
}); | ||
exports.clearUser = exports.clearTokens = exports.setTokens = exports.setUser = exports.loggingIn = undefined; | ||
exports.setImpersonated = exports.clearOriginalTokens = exports.setOriginalTokens = exports.clearUser = exports.clearTokens = exports.setTokens = exports.setUser = exports.loggingIn = undefined; | ||
@@ -18,2 +18,5 @@ var _immutable = require('immutable'); | ||
var LOGGING_IN = PATH + 'LOGGING_IN'; | ||
var SET_ORIGINAL_TOKENS = PATH + 'SET_ORIGINAL_TOKENS'; | ||
var CLEAR_ORIGINAL_TOKENS = PATH + 'CLEAR_ORIGINAL_TOKENS'; | ||
var SET_IMPERSONATED = PATH + 'SET_IMPERSONATED'; | ||
@@ -24,3 +27,5 @@ var initialState = (0, _immutable.Map)({ | ||
tokens: null, | ||
loggingIn: false | ||
loggingIn: false, | ||
originalTokens: null, | ||
isImpersonated: false | ||
}); | ||
@@ -52,2 +57,3 @@ | ||
{ | ||
state.set('originalTokens', null); | ||
return state.set('tokens', null); | ||
@@ -65,2 +71,22 @@ } | ||
} | ||
case SET_ORIGINAL_TOKENS: | ||
{ | ||
var _tokens = action.payload.tokens; | ||
return state.set('originalTokens', (0, _immutable.Map)(_tokens)); | ||
} | ||
case CLEAR_ORIGINAL_TOKENS: | ||
{ | ||
return state.set('originalTokens', null); | ||
} | ||
case SET_IMPERSONATED: | ||
{ | ||
var isImpersonated = action.payload.isImpersonated; | ||
return state.set('isImpersonated', isImpersonated); | ||
} | ||
default: | ||
@@ -110,2 +136,26 @@ break; | ||
}; | ||
}; | ||
var setOriginalTokens = exports.setOriginalTokens = function setOriginalTokens(tokens) { | ||
return { | ||
type: SET_ORIGINAL_TOKENS, | ||
payload: { | ||
tokens: tokens | ||
} | ||
}; | ||
}; | ||
var clearOriginalTokens = exports.clearOriginalTokens = function clearOriginalTokens() { | ||
return { | ||
type: CLEAR_ORIGINAL_TOKENS | ||
}; | ||
}; | ||
var setImpersonated = exports.setImpersonated = function setImpersonated(isImpersonated) { | ||
return { | ||
type: SET_IMPERSONATED, | ||
payload: { | ||
isImpersonated: isImpersonated | ||
} | ||
}; | ||
}; |
648
lib/index.js
(function webpackUniversalModuleDefinition(root, factory) { | ||
if(typeof exports === 'object' && typeof module === 'object') | ||
module.exports = factory(require("babel-runtime/helpers/extends"), require("babel-runtime/regenerator"), require("babel-runtime/core-js/promise"), require("babel-runtime/helpers/asyncToGenerator"), require("babel-runtime/helpers/defineProperty"), require("babel-runtime/helpers/classCallCheck"), require("babel-runtime/helpers/createClass"), require("lodash"), require("jwt-decode"), require("@accounts/common"), require("babel-runtime/helpers/toConsumableArray"), require("redux"), require("immutable"), require("redux-immutable"), require("crypto-js")); | ||
module.exports = factory(require("babel-runtime/helpers/extends"), require("babel-runtime/regenerator"), require("babel-runtime/core-js/promise"), require("babel-runtime/helpers/asyncToGenerator"), require("babel-runtime/helpers/defineProperty"), require("babel-runtime/helpers/classCallCheck"), require("babel-runtime/helpers/createClass"), require("lodash"), require("jwt-decode"), require("@accounts/common"), require("babel-runtime/helpers/toConsumableArray"), require("redux"), require("immutable"), require("redux-immutable"), require("lodash/isString"), require("crypto-js")); | ||
else if(typeof define === 'function' && define.amd) | ||
define(["babel-runtime/helpers/extends", "babel-runtime/regenerator", "babel-runtime/core-js/promise", "babel-runtime/helpers/asyncToGenerator", "babel-runtime/helpers/defineProperty", "babel-runtime/helpers/classCallCheck", "babel-runtime/helpers/createClass", "lodash", "jwt-decode", "@accounts/common", "babel-runtime/helpers/toConsumableArray", "redux", "immutable", "redux-immutable", "crypto-js"], factory); | ||
define(["babel-runtime/helpers/extends", "babel-runtime/regenerator", "babel-runtime/core-js/promise", "babel-runtime/helpers/asyncToGenerator", "babel-runtime/helpers/defineProperty", "babel-runtime/helpers/classCallCheck", "babel-runtime/helpers/createClass", "lodash", "jwt-decode", "@accounts/common", "babel-runtime/helpers/toConsumableArray", "redux", "immutable", "redux-immutable", "lodash/isString", "crypto-js"], factory); | ||
else if(typeof exports === 'object') | ||
exports["@accounts/client"] = factory(require("babel-runtime/helpers/extends"), require("babel-runtime/regenerator"), require("babel-runtime/core-js/promise"), require("babel-runtime/helpers/asyncToGenerator"), require("babel-runtime/helpers/defineProperty"), require("babel-runtime/helpers/classCallCheck"), require("babel-runtime/helpers/createClass"), require("lodash"), require("jwt-decode"), require("@accounts/common"), require("babel-runtime/helpers/toConsumableArray"), require("redux"), require("immutable"), require("redux-immutable"), require("crypto-js")); | ||
exports["@accounts/client"] = factory(require("babel-runtime/helpers/extends"), require("babel-runtime/regenerator"), require("babel-runtime/core-js/promise"), require("babel-runtime/helpers/asyncToGenerator"), require("babel-runtime/helpers/defineProperty"), require("babel-runtime/helpers/classCallCheck"), require("babel-runtime/helpers/createClass"), require("lodash"), require("jwt-decode"), require("@accounts/common"), require("babel-runtime/helpers/toConsumableArray"), require("redux"), require("immutable"), require("redux-immutable"), require("lodash/isString"), require("crypto-js")); | ||
else | ||
root["@accounts/client"] = factory(root["babel-runtime/helpers/extends"], root["babel-runtime/regenerator"], root["babel-runtime/core-js/promise"], root["babel-runtime/helpers/asyncToGenerator"], root["babel-runtime/helpers/defineProperty"], root["babel-runtime/helpers/classCallCheck"], root["babel-runtime/helpers/createClass"], root["lodash"], root["jwt-decode"], root["@accounts/common"], root["babel-runtime/helpers/toConsumableArray"], root["redux"], root["immutable"], root["redux-immutable"], root["crypto-js"]); | ||
})(this, function(__WEBPACK_EXTERNAL_MODULE_2__, __WEBPACK_EXTERNAL_MODULE_3__, __WEBPACK_EXTERNAL_MODULE_4__, __WEBPACK_EXTERNAL_MODULE_5__, __WEBPACK_EXTERNAL_MODULE_6__, __WEBPACK_EXTERNAL_MODULE_7__, __WEBPACK_EXTERNAL_MODULE_8__, __WEBPACK_EXTERNAL_MODULE_9__, __WEBPACK_EXTERNAL_MODULE_10__, __WEBPACK_EXTERNAL_MODULE_11__, __WEBPACK_EXTERNAL_MODULE_15__, __WEBPACK_EXTERNAL_MODULE_16__, __WEBPACK_EXTERNAL_MODULE_17__, __WEBPACK_EXTERNAL_MODULE_18__, __WEBPACK_EXTERNAL_MODULE_21__) { | ||
root["@accounts/client"] = factory(root["babel-runtime/helpers/extends"], root["babel-runtime/regenerator"], root["babel-runtime/core-js/promise"], root["babel-runtime/helpers/asyncToGenerator"], root["babel-runtime/helpers/defineProperty"], root["babel-runtime/helpers/classCallCheck"], root["babel-runtime/helpers/createClass"], root["lodash"], root["jwt-decode"], root["@accounts/common"], root["babel-runtime/helpers/toConsumableArray"], root["redux"], root["immutable"], root["redux-immutable"], root["lodash/isString"], root["crypto-js"]); | ||
})(this, function(__WEBPACK_EXTERNAL_MODULE_2__, __WEBPACK_EXTERNAL_MODULE_3__, __WEBPACK_EXTERNAL_MODULE_4__, __WEBPACK_EXTERNAL_MODULE_5__, __WEBPACK_EXTERNAL_MODULE_6__, __WEBPACK_EXTERNAL_MODULE_7__, __WEBPACK_EXTERNAL_MODULE_8__, __WEBPACK_EXTERNAL_MODULE_9__, __WEBPACK_EXTERNAL_MODULE_10__, __WEBPACK_EXTERNAL_MODULE_11__, __WEBPACK_EXTERNAL_MODULE_15__, __WEBPACK_EXTERNAL_MODULE_16__, __WEBPACK_EXTERNAL_MODULE_17__, __WEBPACK_EXTERNAL_MODULE_18__, __WEBPACK_EXTERNAL_MODULE_21__, __WEBPACK_EXTERNAL_MODULE_22__) { | ||
return /******/ (function(modules) { // webpackBootstrap | ||
@@ -262,11 +262,6 @@ /******/ // The module cache | ||
}, { | ||
key: 'user', | ||
value: function user() { | ||
var user = this.getState().get('user'); | ||
return user ? user.toJS() : null; | ||
} | ||
}, { | ||
key: 'tokens', | ||
key: 'loadTokensFromStorage', | ||
value: function () { | ||
var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4() { | ||
var tokens; | ||
return _regenerator2.default.wrap(function _callee4$(_context4) { | ||
@@ -286,8 +281,10 @@ while (1) { | ||
_context4.t1 = _context4.sent; | ||
return _context4.abrupt('return', { | ||
tokens = { | ||
accessToken: _context4.t0, | ||
refreshToken: _context4.t1 | ||
}); | ||
}; | ||
case 7: | ||
this.store.dispatch((0, _module.setTokens)(tokens)); | ||
case 8: | ||
case 'end': | ||
@@ -300,12 +297,21 @@ return _context4.stop(); | ||
function tokens() { | ||
function loadTokensFromStorage() { | ||
return _ref4.apply(this, arguments); | ||
} | ||
return tokens; | ||
return loadTokensFromStorage; | ||
}() | ||
}, { | ||
key: 'clearTokens', | ||
key: 'user', | ||
value: function user() { | ||
var user = this.getState().get('user'); | ||
return user ? user.toJS() : null; | ||
} | ||
}, { | ||
key: 'impersonate', | ||
value: function () { | ||
var _ref5 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5() { | ||
var _ref5 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5(username) { | ||
var _ref6, accessToken, refreshToken, res; | ||
return _regenerator2.default.wrap(function _callee5$(_context5) { | ||
@@ -315,8 +321,144 @@ while (1) { | ||
case 0: | ||
if ((0, _lodash.isString)(username)) { | ||
_context5.next = 2; | ||
break; | ||
} | ||
throw new _common.AccountsError('Username is required'); | ||
case 2: | ||
if (!this.isImpersonated()) { | ||
_context5.next = 4; | ||
break; | ||
} | ||
throw new _common.AccountsError('User already impersonating'); | ||
case 4: | ||
_context5.next = 6; | ||
return this.tokens(); | ||
case 6: | ||
_ref6 = _context5.sent; | ||
accessToken = _ref6.accessToken; | ||
refreshToken = _ref6.refreshToken; | ||
if (accessToken) { | ||
_context5.next = 11; | ||
break; | ||
} | ||
throw new _common.AccountsError('There is no access tokens available'); | ||
case 11: | ||
_context5.next = 13; | ||
return this.transport.impersonate(accessToken, username); | ||
case 13: | ||
res = _context5.sent; | ||
if (res.authorized) { | ||
_context5.next = 18; | ||
break; | ||
} | ||
throw new _common.AccountsError('User unauthorized to impersonate ' + username); | ||
case 18: | ||
this.store.dispatch((0, _module.setImpersonated)(true)); | ||
this.store.dispatch((0, _module.setOriginalTokens)({ accessToken: accessToken, refreshToken: refreshToken })); | ||
_context5.next = 22; | ||
return this.storeTokens(res.tokens); | ||
case 22: | ||
this.store.dispatch((0, _module.setTokens)(res.tokens)); | ||
this.store.dispatch((0, _module.setUser)(res.user)); | ||
return _context5.abrupt('return', res); | ||
case 25: | ||
case 'end': | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee5, this); | ||
})); | ||
function impersonate(_x5) { | ||
return _ref5.apply(this, arguments); | ||
} | ||
return impersonate; | ||
}() | ||
}, { | ||
key: 'stopImpersonation', | ||
value: function () { | ||
var _ref7 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee6() { | ||
return _regenerator2.default.wrap(function _callee6$(_context6) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
if (!this.isImpersonated()) { | ||
_context6.next = 6; | ||
break; | ||
} | ||
this.store.dispatch((0, _module.setTokens)(this.originalTokens())); | ||
this.store.dispatch((0, _module.clearOriginalTokens)()); | ||
this.store.dispatch((0, _module.setImpersonated)(false)); | ||
_context6.next = 6; | ||
return this.refreshSession(); | ||
case 6: | ||
case 'end': | ||
return _context6.stop(); | ||
} | ||
} | ||
}, _callee6, this); | ||
})); | ||
function stopImpersonation() { | ||
return _ref7.apply(this, arguments); | ||
} | ||
return stopImpersonation; | ||
}() | ||
}, { | ||
key: 'isImpersonated', | ||
value: function isImpersonated() { | ||
return this.getState().get('isImpersonated'); | ||
} | ||
}, { | ||
key: 'originalTokens', | ||
value: function originalTokens() { | ||
var tokens = this.getState().get('originalTokens'); | ||
return tokens ? tokens.toJS() : { | ||
accessToken: null, | ||
refreshToken: null | ||
}; | ||
} | ||
}, { | ||
key: 'tokens', | ||
value: function tokens() { | ||
var tokens = this.getState().get('tokens'); | ||
return tokens ? tokens.toJS() : { | ||
accessToken: null, | ||
refreshToken: null | ||
}; | ||
} | ||
}, { | ||
key: 'clearTokens', | ||
value: function () { | ||
var _ref8 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee7() { | ||
return _regenerator2.default.wrap(function _callee7$(_context7) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
case 0: | ||
this.store.dispatch((0, _module.clearTokens)()); | ||
_context5.next = 3; | ||
_context7.next = 3; | ||
return this.removeStorageData(getTokenKey(ACCESS_TOKEN, this.options)); | ||
case 3: | ||
_context5.next = 5; | ||
_context7.next = 5; | ||
return this.removeStorageData(getTokenKey(REFRESH_TOKEN, this.options)); | ||
@@ -326,10 +468,10 @@ | ||
case 'end': | ||
return _context5.stop(); | ||
return _context7.stop(); | ||
} | ||
} | ||
}, _callee5, this); | ||
}, _callee7, this); | ||
})); | ||
function clearTokens() { | ||
return _ref5.apply(this, arguments); | ||
return _ref8.apply(this, arguments); | ||
} | ||
@@ -342,39 +484,44 @@ | ||
value: function () { | ||
var _ref6 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee6(loginResponse) { | ||
var _ref9 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee8(tokens) { | ||
var newAccessToken, newRefreshToken; | ||
return _regenerator2.default.wrap(function _callee6$(_context6) { | ||
return _regenerator2.default.wrap(function _callee8$(_context8) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
switch (_context8.prev = _context8.next) { | ||
case 0: | ||
newAccessToken = loginResponse.tokens.accessToken; | ||
if (!tokens) { | ||
_context8.next = 9; | ||
break; | ||
} | ||
newAccessToken = tokens.accessToken; | ||
if (!newAccessToken) { | ||
_context6.next = 4; | ||
_context8.next = 5; | ||
break; | ||
} | ||
_context6.next = 4; | ||
_context8.next = 5; | ||
return this.setStorageData(getTokenKey(ACCESS_TOKEN, this.options), newAccessToken); | ||
case 4: | ||
newRefreshToken = loginResponse.tokens.refreshToken; | ||
case 5: | ||
newRefreshToken = tokens.refreshToken; | ||
if (!newRefreshToken) { | ||
_context6.next = 8; | ||
_context8.next = 9; | ||
break; | ||
} | ||
_context6.next = 8; | ||
_context8.next = 9; | ||
return this.setStorageData(getTokenKey(REFRESH_TOKEN, this.options), newRefreshToken); | ||
case 8: | ||
case 9: | ||
case 'end': | ||
return _context6.stop(); | ||
return _context8.stop(); | ||
} | ||
} | ||
}, _callee6, this); | ||
}, _callee8, this); | ||
})); | ||
function storeTokens(_x5) { | ||
return _ref6.apply(this, arguments); | ||
function storeTokens(_x6) { | ||
return _ref9.apply(this, arguments); | ||
} | ||
@@ -398,23 +545,23 @@ | ||
value: function () { | ||
var _ref7 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee7() { | ||
var _ref8, accessToken, refreshToken, decodedRefreshToken, currentTime, refreshedSession; | ||
var _ref10 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee9() { | ||
var _ref11, accessToken, refreshToken, decodedRefreshToken, currentTime, refreshedSession; | ||
return _regenerator2.default.wrap(function _callee7$(_context7) { | ||
return _regenerator2.default.wrap(function _callee9$(_context9) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
switch (_context9.prev = _context9.next) { | ||
case 0: | ||
_context7.next = 2; | ||
_context9.next = 2; | ||
return this.tokens(); | ||
case 2: | ||
_ref8 = _context7.sent; | ||
accessToken = _ref8.accessToken; | ||
refreshToken = _ref8.refreshToken; | ||
_ref11 = _context9.sent; | ||
accessToken = _ref11.accessToken; | ||
refreshToken = _ref11.refreshToken; | ||
if (!(accessToken && refreshToken)) { | ||
_context7.next = 33; | ||
_context9.next = 33; | ||
break; | ||
} | ||
_context7.prev = 6; | ||
_context9.prev = 6; | ||
@@ -427,3 +574,3 @@ this.store.dispatch((0, _module.loggingIn)(true)); | ||
if (!(decodedRefreshToken.exp < currentTime)) { | ||
_context7.next = 15; | ||
_context9.next = 15; | ||
break; | ||
@@ -434,16 +581,16 @@ } | ||
this.clearUser(); | ||
_context7.next = 23; | ||
_context9.next = 23; | ||
break; | ||
case 15: | ||
_context7.next = 17; | ||
_context9.next = 17; | ||
return this.transport.refreshTokens(accessToken, refreshToken); | ||
case 17: | ||
refreshedSession = _context7.sent; | ||
refreshedSession = _context9.sent; | ||
this.store.dispatch((0, _module.loggingIn)(false)); | ||
_context7.next = 21; | ||
return this.storeTokens(refreshedSession); | ||
_context9.next = 21; | ||
return this.storeTokens(refreshedSession.tokens); | ||
@@ -455,8 +602,8 @@ case 21: | ||
case 23: | ||
_context7.next = 31; | ||
_context9.next = 31; | ||
break; | ||
case 25: | ||
_context7.prev = 25; | ||
_context7.t0 = _context7['catch'](6); | ||
_context9.prev = 25; | ||
_context9.t0 = _context9['catch'](6); | ||
@@ -469,3 +616,3 @@ this.store.dispatch((0, _module.loggingIn)(false)); | ||
case 31: | ||
_context7.next = 36; | ||
_context9.next = 36; | ||
break; | ||
@@ -480,10 +627,10 @@ | ||
case 'end': | ||
return _context7.stop(); | ||
return _context9.stop(); | ||
} | ||
} | ||
}, _callee7, this, [[6, 25]]); | ||
}, _callee9, this, [[6, 25]]); | ||
})); | ||
function refreshSession() { | ||
return _ref7.apply(this, arguments); | ||
return _ref10.apply(this, arguments); | ||
} | ||
@@ -496,10 +643,10 @@ | ||
value: function () { | ||
var _ref9 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee8(user, callback) { | ||
var userId; | ||
return _regenerator2.default.wrap(function _callee8$(_context8) { | ||
var _ref12 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee10(user, callback) { | ||
var hashAlgorithm, password, userToCreate, userId; | ||
return _regenerator2.default.wrap(function _callee10$(_context10) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
switch (_context10.prev = _context10.next) { | ||
case 0: | ||
if (!(!user || user.password === undefined)) { | ||
_context8.next = 2; | ||
_context10.next = 2; | ||
break; | ||
@@ -514,4 +661,4 @@ } | ||
case 2: | ||
if (_common.validators.validatePassword(user.password)) { | ||
_context8.next = 4; | ||
if (!(!user.password || (0, _lodash.isString)(user.password) && !_common.validators.validatePassword(user.password))) { | ||
_context10.next = 4; | ||
break; | ||
@@ -524,3 +671,3 @@ } | ||
if (!(!_common.validators.validateUsername(user.username) && !_common.validators.validateEmail(user.email))) { | ||
_context8.next = 6; | ||
_context10.next = 6; | ||
break; | ||
@@ -532,8 +679,11 @@ } | ||
case 6: | ||
_context8.prev = 6; | ||
_context8.next = 9; | ||
return this.transport.createUser(user); | ||
hashAlgorithm = this.options.passwordHashAlgorithm; | ||
password = user.password && hashAlgorithm ? (0, _encryption.hashPassword)(user.password, hashAlgorithm) : user.password; | ||
userToCreate = (0, _extends3.default)({}, user, { password: password }); | ||
_context10.prev = 9; | ||
_context10.next = 12; | ||
return this.transport.createUser(userToCreate); | ||
case 9: | ||
userId = _context8.sent; | ||
case 12: | ||
userId = _context10.sent; | ||
@@ -543,28 +693,29 @@ if (callback && (0, _lodash.isFunction)(callback)) { | ||
} | ||
_context8.next = 13; | ||
_context10.next = 16; | ||
return this.loginWithPassword({ id: userId }, user.password); | ||
case 13: | ||
_context8.next = 19; | ||
case 16: | ||
_context10.next = 22; | ||
break; | ||
case 15: | ||
_context8.prev = 15; | ||
_context8.t0 = _context8['catch'](6); | ||
case 18: | ||
_context10.prev = 18; | ||
_context10.t0 = _context10['catch'](9); | ||
if (callback && (0, _lodash.isFunction)(callback)) { | ||
callback(_context8.t0); | ||
callback(_context10.t0); | ||
} | ||
throw new _common.AccountsError(_context8.t0.message); | ||
throw new _common.AccountsError(_context10.t0.message); | ||
case 19: | ||
case 22: | ||
case 'end': | ||
return _context8.stop(); | ||
return _context10.stop(); | ||
} | ||
} | ||
}, _callee8, this, [[6, 15]]); | ||
}, _callee10, this, [[9, 18]]); | ||
})); | ||
function createUser(_x6, _x7) { | ||
return _ref9.apply(this, arguments); | ||
function createUser(_x7, _x8) { | ||
return _ref12.apply(this, arguments); | ||
} | ||
@@ -577,10 +728,10 @@ | ||
value: function () { | ||
var _ref10 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee9(user, password, callback) { | ||
var _ref13 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee11(user, password, callback) { | ||
var hashAlgorithm, pass, res; | ||
return _regenerator2.default.wrap(function _callee9$(_context9) { | ||
return _regenerator2.default.wrap(function _callee11$(_context11) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
switch (_context11.prev = _context11.next) { | ||
case 0: | ||
if (!(!password || !user)) { | ||
_context9.next = 2; | ||
_context11.next = 2; | ||
break; | ||
@@ -593,3 +744,3 @@ } | ||
if (!(!(0, _lodash.isString)(user) && !isValidUserObject(user) || !(0, _lodash.isString)(password))) { | ||
_context9.next = 4; | ||
_context11.next = 4; | ||
break; | ||
@@ -603,15 +754,15 @@ } | ||
this.store.dispatch((0, _module.loggingIn)(true)); | ||
_context9.prev = 5; | ||
_context11.prev = 5; | ||
hashAlgorithm = this.options.passwordHashAlgorithm; | ||
pass = hashAlgorithm ? (0, _encryption.hashPassword)(password, hashAlgorithm) : password; | ||
_context9.next = 10; | ||
_context11.next = 10; | ||
return this.transport.loginWithPassword(user, pass); | ||
case 10: | ||
res = _context9.sent; | ||
res = _context11.sent; | ||
this.store.dispatch((0, _module.loggingIn)(false)); | ||
_context9.next = 14; | ||
return this.storeTokens(res); | ||
_context11.next = 14; | ||
return this.storeTokens(res.tokens); | ||
@@ -630,24 +781,24 @@ case 14: | ||
return _context9.abrupt('return', res); | ||
return _context11.abrupt('return', res); | ||
case 21: | ||
_context9.prev = 21; | ||
_context9.t0 = _context9['catch'](5); | ||
_context11.prev = 21; | ||
_context11.t0 = _context11['catch'](5); | ||
this.store.dispatch((0, _module.loggingIn)(false)); | ||
if (callback && (0, _lodash.isFunction)(callback)) { | ||
callback(_context9.t0, null); | ||
callback(_context11.t0, null); | ||
} | ||
throw new _common.AccountsError(_context9.t0.message); | ||
throw new _common.AccountsError(_context11.t0.message); | ||
case 26: | ||
case 'end': | ||
return _context9.stop(); | ||
return _context11.stop(); | ||
} | ||
} | ||
}, _callee9, this, [[5, 21]]); | ||
}, _callee11, this, [[5, 21]]); | ||
})); | ||
function loginWithPassword(_x8, _x9, _x10) { | ||
return _ref10.apply(this, arguments); | ||
function loginWithPassword(_x9, _x10, _x11) { | ||
return _ref13.apply(this, arguments); | ||
} | ||
@@ -670,23 +821,23 @@ | ||
value: function () { | ||
var _ref11 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee10(callback) { | ||
var _ref12, accessToken; | ||
var _ref14 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee12(callback) { | ||
var _ref15, accessToken; | ||
return _regenerator2.default.wrap(function _callee10$(_context10) { | ||
return _regenerator2.default.wrap(function _callee12$(_context12) { | ||
while (1) { | ||
switch (_context10.prev = _context10.next) { | ||
switch (_context12.prev = _context12.next) { | ||
case 0: | ||
_context10.prev = 0; | ||
_context10.next = 3; | ||
_context12.prev = 0; | ||
_context12.next = 3; | ||
return this.tokens(); | ||
case 3: | ||
_ref12 = _context10.sent; | ||
accessToken = _ref12.accessToken; | ||
_ref15 = _context12.sent; | ||
accessToken = _ref15.accessToken; | ||
if (!accessToken) { | ||
_context10.next = 8; | ||
_context12.next = 8; | ||
break; | ||
} | ||
_context10.next = 8; | ||
_context12.next = 8; | ||
return this.transport.logout(accessToken); | ||
@@ -705,24 +856,26 @@ | ||
} | ||
_context10.next = 18; | ||
_context12.next = 20; | ||
break; | ||
case 14: | ||
_context10.prev = 14; | ||
_context10.t0 = _context10['catch'](0); | ||
_context12.prev = 14; | ||
_context12.t0 = _context12['catch'](0); | ||
this.clearTokens(); | ||
this.store.dispatch((0, _module.clearUser)()); | ||
if (callback && (0, _lodash.isFunction)(callback)) { | ||
callback(_context10.t0); | ||
callback(_context12.t0); | ||
} | ||
throw new _common.AccountsError(_context10.t0.message); | ||
throw new _common.AccountsError(_context12.t0.message); | ||
case 18: | ||
case 20: | ||
case 'end': | ||
return _context10.stop(); | ||
return _context12.stop(); | ||
} | ||
} | ||
}, _callee10, this, [[0, 14]]); | ||
}, _callee12, this, [[0, 14]]); | ||
})); | ||
function logout(_x11) { | ||
return _ref11.apply(this, arguments); | ||
function logout(_x12) { | ||
return _ref14.apply(this, arguments); | ||
} | ||
@@ -735,30 +888,30 @@ | ||
value: function () { | ||
var _ref13 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee11(token) { | ||
return _regenerator2.default.wrap(function _callee11$(_context11) { | ||
var _ref16 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee13(token) { | ||
return _regenerator2.default.wrap(function _callee13$(_context13) { | ||
while (1) { | ||
switch (_context11.prev = _context11.next) { | ||
switch (_context13.prev = _context13.next) { | ||
case 0: | ||
_context11.prev = 0; | ||
_context11.next = 3; | ||
_context13.prev = 0; | ||
_context13.next = 3; | ||
return this.transport.verifyEmail(token); | ||
case 3: | ||
_context11.next = 8; | ||
_context13.next = 8; | ||
break; | ||
case 5: | ||
_context11.prev = 5; | ||
_context11.t0 = _context11['catch'](0); | ||
throw new _common.AccountsError(_context11.t0.message); | ||
_context13.prev = 5; | ||
_context13.t0 = _context13['catch'](0); | ||
throw new _common.AccountsError(_context13.t0.message); | ||
case 8: | ||
case 'end': | ||
return _context11.stop(); | ||
return _context13.stop(); | ||
} | ||
} | ||
}, _callee11, this, [[0, 5]]); | ||
}, _callee13, this, [[0, 5]]); | ||
})); | ||
function verifyEmail(_x12) { | ||
return _ref13.apply(this, arguments); | ||
function verifyEmail(_x13) { | ||
return _ref16.apply(this, arguments); | ||
} | ||
@@ -771,30 +924,41 @@ | ||
value: function () { | ||
var _ref14 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee12(token, newPassword) { | ||
return _regenerator2.default.wrap(function _callee12$(_context12) { | ||
var _ref17 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee14(token, newPassword) { | ||
var hashAlgorithm, password; | ||
return _regenerator2.default.wrap(function _callee14$(_context14) { | ||
while (1) { | ||
switch (_context12.prev = _context12.next) { | ||
switch (_context14.prev = _context14.next) { | ||
case 0: | ||
_context12.prev = 0; | ||
_context12.next = 3; | ||
return this.transport.resetPassword(token, newPassword); | ||
if (_common.validators.validatePassword(newPassword)) { | ||
_context14.next = 2; | ||
break; | ||
} | ||
case 3: | ||
_context12.next = 8; | ||
throw new _common.AccountsError('Password is invalid!'); | ||
case 2: | ||
hashAlgorithm = this.options.passwordHashAlgorithm; | ||
password = hashAlgorithm ? (0, _encryption.hashPassword)(newPassword, hashAlgorithm) : newPassword; | ||
_context14.prev = 4; | ||
_context14.next = 7; | ||
return this.transport.resetPassword(token, password); | ||
case 7: | ||
_context14.next = 12; | ||
break; | ||
case 5: | ||
_context12.prev = 5; | ||
_context12.t0 = _context12['catch'](0); | ||
throw new _common.AccountsError(_context12.t0.message); | ||
case 9: | ||
_context14.prev = 9; | ||
_context14.t0 = _context14['catch'](4); | ||
throw new _common.AccountsError(_context14.t0.message); | ||
case 8: | ||
case 12: | ||
case 'end': | ||
return _context12.stop(); | ||
return _context14.stop(); | ||
} | ||
} | ||
}, _callee12, this, [[0, 5]]); | ||
}, _callee14, this, [[4, 9]]); | ||
})); | ||
function resetPassword(_x13, _x14) { | ||
return _ref14.apply(this, arguments); | ||
function resetPassword(_x14, _x15) { | ||
return _ref17.apply(this, arguments); | ||
} | ||
@@ -807,9 +971,9 @@ | ||
value: function () { | ||
var _ref15 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee13(email) { | ||
return _regenerator2.default.wrap(function _callee13$(_context13) { | ||
var _ref18 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee15(email) { | ||
return _regenerator2.default.wrap(function _callee15$(_context15) { | ||
while (1) { | ||
switch (_context13.prev = _context13.next) { | ||
switch (_context15.prev = _context15.next) { | ||
case 0: | ||
if (_common.validators.validateEmail(email)) { | ||
_context13.next = 2; | ||
_context15.next = 2; | ||
break; | ||
@@ -821,25 +985,25 @@ } | ||
case 2: | ||
_context13.prev = 2; | ||
_context13.next = 5; | ||
_context15.prev = 2; | ||
_context15.next = 5; | ||
return this.transport.sendResetPasswordEmail(email); | ||
case 5: | ||
_context13.next = 10; | ||
_context15.next = 10; | ||
break; | ||
case 7: | ||
_context13.prev = 7; | ||
_context13.t0 = _context13['catch'](2); | ||
throw new _common.AccountsError(_context13.t0.message); | ||
_context15.prev = 7; | ||
_context15.t0 = _context15['catch'](2); | ||
throw new _common.AccountsError(_context15.t0.message); | ||
case 10: | ||
case 'end': | ||
return _context13.stop(); | ||
return _context15.stop(); | ||
} | ||
} | ||
}, _callee13, this, [[2, 7]]); | ||
}, _callee15, this, [[2, 7]]); | ||
})); | ||
function requestPasswordReset(_x15) { | ||
return _ref15.apply(this, arguments); | ||
function requestPasswordReset(_x16) { | ||
return _ref18.apply(this, arguments); | ||
} | ||
@@ -852,9 +1016,9 @@ | ||
value: function () { | ||
var _ref16 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee14(email) { | ||
return _regenerator2.default.wrap(function _callee14$(_context14) { | ||
var _ref19 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee16(email) { | ||
return _regenerator2.default.wrap(function _callee16$(_context16) { | ||
while (1) { | ||
switch (_context14.prev = _context14.next) { | ||
switch (_context16.prev = _context16.next) { | ||
case 0: | ||
if (_common.validators.validateEmail(email)) { | ||
_context14.next = 2; | ||
_context16.next = 2; | ||
break; | ||
@@ -866,25 +1030,25 @@ } | ||
case 2: | ||
_context14.prev = 2; | ||
_context14.next = 5; | ||
_context16.prev = 2; | ||
_context16.next = 5; | ||
return this.transport.sendVerificationEmail(email); | ||
case 5: | ||
_context14.next = 10; | ||
_context16.next = 10; | ||
break; | ||
case 7: | ||
_context14.prev = 7; | ||
_context14.t0 = _context14['catch'](2); | ||
throw new _common.AccountsError(_context14.t0.message); | ||
_context16.prev = 7; | ||
_context16.t0 = _context16['catch'](2); | ||
throw new _common.AccountsError(_context16.t0.message); | ||
case 10: | ||
case 'end': | ||
return _context14.stop(); | ||
return _context16.stop(); | ||
} | ||
} | ||
}, _callee14, this, [[2, 7]]); | ||
}, _callee16, this, [[2, 7]]); | ||
})); | ||
function requestVerificationEmail(_x16) { | ||
return _ref16.apply(this, arguments); | ||
function requestVerificationEmail(_x17) { | ||
return _ref19.apply(this, arguments); | ||
} | ||
@@ -902,3 +1066,22 @@ | ||
config: function config(options, transport) { | ||
this.instance = new AccountsClient((0, _extends3.default)({}, _config3.default, options), transport); | ||
var _this = this; | ||
return (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee17() { | ||
return _regenerator2.default.wrap(function _callee17$(_context17) { | ||
while (1) { | ||
switch (_context17.prev = _context17.next) { | ||
case 0: | ||
_this.instance = new AccountsClient((0, _extends3.default)({}, _config3.default, options), transport); | ||
return _context17.abrupt('return', _this.instance.loadTokensFromStorage().then(function () { | ||
return _this.instance; | ||
})); | ||
case 2: | ||
case 'end': | ||
return _context17.stop(); | ||
} | ||
} | ||
}, _callee17, _this); | ||
}))(); | ||
}, | ||
@@ -946,2 +1129,14 @@ user: function user() { | ||
return this.instance.requestVerificationEmail(email); | ||
}, | ||
impersonate: function impersonate(username) { | ||
return this.instance.impersonate(username); | ||
}, | ||
stopImpersonation: function stopImpersonation() { | ||
return this.instance.stopImpersonation(); | ||
}, | ||
isImpersonated: function isImpersonated() { | ||
return this.instance.isImpersonated(); | ||
}, | ||
originalTokens: function originalTokens() { | ||
return this.instance.originalTokens(); | ||
} | ||
@@ -1175,3 +1370,3 @@ }; | ||
}); | ||
exports.clearUser = exports.clearTokens = exports.setTokens = exports.setUser = exports.loggingIn = undefined; | ||
exports.setImpersonated = exports.clearOriginalTokens = exports.setOriginalTokens = exports.clearUser = exports.clearTokens = exports.setTokens = exports.setUser = exports.loggingIn = undefined; | ||
@@ -1187,2 +1382,5 @@ var _immutable = __webpack_require__(17); | ||
var LOGGING_IN = PATH + 'LOGGING_IN'; | ||
var SET_ORIGINAL_TOKENS = PATH + 'SET_ORIGINAL_TOKENS'; | ||
var CLEAR_ORIGINAL_TOKENS = PATH + 'CLEAR_ORIGINAL_TOKENS'; | ||
var SET_IMPERSONATED = PATH + 'SET_IMPERSONATED'; | ||
@@ -1193,3 +1391,5 @@ var initialState = (0, _immutable.Map)({ | ||
tokens: null, | ||
loggingIn: false | ||
loggingIn: false, | ||
originalTokens: null, | ||
isImpersonated: false | ||
}); | ||
@@ -1221,2 +1421,3 @@ | ||
{ | ||
state.set('originalTokens', null); | ||
return state.set('tokens', null); | ||
@@ -1234,2 +1435,22 @@ } | ||
} | ||
case SET_ORIGINAL_TOKENS: | ||
{ | ||
var _tokens = action.payload.tokens; | ||
return state.set('originalTokens', (0, _immutable.Map)(_tokens)); | ||
} | ||
case CLEAR_ORIGINAL_TOKENS: | ||
{ | ||
return state.set('originalTokens', null); | ||
} | ||
case SET_IMPERSONATED: | ||
{ | ||
var isImpersonated = action.payload.isImpersonated; | ||
return state.set('isImpersonated', isImpersonated); | ||
} | ||
default: | ||
@@ -1281,2 +1502,26 @@ break; | ||
var setOriginalTokens = exports.setOriginalTokens = function setOriginalTokens(tokens) { | ||
return { | ||
type: SET_ORIGINAL_TOKENS, | ||
payload: { | ||
tokens: tokens | ||
} | ||
}; | ||
}; | ||
var clearOriginalTokens = exports.clearOriginalTokens = function clearOriginalTokens() { | ||
return { | ||
type: CLEAR_ORIGINAL_TOKENS | ||
}; | ||
}; | ||
var setImpersonated = exports.setImpersonated = function setImpersonated(isImpersonated) { | ||
return { | ||
type: SET_IMPERSONATED, | ||
payload: { | ||
isImpersonated: isImpersonated | ||
} | ||
}; | ||
}; | ||
/***/ }, | ||
@@ -1293,4 +1538,8 @@ /* 20 */ | ||
var _cryptoJs = __webpack_require__(21); | ||
var _isString = __webpack_require__(21); | ||
var _isString2 = _interopRequireDefault(_isString); | ||
var _cryptoJs = __webpack_require__(22); | ||
var _cryptoJs2 = _interopRequireDefault(_cryptoJs); | ||
@@ -1310,7 +1559,14 @@ | ||
}; | ||
var hashPassword = exports.hashPassword = function hashPassword(password, algorithm) { | ||
if ((0, _isString2.default)(password)) { | ||
var cryptoAlgoKey = mapHashConstant[algorithm]; | ||
var cryptoFunction = _cryptoJs2.default[cryptoAlgoKey]; | ||
return { | ||
digest: cryptoFunction(password).toString(), | ||
algorithm: algorithm | ||
}; | ||
} | ||
var hashPassword = exports.hashPassword = function hashPassword(password, algorithm) { | ||
var cryptoAlgoKey = mapHashConstant[algorithm]; | ||
var cryptoFunction = _cryptoJs2.default[cryptoAlgoKey]; | ||
return cryptoFunction(password).toString(); | ||
// Prehashed password object | ||
return password; | ||
}; | ||
@@ -1322,2 +1578,8 @@ | ||
module.exports = require("lodash/isString"); | ||
/***/ }, | ||
/* 22 */ | ||
/***/ function(module, exports) { | ||
module.exports = require("crypto-js"); | ||
@@ -1324,0 +1586,0 @@ |
{ | ||
"name": "@accounts/client", | ||
"version": "0.0.10-alpha.18d76699", | ||
"version": "0.0.10-alpha.359b8b92", | ||
"description": "Fullstack authentication and accounts-management", | ||
@@ -65,3 +65,3 @@ "main": "lib/index.js", | ||
"coveralls": "^2.11.14", | ||
"flow-bin": "^0.39.0", | ||
"flow-bin": "0.41.0", | ||
"jest": "^18.0.0", | ||
@@ -75,3 +75,3 @@ "lerna": "2.0.0-beta.32", | ||
"dependencies": { | ||
"@accounts/common": "^0.0.10-alpha.18d76699", | ||
"@accounts/common": "^0.0.10-alpha.359b8b92", | ||
"crypto-js": "^3.1.9-1", | ||
@@ -78,0 +78,0 @@ "immutable": "^3.8.1", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
366731
6760