@accounts/client
Advanced tools
Comparing version 0.0.10 to 0.0.11
@@ -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,9 +193,26 @@ return _context4.stop(); | ||
function tokens() { | ||
function loadTokensFromStorage() { | ||
return _ref4.apply(this, arguments); | ||
} | ||
return tokens; | ||
return loadTokensFromStorage; | ||
}() | ||
}, { | ||
key: 'user', | ||
value: function user() { | ||
var user = this.getState().get('user'); | ||
return user ? user.toJS() : null; | ||
} | ||
}, { | ||
key: 'tokens', | ||
value: function tokens() { | ||
var tokens = this.getState().get('tokens'); | ||
return tokens ? tokens.toJS() : { | ||
accessToken: null, | ||
refreshToken: null | ||
}; | ||
} | ||
}, { | ||
key: 'clearTokens', | ||
@@ -773,3 +787,22 @@ value: function () { | ||
config: function config(options, transport) { | ||
this.instance = new AccountsClient(_extends({}, _config3.default, options), transport); | ||
var _this = this; | ||
return _asyncToGenerator(regeneratorRuntime.mark(function _callee15() { | ||
return regeneratorRuntime.wrap(function _callee15$(_context15) { | ||
while (1) { | ||
switch (_context15.prev = _context15.next) { | ||
case 0: | ||
_this.instance = new AccountsClient(_extends({}, _config3.default, options), transport); | ||
return _context15.abrupt('return', _this.instance.loadTokensFromStorage().then(function () { | ||
return _this.instance; | ||
})); | ||
case 2: | ||
case 'end': | ||
return _context15.stop(); | ||
} | ||
} | ||
}, _callee15, _this); | ||
}))(); | ||
}, | ||
@@ -776,0 +809,0 @@ user: function user() { |
@@ -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,12 +153,12 @@ 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: | ||
@@ -119,4 +170,4 @@ _AccountsClient2.default.config({ | ||
}); | ||
_context2.prev = 1; | ||
_context2.next = 4; | ||
_context4.prev = 1; | ||
_context4.next = 4; | ||
return _AccountsClient2.default.createUser({ | ||
@@ -130,5 +181,5 @@ password: null | ||
case 7: | ||
_context2.prev = 7; | ||
_context2.t0 = _context2['catch'](1); | ||
message = _context2.t0.message; | ||
_context4.prev = 7; | ||
_context4.t0 = _context4['catch'](1); | ||
message = _context4.t0.message; | ||
@@ -139,12 +190,12 @@ expect(message).toEqual('Password is required'); | ||
case 'end': | ||
return _context2.stop(); | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee2, undefined, [[1, 7]]); | ||
}, _callee4, undefined, [[1, 7]]); | ||
}))); | ||
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: | ||
@@ -154,4 +205,4 @@ _AccountsClient2.default.config({ history: history }, { | ||
}); | ||
_context3.prev = 1; | ||
_context3.next = 4; | ||
_context5.prev = 1; | ||
_context5.next = 4; | ||
return _AccountsClient2.default.createUser({ | ||
@@ -167,5 +218,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; | ||
@@ -176,12 +227,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: | ||
@@ -197,5 +248,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({ | ||
@@ -206,18 +259,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: | ||
@@ -229,9 +282,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({ | ||
@@ -242,8 +295,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); | ||
@@ -253,14 +306,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: | ||
@@ -279,3 +332,3 @@ transport = { | ||
_context6.next = 5; | ||
_context8.next = 5; | ||
return _AccountsClient2.default.createUser({ | ||
@@ -293,14 +346,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: | ||
@@ -314,4 +367,4 @@ transport = { | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
_context7.prev = 2; | ||
_context7.next = 5; | ||
_context9.prev = 2; | ||
_context9.next = 5; | ||
return _AccountsClient2.default.loginWithPassword(); | ||
@@ -323,5 +376,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; | ||
@@ -332,12 +385,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: | ||
@@ -351,4 +404,4 @@ transport = { | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
_context8.prev = 2; | ||
_context8.next = 5; | ||
_context10.prev = 2; | ||
_context10.next = 5; | ||
return _AccountsClient2.default.loginWithPassword(); | ||
@@ -360,5 +413,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; | ||
@@ -369,12 +422,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: | ||
@@ -388,4 +441,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'); | ||
@@ -397,5 +450,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; | ||
@@ -406,12 +459,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: | ||
@@ -425,4 +478,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' }, {}); | ||
@@ -434,5 +487,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; | ||
@@ -443,12 +496,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: | ||
@@ -463,3 +516,3 @@ transport = { | ||
callback = jest.fn(); | ||
_context11.next = 5; | ||
_context13.next = 5; | ||
return _AccountsClient2.default.loginWithPassword('username', 'password', callback); | ||
@@ -473,12 +526,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: | ||
@@ -493,3 +546,3 @@ loginWithPassword = jest.fn(function () { | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
_context12.next = 5; | ||
_context14.next = 5; | ||
return _AccountsClient2.default.loginWithPassword('username', 'password'); | ||
@@ -504,12 +557,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: | ||
@@ -524,4 +577,4 @@ transport = { | ||
callback = jest.fn(); | ||
_context13.prev = 3; | ||
_context13.next = 6; | ||
_context15.prev = 3; | ||
_context15.next = 6; | ||
return _AccountsClient2.default.loginWithPassword('username', 'password', callback); | ||
@@ -533,4 +586,4 @@ | ||
case 9: | ||
_context13.prev = 9; | ||
_context13.t0 = _context13['catch'](3); | ||
_context15.prev = 9; | ||
_context15.t0 = _context15['catch'](3); | ||
@@ -542,12 +595,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: | ||
@@ -561,4 +614,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'); | ||
@@ -570,4 +623,4 @@ | ||
case 8: | ||
_context14.prev = 8; | ||
_context14.t0 = _context14['catch'](2); | ||
_context16.prev = 8; | ||
_context16.t0 = _context16['catch'](2); | ||
@@ -578,12 +631,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: | ||
@@ -597,3 +650,3 @@ transport = { | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
_context15.next = 4; | ||
_context17.next = 4; | ||
return _AccountsClient2.default.loginWithPassword('username', 'password'); | ||
@@ -607,13 +660,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: | ||
@@ -625,5 +678,4 @@ transport = { | ||
}; | ||
_AccountsClient2.default.config({ | ||
_context18.next = 3; | ||
return _AccountsClient2.default.config({ | ||
history: history, | ||
@@ -637,63 +689,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: | ||
@@ -707,3 +716,3 @@ transport = { | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
_context18.next = 4; | ||
_context19.next = 4; | ||
return _AccountsClient2.default.loginWithPassword('username', 'password'); | ||
@@ -716,12 +725,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: | ||
@@ -735,3 +744,3 @@ transport = { | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
_context19.next = 4; | ||
_context20.next = 4; | ||
return _AccountsClient2.default.loginWithPassword('username', 'password'); | ||
@@ -744,13 +753,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() { | ||
it('can hash password with specified algorithm', _asyncToGenerator(regeneratorRuntime.mark(function _callee21() { | ||
var loginWithPassword, transport, hashDigest; | ||
return regeneratorRuntime.wrap(function _callee20$(_context20) { | ||
return regeneratorRuntime.wrap(function _callee21$(_context21) { | ||
while (1) { | ||
switch (_context20.prev = _context20.next) { | ||
switch (_context21.prev = _context21.next) { | ||
case 0: | ||
@@ -769,3 +778,3 @@ loginWithPassword = jest.fn(function () { | ||
hashDigest = _crypto2.default.createHash('sha256').update('password').digest('hex'); | ||
_context20.next = 6; | ||
_context21.next = 6; | ||
return _AccountsClient2.default.loginWithPassword('username', 'password'); | ||
@@ -780,14 +789,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 +811,3 @@ transport = { | ||
callback = jest.fn(); | ||
_context21.next = 5; | ||
_context22.next = 5; | ||
return _AccountsClient2.default.logout(callback); | ||
@@ -811,12 +820,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 +840,3 @@ onSignedOutHook = jest.fn(); | ||
_AccountsClient2.default.config({ history: history, onSignedOutHook: onSignedOutHook }, transport); | ||
_context22.next = 5; | ||
_context23.next = 5; | ||
return _AccountsClient2.default.logout(); | ||
@@ -840,13 +849,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 +867,39 @@ transport = { | ||
}; | ||
_context24.next = 3; | ||
return _AccountsClient2.default.instance.storeTokens({ tokens: { 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: | ||
@@ -901,4 +915,4 @@ transport = { | ||
callback = jest.fn(); | ||
_context24.prev = 4; | ||
_context24.next = 7; | ||
_context25.prev = 4; | ||
_context25.next = 7; | ||
return _AccountsClient2.default.logout(callback); | ||
@@ -910,4 +924,4 @@ | ||
case 10: | ||
_context24.prev = 10; | ||
_context24.t0 = _context24['catch'](4); | ||
_context25.prev = 10; | ||
_context25.t0 = _context25['catch'](4); | ||
@@ -918,12 +932,12 @@ expect(_AccountsClient2.default.instance.getState().get('tokens')).toEqual(null); | ||
case 'end': | ||
return _context24.stop(); | ||
return _context25.stop(); | ||
} | ||
} | ||
}, _callee24, undefined, [[4, 10]]); | ||
}, _callee25, undefined, [[4, 10]]); | ||
}))); | ||
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: | ||
@@ -939,4 +953,4 @@ transport = { | ||
callback = jest.fn(); | ||
_context25.prev = 4; | ||
_context25.next = 7; | ||
_context26.prev = 4; | ||
_context26.next = 7; | ||
return _AccountsClient2.default.logout(callback); | ||
@@ -948,4 +962,4 @@ | ||
case 10: | ||
_context25.prev = 10; | ||
_context25.t0 = _context25['catch'](4); | ||
_context26.prev = 10; | ||
_context26.t0 = _context26['catch'](4); | ||
@@ -956,18 +970,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: | ||
@@ -981,15 +995,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); | ||
@@ -1000,16 +1014,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 () { | ||
@@ -1021,22 +1038,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]]); | ||
}))); | ||
@@ -1080,14 +1097,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: | ||
@@ -1099,4 +1116,4 @@ error = 'something bad'; | ||
} }); | ||
_context29.prev = 2; | ||
_context29.next = 5; | ||
_context30.prev = 2; | ||
_context30.next = 5; | ||
return _AccountsClient2.default.verifyEmail(); | ||
@@ -1108,20 +1125,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: | ||
@@ -1133,3 +1150,3 @@ mock = jest.fn(function () { | ||
_AccountsClient2.default.config({}, { verifyEmail: mock }); | ||
_context30.next = 4; | ||
_context31.next = 4; | ||
return _AccountsClient2.default.verifyEmail('token'); | ||
@@ -1143,6 +1160,6 @@ | ||
case 'end': | ||
return _context30.stop(); | ||
return _context31.stop(); | ||
} | ||
} | ||
}, _callee30, undefined); | ||
}, _callee31, undefined); | ||
}))); | ||
@@ -1152,7 +1169,7 @@ }); | ||
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: | ||
@@ -1164,4 +1181,4 @@ error = 'something bad'; | ||
} }); | ||
_context31.prev = 2; | ||
_context31.next = 5; | ||
_context32.prev = 2; | ||
_context32.next = 5; | ||
return _AccountsClient2.default.resetPassword(); | ||
@@ -1173,20 +1190,20 @@ | ||
case 8: | ||
_context31.prev = 8; | ||
_context31.t0 = _context31['catch'](2); | ||
_context32.prev = 8; | ||
_context32.t0 = _context32['catch'](2); | ||
expect(_context31.t0.message).toEqual(error); | ||
expect(_context32.t0.message).toEqual(error); | ||
case 11: | ||
case 'end': | ||
return _context31.stop(); | ||
return _context32.stop(); | ||
} | ||
} | ||
}, _callee31, undefined, [[2, 8]]); | ||
}, _callee32, undefined, [[2, 8]]); | ||
}))); | ||
it('should call transport.resetPassword', _asyncToGenerator(regeneratorRuntime.mark(function _callee32() { | ||
it('should call transport.resetPassword', _asyncToGenerator(regeneratorRuntime.mark(function _callee33() { | ||
var mock; | ||
return regeneratorRuntime.wrap(function _callee32$(_context32) { | ||
return regeneratorRuntime.wrap(function _callee33$(_context33) { | ||
while (1) { | ||
switch (_context32.prev = _context32.next) { | ||
switch (_context33.prev = _context33.next) { | ||
case 0: | ||
@@ -1198,3 +1215,3 @@ mock = jest.fn(function () { | ||
_AccountsClient2.default.config({}, { resetPassword: mock }); | ||
_context32.next = 4; | ||
_context33.next = 4; | ||
return _AccountsClient2.default.resetPassword('token', 'newPassword'); | ||
@@ -1209,6 +1226,6 @@ | ||
case 'end': | ||
return _context32.stop(); | ||
return _context33.stop(); | ||
} | ||
} | ||
}, _callee32, undefined); | ||
}, _callee33, undefined); | ||
}))); | ||
@@ -1218,7 +1235,7 @@ }); | ||
describe('requestPasswordReset', function () { | ||
it('should return an AccountsError', _asyncToGenerator(regeneratorRuntime.mark(function _callee33() { | ||
it('should return an AccountsError', _asyncToGenerator(regeneratorRuntime.mark(function _callee34() { | ||
var error; | ||
return regeneratorRuntime.wrap(function _callee33$(_context33) { | ||
return regeneratorRuntime.wrap(function _callee34$(_context34) { | ||
while (1) { | ||
switch (_context33.prev = _context33.next) { | ||
switch (_context34.prev = _context34.next) { | ||
case 0: | ||
@@ -1230,4 +1247,4 @@ error = 'something bad'; | ||
} }); | ||
_context33.prev = 2; | ||
_context33.next = 5; | ||
_context34.prev = 2; | ||
_context34.next = 5; | ||
return _AccountsClient2.default.requestPasswordReset('email@g.co'); | ||
@@ -1239,20 +1256,20 @@ | ||
case 8: | ||
_context33.prev = 8; | ||
_context33.t0 = _context33['catch'](2); | ||
_context34.prev = 8; | ||
_context34.t0 = _context34['catch'](2); | ||
expect(_context33.t0.message).toEqual(error); | ||
expect(_context34.t0.message).toEqual(error); | ||
case 11: | ||
case 'end': | ||
return _context33.stop(); | ||
return _context34.stop(); | ||
} | ||
} | ||
}, _callee33, undefined, [[2, 8]]); | ||
}, _callee34, undefined, [[2, 8]]); | ||
}))); | ||
it('should call transport.sendResetPasswordEmail', _asyncToGenerator(regeneratorRuntime.mark(function _callee34() { | ||
it('should call transport.sendResetPasswordEmail', _asyncToGenerator(regeneratorRuntime.mark(function _callee35() { | ||
var mock; | ||
return regeneratorRuntime.wrap(function _callee34$(_context34) { | ||
return regeneratorRuntime.wrap(function _callee35$(_context35) { | ||
while (1) { | ||
switch (_context34.prev = _context34.next) { | ||
switch (_context35.prev = _context35.next) { | ||
case 0: | ||
@@ -1264,3 +1281,3 @@ mock = jest.fn(function () { | ||
_AccountsClient2.default.config({}, { sendResetPasswordEmail: mock }); | ||
_context34.next = 4; | ||
_context35.next = 4; | ||
return _AccountsClient2.default.requestPasswordReset('email@g.co'); | ||
@@ -1274,13 +1291,13 @@ | ||
case 'end': | ||
return _context34.stop(); | ||
return _context35.stop(); | ||
} | ||
} | ||
}, _callee34, undefined); | ||
}, _callee35, 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 _callee36() { | ||
var mock; | ||
return regeneratorRuntime.wrap(function _callee35$(_context35) { | ||
return regeneratorRuntime.wrap(function _callee36$(_context36) { | ||
while (1) { | ||
switch (_context35.prev = _context35.next) { | ||
switch (_context36.prev = _context36.next) { | ||
case 0: | ||
@@ -1290,4 +1307,4 @@ mock = jest.fn(); | ||
_AccountsClient2.default.config({}, { sendResetPasswordEmail: mock }); | ||
_context35.prev = 2; | ||
_context35.next = 5; | ||
_context36.prev = 2; | ||
_context36.next = 5; | ||
return _AccountsClient2.default.requestPasswordReset('email'); | ||
@@ -1299,6 +1316,6 @@ | ||
case 8: | ||
_context35.prev = 8; | ||
_context35.t0 = _context35['catch'](2); | ||
_context36.prev = 8; | ||
_context36.t0 = _context36['catch'](2); | ||
expect(_context35.t0.message).toEqual('Valid email must be provided'); | ||
expect(_context36.t0.message).toEqual('Valid email must be provided'); | ||
expect(mock.mock.calls.length).toEqual(0); | ||
@@ -1308,6 +1325,6 @@ | ||
case 'end': | ||
return _context35.stop(); | ||
return _context36.stop(); | ||
} | ||
} | ||
}, _callee35, undefined, [[2, 8]]); | ||
}, _callee36, undefined, [[2, 8]]); | ||
}))); | ||
@@ -1317,7 +1334,7 @@ }); | ||
describe('requestVerificationEmail', function () { | ||
it('should return an AccountsError', _asyncToGenerator(regeneratorRuntime.mark(function _callee36() { | ||
it('should return an AccountsError', _asyncToGenerator(regeneratorRuntime.mark(function _callee37() { | ||
var error; | ||
return regeneratorRuntime.wrap(function _callee36$(_context36) { | ||
return regeneratorRuntime.wrap(function _callee37$(_context37) { | ||
while (1) { | ||
switch (_context36.prev = _context36.next) { | ||
switch (_context37.prev = _context37.next) { | ||
case 0: | ||
@@ -1329,4 +1346,4 @@ error = 'something bad'; | ||
} }); | ||
_context36.prev = 2; | ||
_context36.next = 5; | ||
_context37.prev = 2; | ||
_context37.next = 5; | ||
return _AccountsClient2.default.requestVerificationEmail('email@g.co'); | ||
@@ -1338,20 +1355,20 @@ | ||
case 8: | ||
_context36.prev = 8; | ||
_context36.t0 = _context36['catch'](2); | ||
_context37.prev = 8; | ||
_context37.t0 = _context37['catch'](2); | ||
expect(_context36.t0.message).toEqual(error); | ||
expect(_context37.t0.message).toEqual(error); | ||
case 11: | ||
case 'end': | ||
return _context36.stop(); | ||
return _context37.stop(); | ||
} | ||
} | ||
}, _callee36, undefined, [[2, 8]]); | ||
}, _callee37, undefined, [[2, 8]]); | ||
}))); | ||
it('should call transport.sendVerificationEmail', _asyncToGenerator(regeneratorRuntime.mark(function _callee37() { | ||
it('should call transport.sendVerificationEmail', _asyncToGenerator(regeneratorRuntime.mark(function _callee38() { | ||
var mock; | ||
return regeneratorRuntime.wrap(function _callee37$(_context37) { | ||
return regeneratorRuntime.wrap(function _callee38$(_context38) { | ||
while (1) { | ||
switch (_context37.prev = _context37.next) { | ||
switch (_context38.prev = _context38.next) { | ||
case 0: | ||
@@ -1363,3 +1380,3 @@ mock = jest.fn(function () { | ||
_AccountsClient2.default.config({}, { sendVerificationEmail: mock }); | ||
_context37.next = 4; | ||
_context38.next = 4; | ||
return _AccountsClient2.default.requestVerificationEmail('email@g.co'); | ||
@@ -1373,13 +1390,13 @@ | ||
case 'end': | ||
return _context37.stop(); | ||
return _context38.stop(); | ||
} | ||
} | ||
}, _callee37, undefined); | ||
}, _callee38, 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 _callee39() { | ||
var mock; | ||
return regeneratorRuntime.wrap(function _callee38$(_context38) { | ||
return regeneratorRuntime.wrap(function _callee39$(_context39) { | ||
while (1) { | ||
switch (_context38.prev = _context38.next) { | ||
switch (_context39.prev = _context39.next) { | ||
case 0: | ||
@@ -1389,4 +1406,4 @@ mock = jest.fn(); | ||
_AccountsClient2.default.config({}, { sendVerificationEmail: mock }); | ||
_context38.prev = 2; | ||
_context38.next = 5; | ||
_context39.prev = 2; | ||
_context39.next = 5; | ||
return _AccountsClient2.default.requestVerificationEmail('email'); | ||
@@ -1398,6 +1415,6 @@ | ||
case 8: | ||
_context38.prev = 8; | ||
_context38.t0 = _context38['catch'](2); | ||
_context39.prev = 8; | ||
_context39.t0 = _context39['catch'](2); | ||
expect(_context38.t0.message).toEqual('Valid email must be provided'); | ||
expect(_context39.t0.message).toEqual('Valid email must be provided'); | ||
expect(mock.mock.calls.length).toEqual(0); | ||
@@ -1407,8 +1424,8 @@ | ||
case 'end': | ||
return _context38.stop(); | ||
return _context39.stop(); | ||
} | ||
} | ||
}, _callee38, undefined, [[2, 8]]); | ||
}, _callee39, undefined, [[2, 8]]); | ||
}))); | ||
}); | ||
}); |
@@ -36,4 +36,2 @@ 'use strict'; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -49,3 +47,3 @@ | ||
var getTokenKey = function getTokenKey(type, options) { | ||
return (0, _lodash.isString)(options.tokenStoragePrefix) && options.tokenStoragePrefix.length > 0 ? options.tokenStoragePrefix + ':' + type : type; | ||
return (0, _lodash.isString)(options.localStoragePrefix) && options.localStoragePrefix.length > 0 ? options.localStoragePrefix + ':' + type : type; | ||
}; | ||
@@ -58,3 +56,2 @@ | ||
this.options = options; | ||
this.storage = options.tokenStorage || _config3.default.tokenStorage; | ||
if (!transport) { | ||
@@ -68,5 +65,6 @@ throw new _common.AccountsError('A REST or GraphQL transport is required'); | ||
var reduxStoreKey = options.reduxStoreKey || _config3.default.reduxStoreKey; | ||
this.store = options.store || (0, _createStore2.default)({ | ||
reducers: _defineProperty({}, reduxStoreKey, _module2.default), | ||
this.store = (0, _createStore2.default)({ | ||
reducers: { | ||
accounts: _module2.default | ||
}, | ||
middleware: middleware | ||
@@ -79,199 +77,24 @@ }); | ||
value: function getState() { | ||
var state = this.store.getState(); | ||
if (typeof state.get === 'function') { | ||
return state.get(this.options.reduxStoreKey); | ||
} | ||
return state[this.options.reduxStoreKey]; | ||
return this.store.getState().get('accounts'); | ||
} | ||
}, { | ||
key: 'getStorageData', | ||
value: function () { | ||
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(keyName) { | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
return _context.abrupt('return', Promise.resolve(this.storage.getItem(keyName))); | ||
case 1: | ||
case 'end': | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, this); | ||
})); | ||
function getStorageData(_x) { | ||
return _ref.apply(this, arguments); | ||
} | ||
return getStorageData; | ||
}() | ||
}, { | ||
key: 'setStorageData', | ||
value: function () { | ||
var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(keyName, value) { | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
return _context2.abrupt('return', Promise.resolve(this.storage.setItem(keyName, value))); | ||
case 1: | ||
case 'end': | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, this); | ||
})); | ||
function setStorageData(_x2, _x3) { | ||
return _ref2.apply(this, arguments); | ||
} | ||
return setStorageData; | ||
}() | ||
}, { | ||
key: 'removeStorageData', | ||
value: function () { | ||
var _ref3 = _asyncToGenerator(regeneratorRuntime.mark(function _callee3(keyName) { | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
return _context3.abrupt('return', Promise.resolve(this.storage.removeItem(keyName))); | ||
case 1: | ||
case 'end': | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee3, this); | ||
})); | ||
function removeStorageData(_x4) { | ||
return _ref3.apply(this, arguments); | ||
} | ||
return removeStorageData; | ||
}() | ||
}, { | ||
key: 'user', | ||
value: function user() { | ||
var user = this.getState().get('user'); | ||
return user ? user.toJS() : null; | ||
return this.getState().get('user').toJS(); | ||
} | ||
}, { | ||
key: 'tokens', | ||
value: function () { | ||
var _ref4 = _asyncToGenerator(regeneratorRuntime.mark(function _callee4() { | ||
return regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
_context4.next = 2; | ||
return this.getStorageData(getTokenKey(ACCESS_TOKEN, this.options)); | ||
case 2: | ||
_context4.t0 = _context4.sent; | ||
_context4.next = 5; | ||
return this.getStorageData(getTokenKey(REFRESH_TOKEN, this.options)); | ||
case 5: | ||
_context4.t1 = _context4.sent; | ||
return _context4.abrupt('return', { | ||
accessToken: _context4.t0, | ||
refreshToken: _context4.t1 | ||
}); | ||
case 7: | ||
case 'end': | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee4, this); | ||
})); | ||
function tokens() { | ||
return _ref4.apply(this, arguments); | ||
} | ||
return tokens; | ||
}() | ||
value: function tokens() { | ||
return { | ||
accessToken: localStorage.getItem(getTokenKey(ACCESS_TOKEN, this.options)), | ||
refreshToken: localStorage.getItem(getTokenKey(REFRESH_TOKEN, this.options)) | ||
}; | ||
} | ||
}, { | ||
key: 'clearTokens', | ||
value: function () { | ||
var _ref5 = _asyncToGenerator(regeneratorRuntime.mark(function _callee5() { | ||
return regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
_context5.next = 2; | ||
return this.removeStorageData(getTokenKey(ACCESS_TOKEN, this.options)); | ||
case 2: | ||
_context5.next = 4; | ||
return this.removeStorageData(getTokenKey(REFRESH_TOKEN, this.options)); | ||
case 4: | ||
case 'end': | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee5, this); | ||
})); | ||
function clearTokens() { | ||
return _ref5.apply(this, arguments); | ||
} | ||
return clearTokens; | ||
}() | ||
value: function clearTokens() { | ||
localStorage.removeItem(getTokenKey(ACCESS_TOKEN, this.options)); | ||
localStorage.removeItem(getTokenKey(REFRESH_TOKEN, this.options)); | ||
} | ||
}, { | ||
key: 'storeTokens', | ||
value: function () { | ||
var _ref6 = _asyncToGenerator(regeneratorRuntime.mark(function _callee6(loginResponse) { | ||
var newAccessToken, newRefreshToken; | ||
return regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
newAccessToken = loginResponse.tokens.accessToken; | ||
if (!newAccessToken) { | ||
_context6.next = 4; | ||
break; | ||
} | ||
_context6.next = 4; | ||
return this.setStorageData(getTokenKey(ACCESS_TOKEN, this.options), newAccessToken); | ||
case 4: | ||
newRefreshToken = loginResponse.tokens.refreshToken; | ||
if (!newRefreshToken) { | ||
_context6.next = 8; | ||
break; | ||
} | ||
_context6.next = 8; | ||
return this.setStorageData(getTokenKey(REFRESH_TOKEN, this.options), newRefreshToken); | ||
case 8: | ||
case 'end': | ||
return _context6.stop(); | ||
} | ||
} | ||
}, _callee6, this); | ||
})); | ||
function storeTokens(_x5) { | ||
return _ref6.apply(this, arguments); | ||
} | ||
return storeTokens; | ||
}() | ||
}, { | ||
key: 'clearUser', | ||
@@ -290,25 +113,17 @@ value: function clearUser() { | ||
value: function () { | ||
var _ref7 = _asyncToGenerator(regeneratorRuntime.mark(function _callee7() { | ||
var _ref8, accessToken, refreshToken, decodedRefreshToken, currentTime, refreshedSession; | ||
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee() { | ||
var _tokens, accessToken, refreshToken, decodedRefreshToken, currentTime, refreshedSession; | ||
return regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context7.next = 2; | ||
return this.tokens(); | ||
_tokens = this.tokens(), accessToken = _tokens.accessToken, refreshToken = _tokens.refreshToken; | ||
case 2: | ||
_ref8 = _context7.sent; | ||
accessToken = _ref8.accessToken; | ||
refreshToken = _ref8.refreshToken; | ||
if (!(accessToken && refreshToken)) { | ||
_context7.next = 32; | ||
_context.next = 25; | ||
break; | ||
} | ||
_context7.prev = 6; | ||
this.store.dispatch((0, _module.loggingIn)(true)); | ||
_context.prev = 2; | ||
decodedRefreshToken = (0, _jwtDecode2.default)(refreshToken); | ||
@@ -319,3 +134,3 @@ currentTime = Date.now() / 1000; | ||
if (!(decodedRefreshToken.exp < currentTime)) { | ||
_context7.next = 15; | ||
_context.next = 10; | ||
break; | ||
@@ -326,29 +141,24 @@ } | ||
this.clearUser(); | ||
_context7.next = 22; | ||
_context.next = 16; | ||
break; | ||
case 15: | ||
_context7.next = 17; | ||
case 10: | ||
_context.next = 12; | ||
return this.transport.refreshTokens(accessToken, refreshToken); | ||
case 17: | ||
refreshedSession = _context7.sent; | ||
case 12: | ||
refreshedSession = _context.sent; | ||
this.store.dispatch((0, _module.loggingIn)(false)); | ||
_context7.next = 21; | ||
return this.storeTokens(refreshedSession); | ||
case 21: | ||
localStorage.setItem(getTokenKey(ACCESS_TOKEN, this.options), refreshedSession.tokens.accessToken); | ||
localStorage.setItem(getTokenKey(REFRESH_TOKEN, this.options), refreshedSession.tokens.refreshToken); | ||
this.store.dispatch((0, _module.setUser)(refreshedSession.user)); | ||
case 22: | ||
_context7.next = 30; | ||
case 16: | ||
_context.next = 23; | ||
break; | ||
case 24: | ||
_context7.prev = 24; | ||
_context7.t0 = _context7['catch'](6); | ||
case 18: | ||
_context.prev = 18; | ||
_context.t0 = _context['catch'](2); | ||
this.store.dispatch((0, _module.loggingIn)(false)); | ||
this.clearTokens(); | ||
@@ -358,21 +168,20 @@ this.clearUser(); | ||
case 30: | ||
_context7.next = 35; | ||
case 23: | ||
_context.next = 27; | ||
break; | ||
case 32: | ||
case 25: | ||
this.clearTokens(); | ||
this.clearUser(); | ||
throw new _common.AccountsError('no tokens provided'); | ||
case 35: | ||
case 27: | ||
case 'end': | ||
return _context7.stop(); | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee7, this, [[6, 24]]); | ||
}, _callee, this, [[2, 18]]); | ||
})); | ||
function refreshSession() { | ||
return _ref7.apply(this, arguments); | ||
return _ref.apply(this, arguments); | ||
} | ||
@@ -385,10 +194,10 @@ | ||
value: function () { | ||
var _ref9 = _asyncToGenerator(regeneratorRuntime.mark(function _callee8(user, callback) { | ||
var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(user, callback) { | ||
var userId; | ||
return regeneratorRuntime.wrap(function _callee8$(_context8) { | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
if (!(!user || user.password === undefined)) { | ||
_context8.next = 2; | ||
_context2.next = 2; | ||
break; | ||
@@ -404,3 +213,3 @@ } | ||
if (_common.validators.validatePassword(user.password)) { | ||
_context8.next = 4; | ||
_context2.next = 4; | ||
break; | ||
@@ -413,3 +222,3 @@ } | ||
if (!(!_common.validators.validateUsername(user.username) && !_common.validators.validateEmail(user.email))) { | ||
_context8.next = 6; | ||
_context2.next = 6; | ||
break; | ||
@@ -421,8 +230,8 @@ } | ||
case 6: | ||
_context8.prev = 6; | ||
_context8.next = 9; | ||
_context2.prev = 6; | ||
_context2.next = 9; | ||
return this.transport.createUser(user); | ||
case 9: | ||
userId = _context8.sent; | ||
userId = _context2.sent; | ||
@@ -432,28 +241,28 @@ if (callback && (0, _lodash.isFunction)(callback)) { | ||
} | ||
_context8.next = 13; | ||
_context2.next = 13; | ||
return this.loginWithPassword({ id: userId }, user.password); | ||
case 13: | ||
_context8.next = 19; | ||
_context2.next = 19; | ||
break; | ||
case 15: | ||
_context8.prev = 15; | ||
_context8.t0 = _context8['catch'](6); | ||
_context2.prev = 15; | ||
_context2.t0 = _context2['catch'](6); | ||
if (callback && (0, _lodash.isFunction)(callback)) { | ||
callback(_context8.t0); | ||
callback(_context2.t0); | ||
} | ||
throw new _common.AccountsError(_context8.t0.message); | ||
throw new _common.AccountsError(_context2.t0.message); | ||
case 19: | ||
case 'end': | ||
return _context8.stop(); | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee8, this, [[6, 15]]); | ||
}, _callee2, this, [[6, 15]]); | ||
})); | ||
function createUser(_x6, _x7) { | ||
return _ref9.apply(this, arguments); | ||
function createUser(_x, _x2) { | ||
return _ref2.apply(this, arguments); | ||
} | ||
@@ -466,10 +275,10 @@ | ||
value: function () { | ||
var _ref10 = _asyncToGenerator(regeneratorRuntime.mark(function _callee9(user, password, callback) { | ||
var _ref3 = _asyncToGenerator(regeneratorRuntime.mark(function _callee3(user, password, callback) { | ||
var res; | ||
return regeneratorRuntime.wrap(function _callee9$(_context9) { | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
if (!(!password || !user)) { | ||
_context9.next = 2; | ||
_context3.next = 2; | ||
break; | ||
@@ -482,3 +291,3 @@ } | ||
if (!(!(0, _lodash.isString)(user) && !isValidUserObject(user) || !(0, _lodash.isString)(password))) { | ||
_context9.next = 4; | ||
_context3.next = 4; | ||
break; | ||
@@ -492,46 +301,41 @@ } | ||
this.store.dispatch((0, _module.loggingIn)(true)); | ||
_context9.prev = 5; | ||
_context9.next = 8; | ||
_context3.prev = 5; | ||
_context3.next = 8; | ||
return this.transport.loginWithPassword(user, password); | ||
case 8: | ||
res = _context9.sent; | ||
res = _context3.sent; | ||
this.store.dispatch((0, _module.loggingIn)(false)); | ||
_context9.next = 12; | ||
return this.storeTokens(res); | ||
case 12: | ||
localStorage.setItem(getTokenKey(ACCESS_TOKEN, this.options), res.tokens.accessToken); | ||
localStorage.setItem(getTokenKey(REFRESH_TOKEN, this.options), res.tokens.refreshToken); | ||
this.store.dispatch((0, _module.setUser)(res.user)); | ||
if (this.options.onSignedInHook) { | ||
this.options.onSignedInHook(); | ||
} | ||
this.options.onSignedInHook(); | ||
if (callback && (0, _lodash.isFunction)(callback)) { | ||
callback(); | ||
} | ||
_context9.next = 22; | ||
_context3.next = 20; | ||
break; | ||
case 17: | ||
_context9.prev = 17; | ||
_context9.t0 = _context9['catch'](5); | ||
case 16: | ||
_context3.prev = 16; | ||
_context3.t0 = _context3['catch'](5); | ||
this.store.dispatch((0, _module.loggingIn)(false)); | ||
if (callback && (0, _lodash.isFunction)(callback)) { | ||
callback(_context9.t0); | ||
callback(_context3.t0); | ||
} | ||
throw new _common.AccountsError(_context9.t0.message); | ||
throw new _common.AccountsError(_context3.t0.message); | ||
case 22: | ||
case 20: | ||
this.store.dispatch((0, _module.loggingIn)(false), user); | ||
case 21: | ||
case 'end': | ||
return _context9.stop(); | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee9, this, [[5, 17]]); | ||
}, _callee3, this, [[5, 16]]); | ||
})); | ||
function loginWithPassword(_x8, _x9, _x10) { | ||
return _ref10.apply(this, arguments); | ||
function loginWithPassword(_x3, _x4, _x5) { | ||
return _ref3.apply(this, arguments); | ||
} | ||
@@ -554,27 +358,15 @@ | ||
value: function () { | ||
var _ref11 = _asyncToGenerator(regeneratorRuntime.mark(function _callee10(callback) { | ||
var _ref12, accessToken; | ||
var _ref4 = _asyncToGenerator(regeneratorRuntime.mark(function _callee4(callback) { | ||
var _tokens2, accessToken; | ||
return regeneratorRuntime.wrap(function _callee10$(_context10) { | ||
return regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context10.prev = _context10.next) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
_context10.prev = 0; | ||
_context10.next = 3; | ||
return this.tokens(); | ||
case 3: | ||
_ref12 = _context10.sent; | ||
accessToken = _ref12.accessToken; | ||
if (!accessToken) { | ||
_context10.next = 8; | ||
break; | ||
} | ||
_context10.next = 8; | ||
_context4.prev = 0; | ||
_tokens2 = this.tokens(), accessToken = _tokens2.accessToken; | ||
_context4.next = 4; | ||
return this.transport.logout(accessToken); | ||
case 8: | ||
case 4: | ||
this.clearTokens(); | ||
@@ -585,28 +377,25 @@ this.store.dispatch((0, _module.clearUser)()); | ||
} | ||
if (this.options.onSignedOutHook) { | ||
this.options.onSignedOutHook(); | ||
} | ||
_context10.next = 18; | ||
this.options.onSignedOutHook(); | ||
_context4.next = 14; | ||
break; | ||
case 14: | ||
_context10.prev = 14; | ||
_context10.t0 = _context10['catch'](0); | ||
case 10: | ||
_context4.prev = 10; | ||
_context4.t0 = _context4['catch'](0); | ||
if (callback && (0, _lodash.isFunction)(callback)) { | ||
callback(_context10.t0); | ||
callback(_context4.t0); | ||
} | ||
throw new _common.AccountsError(_context10.t0.message); | ||
throw new _common.AccountsError(_context4.t0.message); | ||
case 18: | ||
case 14: | ||
case 'end': | ||
return _context10.stop(); | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee10, this, [[0, 14]]); | ||
}, _callee4, this, [[0, 10]]); | ||
})); | ||
function logout(_x11) { | ||
return _ref11.apply(this, arguments); | ||
function logout(_x6) { | ||
return _ref4.apply(this, arguments); | ||
} | ||
@@ -613,0 +402,0 @@ |
@@ -434,5 +434,4 @@ 'use strict'; | ||
expect(callback.mock.calls.length).toEqual(1); | ||
expect(_AccountsClient2.default.loggingIn()).toBe(false); | ||
case 7: | ||
case 6: | ||
case 'end': | ||
@@ -508,3 +507,3 @@ return _context11.stop(); | ||
}))); | ||
it('sets loggingIn flag to false on failed login', _asyncToGenerator(regeneratorRuntime.mark(function _callee14() { | ||
it('stores tokens in local storage', _asyncToGenerator(regeneratorRuntime.mark(function _callee14() { | ||
var transport; | ||
@@ -517,35 +516,2 @@ return regeneratorRuntime.wrap(function _callee14$(_context14) { | ||
loginWithPassword: function loginWithPassword() { | ||
return Promise.reject('error'); | ||
} | ||
}; | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
_context14.prev = 2; | ||
_context14.next = 5; | ||
return _AccountsClient2.default.loginWithPassword('username', 'password'); | ||
case 5: | ||
throw new Error(); | ||
case 8: | ||
_context14.prev = 8; | ||
_context14.t0 = _context14['catch'](2); | ||
expect(_AccountsClient2.default.loggingIn()).toBe(false); | ||
case 11: | ||
case 'end': | ||
return _context14.stop(); | ||
} | ||
} | ||
}, _callee14, undefined, [[2, 8]]); | ||
}))); | ||
it('stores tokens in local storage', _asyncToGenerator(regeneratorRuntime.mark(function _callee15() { | ||
var transport; | ||
return regeneratorRuntime.wrap(function _callee15$(_context15) { | ||
while (1) { | ||
switch (_context15.prev = _context15.next) { | ||
case 0: | ||
transport = { | ||
loginWithPassword: function loginWithPassword() { | ||
return Promise.resolve(loggedInUser); | ||
@@ -556,3 +522,3 @@ } | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
_context15.next = 4; | ||
_context14.next = 4; | ||
return _AccountsClient2.default.loginWithPassword('username', 'password'); | ||
@@ -566,91 +532,12 @@ | ||
case 'end': | ||
return _context15.stop(); | ||
return _context14.stop(); | ||
} | ||
} | ||
}, _callee15, undefined); | ||
}, _callee14, undefined); | ||
}))); | ||
it('fetch tokens from storage when storage is async', _asyncToGenerator(regeneratorRuntime.mark(function _callee16() { | ||
var transport, tokens; | ||
return regeneratorRuntime.wrap(function _callee16$(_context16) { | ||
while (1) { | ||
switch (_context16.prev = _context16.next) { | ||
case 0: | ||
transport = { | ||
loginWithPassword: function loginWithPassword() { | ||
return Promise.resolve(loggedInUser); | ||
} | ||
}; | ||
_AccountsClient2.default.config({ | ||
history: history, | ||
tokenStorage: { | ||
getItem: function getItem() { | ||
return Promise.resolve('testValue'); | ||
} | ||
} | ||
}, transport); | ||
_context16.next = 4; | ||
return _AccountsClient2.default.tokens(); | ||
case 4: | ||
tokens = _context16.sent; | ||
expect(tokens.accessToken).toBe('testValue'); | ||
expect(tokens.refreshToken).toBe('testValue'); | ||
case 7: | ||
case 'end': | ||
return _context16.stop(); | ||
} | ||
} | ||
}, _callee16, 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 _callee15() { | ||
var transport; | ||
return regeneratorRuntime.wrap(function _callee18$(_context18) { | ||
return regeneratorRuntime.wrap(function _callee15$(_context15) { | ||
while (1) { | ||
switch (_context18.prev = _context18.next) { | ||
switch (_context15.prev = _context15.next) { | ||
case 0: | ||
@@ -664,3 +551,3 @@ transport = { | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
_context18.next = 4; | ||
_context15.next = 4; | ||
return _AccountsClient2.default.loginWithPassword('username', 'password'); | ||
@@ -673,14 +560,14 @@ | ||
case 'end': | ||
return _context18.stop(); | ||
return _context15.stop(); | ||
} | ||
} | ||
}, _callee18, undefined); | ||
}, _callee15, undefined); | ||
}))); | ||
}); | ||
describe('logout', function () { | ||
it('calls callback on successful logout', _asyncToGenerator(regeneratorRuntime.mark(function _callee19() { | ||
it('calls callback on successful logout', _asyncToGenerator(regeneratorRuntime.mark(function _callee16() { | ||
var transport, callback; | ||
return regeneratorRuntime.wrap(function _callee19$(_context19) { | ||
return regeneratorRuntime.wrap(function _callee16$(_context16) { | ||
while (1) { | ||
switch (_context19.prev = _context19.next) { | ||
switch (_context16.prev = _context16.next) { | ||
case 0: | ||
@@ -695,3 +582,3 @@ transport = { | ||
callback = jest.fn(); | ||
_context19.next = 5; | ||
_context16.next = 5; | ||
return _AccountsClient2.default.logout(callback); | ||
@@ -704,12 +591,12 @@ | ||
case 'end': | ||
return _context19.stop(); | ||
return _context16.stop(); | ||
} | ||
} | ||
}, _callee19, undefined); | ||
}, _callee16, undefined); | ||
}))); | ||
it('calls onLogout on successful logout', _asyncToGenerator(regeneratorRuntime.mark(function _callee20() { | ||
it('calls onLogout on successful logout', _asyncToGenerator(regeneratorRuntime.mark(function _callee17() { | ||
var onSignedOutHook, transport; | ||
return regeneratorRuntime.wrap(function _callee20$(_context20) { | ||
return regeneratorRuntime.wrap(function _callee17$(_context17) { | ||
while (1) { | ||
switch (_context20.prev = _context20.next) { | ||
switch (_context17.prev = _context17.next) { | ||
case 0: | ||
@@ -724,3 +611,3 @@ onSignedOutHook = jest.fn(); | ||
_AccountsClient2.default.config({ history: history, onSignedOutHook: onSignedOutHook }, transport); | ||
_context20.next = 5; | ||
_context17.next = 5; | ||
return _AccountsClient2.default.logout(); | ||
@@ -733,13 +620,12 @@ | ||
case 'end': | ||
return _context20.stop(); | ||
return _context17.stop(); | ||
} | ||
} | ||
}, _callee20, undefined); | ||
}, _callee17, undefined); | ||
}))); | ||
it('calls callback on failure with error message', _asyncToGenerator(regeneratorRuntime.mark(function _callee21() { | ||
it('calls callback on failure with error message', _asyncToGenerator(regeneratorRuntime.mark(function _callee18() { | ||
var transport, callback; | ||
return regeneratorRuntime.wrap(function _callee21$(_context21) { | ||
return regeneratorRuntime.wrap(function _callee18$(_context18) { | ||
while (1) { | ||
switch (_context21.prev = _context21.next) { | ||
switch (_context18.prev = _context18.next) { | ||
case 0: | ||
@@ -752,38 +638,37 @@ transport = { | ||
_AccountsClient2.default.instance.storeTokens({ tokens: { accessToken: '1' } }); | ||
_AccountsClient2.default.config({ history: history }, transport); | ||
callback = jest.fn(); | ||
_context21.prev = 4; | ||
_context21.next = 7; | ||
_context18.prev = 3; | ||
_context18.next = 6; | ||
return _AccountsClient2.default.logout(callback); | ||
case 7: | ||
case 6: | ||
throw new Error(); | ||
case 10: | ||
_context21.prev = 10; | ||
_context21.t0 = _context21['catch'](4); | ||
case 9: | ||
_context18.prev = 9; | ||
_context18.t0 = _context18['catch'](3); | ||
expect(_context21.t0.message).toEqual('error message'); | ||
expect(_context18.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 14: | ||
case 'end': | ||
return _context21.stop(); | ||
return _context18.stop(); | ||
} | ||
} | ||
}, _callee21, undefined, [[4, 10]]); | ||
}, _callee18, undefined, [[3, 9]]); | ||
}))); | ||
}); | ||
describe('refreshSession', _asyncToGenerator(regeneratorRuntime.mark(function _callee24() { | ||
return regeneratorRuntime.wrap(function _callee24$(_context24) { | ||
describe('refreshSession', _asyncToGenerator(regeneratorRuntime.mark(function _callee21() { | ||
return regeneratorRuntime.wrap(function _callee21$(_context21) { | ||
while (1) { | ||
switch (_context24.prev = _context24.next) { | ||
switch (_context21.prev = _context21.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 _callee22() { | ||
return regeneratorRuntime.wrap(function _callee22$(_context22) { | ||
it('clears tokens and user if tokens are not set', _asyncToGenerator(regeneratorRuntime.mark(function _callee19() { | ||
return regeneratorRuntime.wrap(function _callee19$(_context19) { | ||
while (1) { | ||
switch (_context22.prev = _context22.next) { | ||
switch (_context19.prev = _context19.next) { | ||
case 0: | ||
@@ -797,30 +682,21 @@ _AccountsClient2.default.config({}, {}); | ||
}); | ||
_context22.prev = 3; | ||
_context22.next = 6; | ||
_context19.next = 5; | ||
return _AccountsClient2.default.refreshSession(); | ||
case 6: | ||
_context22.next = 13; | ||
break; | ||
case 8: | ||
_context22.prev = 8; | ||
_context22.t0 = _context22['catch'](3); | ||
expect(_context22.t0.message).toEqual('no tokens provided'); | ||
case 5: | ||
expect(_AccountsClient2.default.instance.clearTokens.mock.calls.length).toEqual(1); | ||
expect(_AccountsClient2.default.instance.clearUser.mock.calls.length).toEqual(1); | ||
case 13: | ||
case 7: | ||
case 'end': | ||
return _context22.stop(); | ||
return _context19.stop(); | ||
} | ||
} | ||
}, _callee22, undefined, [[3, 8]]); | ||
}, _callee19, undefined); | ||
}))); | ||
it('clears tokens, users and throws error if bad refresh token provided', _asyncToGenerator(regeneratorRuntime.mark(function _callee23() { | ||
it('clears tokens, users and throws error if bad refresh token provided', _asyncToGenerator(regeneratorRuntime.mark(function _callee20() { | ||
var message; | ||
return regeneratorRuntime.wrap(function _callee23$(_context23) { | ||
return regeneratorRuntime.wrap(function _callee20$(_context20) { | ||
while (1) { | ||
switch (_context23.prev = _context23.next) { | ||
switch (_context20.prev = _context20.next) { | ||
case 0: | ||
@@ -836,4 +712,4 @@ _AccountsClient2.default.config({}, {}); | ||
}); | ||
_context23.prev = 5; | ||
_context23.next = 8; | ||
_context20.prev = 5; | ||
_context20.next = 8; | ||
return _AccountsClient2.default.refreshSession(); | ||
@@ -845,5 +721,5 @@ | ||
case 11: | ||
_context23.prev = 11; | ||
_context23.t0 = _context23['catch'](5); | ||
message = _context23.t0.message; | ||
_context20.prev = 11; | ||
_context20.t0 = _context20['catch'](5); | ||
message = _context20.t0.message; | ||
@@ -854,6 +730,6 @@ expect(message).toEqual('falsy token provided'); | ||
case 'end': | ||
return _context23.stop(); | ||
return _context20.stop(); | ||
} | ||
} | ||
}, _callee23, undefined, [[5, 11]]); | ||
}, _callee20, undefined, [[5, 11]]); | ||
}))); | ||
@@ -897,7 +773,7 @@ // it('requests a new token pair, sets the tokens and the user', async () => { | ||
case 'end': | ||
return _context24.stop(); | ||
return _context21.stop(); | ||
} | ||
} | ||
}, _callee24, undefined); | ||
}, _callee21, undefined); | ||
}))); | ||
}); |
@@ -22,8 +22,5 @@ 'use strict'; | ||
exports.default = _extends({}, _common.config, { | ||
store: null, | ||
reduxLogger: null, | ||
reduxStoreKey: 'accounts', | ||
tokenStorage: global.localStorage, | ||
server: '', | ||
tokenStoragePrefix: '', | ||
localStoragePrefix: '', | ||
title: '', | ||
@@ -40,3 +37,2 @@ requestPermissions: [], | ||
homePath: '/', | ||
signOutPath: '/', | ||
// TODO enable all of these | ||
@@ -59,5 +55,5 @@ // onSubmitHook: () => {}, | ||
onSignedOutHook: function onSignedOutHook() { | ||
return (0, _redirect2.default)(_AccountsClient2.default.options().signOutPath); | ||
return (0, _redirect2.default)(_AccountsClient2.default.options().homePath); | ||
}, | ||
loginOnSignUp: true | ||
}); |
@@ -262,11 +262,6 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
}, { | ||
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,9 +297,26 @@ return _context4.stop(); | ||
function tokens() { | ||
function loadTokensFromStorage() { | ||
return _ref4.apply(this, arguments); | ||
} | ||
return tokens; | ||
return loadTokensFromStorage; | ||
}() | ||
}, { | ||
key: 'user', | ||
value: function user() { | ||
var user = this.getState().get('user'); | ||
return user ? user.toJS() : null; | ||
} | ||
}, { | ||
key: 'tokens', | ||
value: function tokens() { | ||
var tokens = this.getState().get('tokens'); | ||
return tokens ? tokens.toJS() : { | ||
accessToken: null, | ||
refreshToken: null | ||
}; | ||
} | ||
}, { | ||
key: 'clearTokens', | ||
@@ -876,3 +890,22 @@ value: function () { | ||
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 _callee15() { | ||
return _regenerator2.default.wrap(function _callee15$(_context15) { | ||
while (1) { | ||
switch (_context15.prev = _context15.next) { | ||
case 0: | ||
_this.instance = new AccountsClient((0, _extends3.default)({}, _config3.default, options), transport); | ||
return _context15.abrupt('return', _this.instance.loadTokensFromStorage().then(function () { | ||
return _this.instance; | ||
})); | ||
case 2: | ||
case 'end': | ||
return _context15.stop(); | ||
} | ||
} | ||
}, _callee15, _this); | ||
}))(); | ||
}, | ||
@@ -879,0 +912,0 @@ user: function user() { |
@@ -18,3 +18,3 @@ 'use strict'; | ||
isLoading: false, | ||
user: null, | ||
user: (0, _immutable.Map)(), | ||
loggingIn: false | ||
@@ -41,3 +41,3 @@ }); | ||
{ | ||
return state.set('user', null); | ||
return state.set('user', (0, _immutable.Map)()); | ||
} | ||
@@ -44,0 +44,0 @@ case LOGGING_IN: |
@@ -15,3 +15,2 @@ 'use strict'; | ||
return _AccountsClient2.default.options().history.push(path); | ||
}; | ||
// eslint-disable-next-line import/no-named-as-default | ||
}; // eslint-disable-next-line import/no-named-as-default |
{ | ||
"name": "@accounts/client", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "Fullstack authentication and accounts-management", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
309163
5781