@accounts/client
Advanced tools
Comparing version 0.0.8 to 0.0.9-alpha.146b94d6
@@ -56,3 +56,3 @@ 'use strict'; | ||
this.options = options; | ||
this.storage = options.tokenStorage; | ||
this.storage = options.tokenStorage || _config3.default.tokenStorage; | ||
if (!transport) { | ||
@@ -66,4 +66,5 @@ 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({}, options.reduxStoreKey, _module2.default), | ||
reducers: _defineProperty({}, reduxStoreKey, _module2.default), | ||
middleware: middleware | ||
@@ -302,3 +303,3 @@ }); | ||
if (!(accessToken && refreshToken)) { | ||
_context7.next = 29; | ||
_context7.next = 32; | ||
break; | ||
@@ -308,2 +309,4 @@ } | ||
_context7.prev = 6; | ||
this.store.dispatch((0, _module.loggingIn)(true)); | ||
decodedRefreshToken = (0, _jwtDecode2.default)(refreshToken); | ||
@@ -314,3 +317,3 @@ currentTime = Date.now() / 1000; | ||
if (!(decodedRefreshToken.exp < currentTime)) { | ||
_context7.next = 14; | ||
_context7.next = 15; | ||
break; | ||
@@ -321,25 +324,29 @@ } | ||
this.clearUser(); | ||
_context7.next = 20; | ||
_context7.next = 22; | ||
break; | ||
case 14: | ||
_context7.next = 16; | ||
case 15: | ||
_context7.next = 17; | ||
return this.transport.refreshTokens(accessToken, refreshToken); | ||
case 16: | ||
case 17: | ||
refreshedSession = _context7.sent; | ||
_context7.next = 19; | ||
this.store.dispatch((0, _module.loggingIn)(false)); | ||
_context7.next = 21; | ||
return this.storeTokens(refreshedSession); | ||
case 19: | ||
case 21: | ||
this.store.dispatch((0, _module.setUser)(refreshedSession.user)); | ||
case 20: | ||
_context7.next = 27; | ||
case 22: | ||
_context7.next = 30; | ||
break; | ||
case 22: | ||
_context7.prev = 22; | ||
case 24: | ||
_context7.prev = 24; | ||
_context7.t0 = _context7['catch'](6); | ||
this.store.dispatch((0, _module.loggingIn)(false)); | ||
this.clearTokens(); | ||
@@ -349,7 +356,7 @@ this.clearUser(); | ||
case 27: | ||
_context7.next = 32; | ||
case 30: | ||
_context7.next = 35; | ||
break; | ||
case 29: | ||
case 32: | ||
this.clearTokens(); | ||
@@ -359,3 +366,3 @@ this.clearUser(); | ||
case 32: | ||
case 35: | ||
case 'end': | ||
@@ -365,3 +372,3 @@ return _context7.stop(); | ||
} | ||
}, _callee7, this, [[6, 22]]); | ||
}, _callee7, this, [[6, 24]]); | ||
})); | ||
@@ -491,3 +498,7 @@ | ||
this.store.dispatch((0, _module.setUser)(res.user)); | ||
this.options.onSignedInHook(); | ||
if (this.options.onSignedInHook) { | ||
this.options.onSignedInHook(); | ||
} | ||
if (callback && (0, _lodash.isFunction)(callback)) { | ||
@@ -566,3 +577,6 @@ callback(); | ||
} | ||
this.options.onSignedOutHook(); | ||
if (this.options.onSignedOutHook) { | ||
this.options.onSignedOutHook(); | ||
} | ||
_context10.next = 18; | ||
@@ -569,0 +583,0 @@ break; |
1250
lib/index.js
@@ -1,25 +0,1243 @@ | ||
'use strict'; | ||
(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")); | ||
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"], 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")); | ||
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"]); | ||
})(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__) { | ||
return /******/ (function(modules) { // webpackBootstrap | ||
/******/ // The module cache | ||
/******/ var installedModules = {}; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.reducer = exports.config = exports.AccountsClient = undefined; | ||
/******/ // The require function | ||
/******/ function __webpack_require__(moduleId) { | ||
var _AccountsClient = require('./AccountsClient'); | ||
/******/ // Check if module is in cache | ||
/******/ if(installedModules[moduleId]) | ||
/******/ return installedModules[moduleId].exports; | ||
var _AccountsClient2 = _interopRequireDefault(_AccountsClient); | ||
/******/ // Create a new module (and put it into the cache) | ||
/******/ var module = installedModules[moduleId] = { | ||
/******/ exports: {}, | ||
/******/ id: moduleId, | ||
/******/ loaded: false | ||
/******/ }; | ||
var _config = require('./config'); | ||
/******/ // Execute the module function | ||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); | ||
var _config2 = _interopRequireDefault(_config); | ||
/******/ // Flag the module as loaded | ||
/******/ module.loaded = true; | ||
var _module = require('./module'); | ||
/******/ // Return the exports of the module | ||
/******/ return module.exports; | ||
/******/ } | ||
var _module2 = _interopRequireDefault(_module); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/******/ // expose the modules object (__webpack_modules__) | ||
/******/ __webpack_require__.m = modules; | ||
exports.default = _AccountsClient2.default; | ||
exports.AccountsClient = _AccountsClient.AccountsClient; | ||
exports.config = _config2.default; | ||
exports.reducer = _module2.default; | ||
/******/ // expose the module cache | ||
/******/ __webpack_require__.c = installedModules; | ||
/******/ // __webpack_public_path__ | ||
/******/ __webpack_require__.p = ""; | ||
/******/ // Load entry module and return exports | ||
/******/ return __webpack_require__(0); | ||
/******/ }) | ||
/************************************************************************/ | ||
/******/ ([ | ||
/* 0 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.reducer = exports.config = exports.AccountsClient = undefined; | ||
var _AccountsClient = __webpack_require__(1); | ||
var _AccountsClient2 = _interopRequireDefault(_AccountsClient); | ||
var _config = __webpack_require__(12); | ||
var _config2 = _interopRequireDefault(_config); | ||
var _module = __webpack_require__(19); | ||
var _module2 = _interopRequireDefault(_module); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
exports.default = _AccountsClient2.default; | ||
exports.AccountsClient = _AccountsClient.AccountsClient; | ||
exports.config = _config2.default; | ||
exports.reducer = _module2.default; | ||
/***/ }, | ||
/* 1 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.AccountsClient = undefined; | ||
var _extends2 = __webpack_require__(2); | ||
var _extends3 = _interopRequireDefault(_extends2); | ||
var _regenerator = __webpack_require__(3); | ||
var _regenerator2 = _interopRequireDefault(_regenerator); | ||
var _promise = __webpack_require__(4); | ||
var _promise2 = _interopRequireDefault(_promise); | ||
var _asyncToGenerator2 = __webpack_require__(5); | ||
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); | ||
var _defineProperty2 = __webpack_require__(6); | ||
var _defineProperty3 = _interopRequireDefault(_defineProperty2); | ||
var _classCallCheck2 = __webpack_require__(7); | ||
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | ||
var _createClass2 = __webpack_require__(8); | ||
var _createClass3 = _interopRequireDefault(_createClass2); | ||
var _lodash = __webpack_require__(9); | ||
var _jwtDecode = __webpack_require__(10); | ||
var _jwtDecode2 = _interopRequireDefault(_jwtDecode); | ||
var _common = __webpack_require__(11); | ||
var _config2 = __webpack_require__(12); | ||
var _config3 = _interopRequireDefault(_config2); | ||
var _createStore = __webpack_require__(14); | ||
var _createStore2 = _interopRequireDefault(_createStore); | ||
var _module = __webpack_require__(19); | ||
var _module2 = _interopRequireDefault(_module); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var isValidUserObject = function isValidUserObject(user) { | ||
return (0, _lodash.has)(user, 'user') || (0, _lodash.has)(user, 'email') || (0, _lodash.has)(user, 'id'); | ||
}; | ||
var ACCESS_TOKEN = 'accounts:accessToken'; | ||
var REFRESH_TOKEN = 'accounts:refreshToken'; | ||
var getTokenKey = function getTokenKey(type, options) { | ||
return (0, _lodash.isString)(options.tokenStoragePrefix) && options.tokenStoragePrefix.length > 0 ? options.tokenStoragePrefix + ':' + type : type; | ||
}; | ||
var AccountsClient = exports.AccountsClient = function () { | ||
function AccountsClient(options, transport) { | ||
(0, _classCallCheck3.default)(this, AccountsClient); | ||
this.options = options; | ||
this.storage = options.tokenStorage || _config3.default.tokenStorage; | ||
if (!transport) { | ||
throw new _common.AccountsError('A REST or GraphQL transport is required'); | ||
} | ||
this.transport = transport; | ||
var middleware = options.reduxLogger ? [options.reduxLogger] : []; | ||
var reduxStoreKey = options.reduxStoreKey || _config3.default.reduxStoreKey; | ||
this.store = options.store || (0, _createStore2.default)({ | ||
reducers: (0, _defineProperty3.default)({}, reduxStoreKey, _module2.default), | ||
middleware: middleware | ||
}); | ||
} | ||
(0, _createClass3.default)(AccountsClient, [{ | ||
key: 'getState', | ||
value: function getState() { | ||
var state = this.store.getState(); | ||
if (typeof state.get === 'function') { | ||
return state.get(this.options.reduxStoreKey); | ||
} | ||
return state[this.options.reduxStoreKey]; | ||
} | ||
}, { | ||
key: 'getStorageData', | ||
value: function () { | ||
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(keyName) { | ||
return _regenerator2.default.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
return _context.abrupt('return', _promise2.default.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 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(keyName, value) { | ||
return _regenerator2.default.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
return _context2.abrupt('return', _promise2.default.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 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3(keyName) { | ||
return _regenerator2.default.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
return _context3.abrupt('return', _promise2.default.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; | ||
} | ||
}, { | ||
key: 'tokens', | ||
value: function () { | ||
var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4() { | ||
return _regenerator2.default.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; | ||
}() | ||
}, { | ||
key: 'clearTokens', | ||
value: function () { | ||
var _ref5 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5() { | ||
return _regenerator2.default.wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
this.store.dispatch((0, _module.clearTokens)()); | ||
_context5.next = 3; | ||
return this.removeStorageData(getTokenKey(ACCESS_TOKEN, this.options)); | ||
case 3: | ||
_context5.next = 5; | ||
return this.removeStorageData(getTokenKey(REFRESH_TOKEN, this.options)); | ||
case 5: | ||
case 'end': | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee5, this); | ||
})); | ||
function clearTokens() { | ||
return _ref5.apply(this, arguments); | ||
} | ||
return clearTokens; | ||
}() | ||
}, { | ||
key: 'storeTokens', | ||
value: function () { | ||
var _ref6 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee6(loginResponse) { | ||
var newAccessToken, newRefreshToken; | ||
return _regenerator2.default.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', | ||
value: function clearUser() { | ||
this.store.dispatch((0, _module.clearUser)()); | ||
} | ||
}, { | ||
key: 'resumeSession', | ||
value: function resumeSession() { | ||
// TODO Should there be any additional resume session logic here? | ||
return this.refreshSession(); | ||
} | ||
}, { | ||
key: 'refreshSession', | ||
value: function () { | ||
var _ref7 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee7() { | ||
var _ref8, accessToken, refreshToken, decodedRefreshToken, currentTime, refreshedSession; | ||
return _regenerator2.default.wrap(function _callee7$(_context7) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
case 0: | ||
_context7.next = 2; | ||
return this.tokens(); | ||
case 2: | ||
_ref8 = _context7.sent; | ||
accessToken = _ref8.accessToken; | ||
refreshToken = _ref8.refreshToken; | ||
if (!(accessToken && refreshToken)) { | ||
_context7.next = 33; | ||
break; | ||
} | ||
_context7.prev = 6; | ||
this.store.dispatch((0, _module.loggingIn)(true)); | ||
decodedRefreshToken = (0, _jwtDecode2.default)(refreshToken); | ||
currentTime = Date.now() / 1000; | ||
// Refresh token is expired, user must sign back in | ||
if (!(decodedRefreshToken.exp < currentTime)) { | ||
_context7.next = 15; | ||
break; | ||
} | ||
this.clearTokens(); | ||
this.clearUser(); | ||
_context7.next = 23; | ||
break; | ||
case 15: | ||
_context7.next = 17; | ||
return this.transport.refreshTokens(accessToken, refreshToken); | ||
case 17: | ||
refreshedSession = _context7.sent; | ||
this.store.dispatch((0, _module.loggingIn)(false)); | ||
_context7.next = 21; | ||
return this.storeTokens(refreshedSession); | ||
case 21: | ||
this.store.dispatch((0, _module.setTokens)(refreshedSession.tokens)); | ||
this.store.dispatch((0, _module.setUser)(refreshedSession.user)); | ||
case 23: | ||
_context7.next = 31; | ||
break; | ||
case 25: | ||
_context7.prev = 25; | ||
_context7.t0 = _context7['catch'](6); | ||
this.store.dispatch((0, _module.loggingIn)(false)); | ||
this.clearTokens(); | ||
this.clearUser(); | ||
throw new _common.AccountsError('falsy token provided'); | ||
case 31: | ||
_context7.next = 36; | ||
break; | ||
case 33: | ||
this.clearTokens(); | ||
this.clearUser(); | ||
throw new _common.AccountsError('no tokens provided'); | ||
case 36: | ||
case 'end': | ||
return _context7.stop(); | ||
} | ||
} | ||
}, _callee7, this, [[6, 25]]); | ||
})); | ||
function refreshSession() { | ||
return _ref7.apply(this, arguments); | ||
} | ||
return refreshSession; | ||
}() | ||
}, { | ||
key: 'createUser', | ||
value: function () { | ||
var _ref9 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee8(user, callback) { | ||
var userId; | ||
return _regenerator2.default.wrap(function _callee8$(_context8) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
case 0: | ||
if (!(!user || user.password === undefined)) { | ||
_context8.next = 2; | ||
break; | ||
} | ||
throw new _common.AccountsError('Unrecognized options for create user request', { | ||
username: user && user.username, | ||
email: user && user.email | ||
}, 400); | ||
case 2: | ||
if (_common.validators.validatePassword(user.password)) { | ||
_context8.next = 4; | ||
break; | ||
} | ||
throw new _common.AccountsError('Password is required'); | ||
case 4: | ||
if (!(!_common.validators.validateUsername(user.username) && !_common.validators.validateEmail(user.email))) { | ||
_context8.next = 6; | ||
break; | ||
} | ||
throw new _common.AccountsError('Username or Email is required'); | ||
case 6: | ||
_context8.prev = 6; | ||
_context8.next = 9; | ||
return this.transport.createUser(user); | ||
case 9: | ||
userId = _context8.sent; | ||
if (callback && (0, _lodash.isFunction)(callback)) { | ||
callback(); | ||
} | ||
_context8.next = 13; | ||
return this.loginWithPassword({ id: userId }, user.password); | ||
case 13: | ||
_context8.next = 19; | ||
break; | ||
case 15: | ||
_context8.prev = 15; | ||
_context8.t0 = _context8['catch'](6); | ||
if (callback && (0, _lodash.isFunction)(callback)) { | ||
callback(_context8.t0); | ||
} | ||
throw new _common.AccountsError(_context8.t0.message); | ||
case 19: | ||
case 'end': | ||
return _context8.stop(); | ||
} | ||
} | ||
}, _callee8, this, [[6, 15]]); | ||
})); | ||
function createUser(_x6, _x7) { | ||
return _ref9.apply(this, arguments); | ||
} | ||
return createUser; | ||
}() | ||
}, { | ||
key: 'loginWithPassword', | ||
value: function () { | ||
var _ref10 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee9(user, password, callback) { | ||
var res; | ||
return _regenerator2.default.wrap(function _callee9$(_context9) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
case 0: | ||
if (!(!password || !user)) { | ||
_context9.next = 2; | ||
break; | ||
} | ||
throw new _common.AccountsError('Unrecognized options for login request', user, 400); | ||
case 2: | ||
if (!(!(0, _lodash.isString)(user) && !isValidUserObject(user) || !(0, _lodash.isString)(password))) { | ||
_context9.next = 4; | ||
break; | ||
} | ||
throw new _common.AccountsError('Match failed', user, 400); | ||
case 4: | ||
this.store.dispatch((0, _module.loggingIn)(true)); | ||
_context9.prev = 5; | ||
_context9.next = 8; | ||
return this.transport.loginWithPassword(user, password); | ||
case 8: | ||
res = _context9.sent; | ||
this.store.dispatch((0, _module.loggingIn)(false)); | ||
_context9.next = 12; | ||
return this.storeTokens(res); | ||
case 12: | ||
this.store.dispatch((0, _module.setTokens)(res.tokens)); | ||
this.store.dispatch((0, _module.setUser)(res.user)); | ||
if (this.options.onSignedInHook) { | ||
this.options.onSignedInHook(); | ||
} | ||
if (callback && (0, _lodash.isFunction)(callback)) { | ||
callback(null, res); | ||
} | ||
return _context9.abrupt('return', res); | ||
case 19: | ||
_context9.prev = 19; | ||
_context9.t0 = _context9['catch'](5); | ||
this.store.dispatch((0, _module.loggingIn)(false)); | ||
if (callback && (0, _lodash.isFunction)(callback)) { | ||
callback(_context9.t0, null); | ||
} | ||
throw new _common.AccountsError(_context9.t0.message); | ||
case 24: | ||
case 'end': | ||
return _context9.stop(); | ||
} | ||
} | ||
}, _callee9, this, [[5, 19]]); | ||
})); | ||
function loginWithPassword(_x8, _x9, _x10) { | ||
return _ref10.apply(this, arguments); | ||
} | ||
return loginWithPassword; | ||
}() | ||
}, { | ||
key: 'loggingIn', | ||
value: function loggingIn() { | ||
return this.getState().get('loggingIn'); | ||
} | ||
}, { | ||
key: 'isLoading', | ||
value: function isLoading() { | ||
return this.getState().get('isLoading'); | ||
} | ||
}, { | ||
key: 'logout', | ||
value: function () { | ||
var _ref11 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee10(callback) { | ||
var _ref12, accessToken; | ||
return _regenerator2.default.wrap(function _callee10$(_context10) { | ||
while (1) { | ||
switch (_context10.prev = _context10.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; | ||
return this.transport.logout(accessToken); | ||
case 8: | ||
this.clearTokens(); | ||
this.store.dispatch((0, _module.clearUser)()); | ||
if (callback && (0, _lodash.isFunction)(callback)) { | ||
callback(); | ||
} | ||
if (this.options.onSignedOutHook) { | ||
this.options.onSignedOutHook(); | ||
} | ||
_context10.next = 18; | ||
break; | ||
case 14: | ||
_context10.prev = 14; | ||
_context10.t0 = _context10['catch'](0); | ||
if (callback && (0, _lodash.isFunction)(callback)) { | ||
callback(_context10.t0); | ||
} | ||
throw new _common.AccountsError(_context10.t0.message); | ||
case 18: | ||
case 'end': | ||
return _context10.stop(); | ||
} | ||
} | ||
}, _callee10, this, [[0, 14]]); | ||
})); | ||
function logout(_x11) { | ||
return _ref11.apply(this, arguments); | ||
} | ||
return logout; | ||
}() | ||
}, { | ||
key: 'verifyEmail', | ||
value: function () { | ||
var _ref13 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee11(token) { | ||
return _regenerator2.default.wrap(function _callee11$(_context11) { | ||
while (1) { | ||
switch (_context11.prev = _context11.next) { | ||
case 0: | ||
_context11.prev = 0; | ||
_context11.next = 3; | ||
return this.transport.verifyEmail(token); | ||
case 3: | ||
_context11.next = 8; | ||
break; | ||
case 5: | ||
_context11.prev = 5; | ||
_context11.t0 = _context11['catch'](0); | ||
throw new _common.AccountsError(_context11.t0.message); | ||
case 8: | ||
case 'end': | ||
return _context11.stop(); | ||
} | ||
} | ||
}, _callee11, this, [[0, 5]]); | ||
})); | ||
function verifyEmail(_x12) { | ||
return _ref13.apply(this, arguments); | ||
} | ||
return verifyEmail; | ||
}() | ||
}, { | ||
key: 'resetPassword', | ||
value: function () { | ||
var _ref14 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee12(token, newPassword) { | ||
return _regenerator2.default.wrap(function _callee12$(_context12) { | ||
while (1) { | ||
switch (_context12.prev = _context12.next) { | ||
case 0: | ||
_context12.prev = 0; | ||
_context12.next = 3; | ||
return this.transport.resetPassword(token, newPassword); | ||
case 3: | ||
_context12.next = 8; | ||
break; | ||
case 5: | ||
_context12.prev = 5; | ||
_context12.t0 = _context12['catch'](0); | ||
throw new _common.AccountsError(_context12.t0.message); | ||
case 8: | ||
case 'end': | ||
return _context12.stop(); | ||
} | ||
} | ||
}, _callee12, this, [[0, 5]]); | ||
})); | ||
function resetPassword(_x13, _x14) { | ||
return _ref14.apply(this, arguments); | ||
} | ||
return resetPassword; | ||
}() | ||
}, { | ||
key: 'requestPasswordReset', | ||
value: function () { | ||
var _ref15 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee13(email) { | ||
return _regenerator2.default.wrap(function _callee13$(_context13) { | ||
while (1) { | ||
switch (_context13.prev = _context13.next) { | ||
case 0: | ||
if (email) { | ||
_context13.next = 2; | ||
break; | ||
} | ||
throw new _common.AccountsError('Email must be provided'); | ||
case 2: | ||
_context13.prev = 2; | ||
_context13.next = 5; | ||
return this.transport.sendResetPasswordEmail(email); | ||
case 5: | ||
_context13.next = 10; | ||
break; | ||
case 7: | ||
_context13.prev = 7; | ||
_context13.t0 = _context13['catch'](2); | ||
throw new _common.AccountsError(_context13.t0.message); | ||
case 10: | ||
case 'end': | ||
return _context13.stop(); | ||
} | ||
} | ||
}, _callee13, this, [[2, 7]]); | ||
})); | ||
function requestPasswordReset(_x15) { | ||
return _ref15.apply(this, arguments); | ||
} | ||
return requestPasswordReset; | ||
}() | ||
}, { | ||
key: 'requestVerificationEmail', | ||
value: function () { | ||
var _ref16 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee14(email) { | ||
return _regenerator2.default.wrap(function _callee14$(_context14) { | ||
while (1) { | ||
switch (_context14.prev = _context14.next) { | ||
case 0: | ||
if (email) { | ||
_context14.next = 2; | ||
break; | ||
} | ||
throw new _common.AccountsError('Email must be provided'); | ||
case 2: | ||
_context14.prev = 2; | ||
_context14.next = 5; | ||
return this.transport.sendVerificationEmail(email); | ||
case 5: | ||
_context14.next = 10; | ||
break; | ||
case 7: | ||
_context14.prev = 7; | ||
_context14.t0 = _context14['catch'](2); | ||
throw new _common.AccountsError(_context14.t0.message); | ||
case 10: | ||
case 'end': | ||
return _context14.stop(); | ||
} | ||
} | ||
}, _callee14, this, [[2, 7]]); | ||
})); | ||
function requestVerificationEmail(_x16) { | ||
return _ref16.apply(this, arguments); | ||
} | ||
return requestVerificationEmail; | ||
}() | ||
}]); | ||
return AccountsClient; | ||
}(); | ||
var Accounts = { | ||
instance: AccountsClient, | ||
ui: {}, | ||
config: function config(options, transport) { | ||
this.instance = new AccountsClient((0, _extends3.default)({}, _config3.default, options), transport); | ||
}, | ||
user: function user() { | ||
return this.instance.user(); | ||
}, | ||
options: function options() { | ||
return this.instance.options; | ||
}, | ||
createUser: function createUser(user, callback) { | ||
return this.instance.createUser(user, callback); | ||
}, | ||
loginWithPassword: function loginWithPassword(user, password, callback) { | ||
return this.instance.loginWithPassword(user, password, callback); | ||
}, | ||
loggingIn: function loggingIn() { | ||
return this.instance.loggingIn(); | ||
}, | ||
isLoading: function isLoading() { | ||
return this.instance.isLoading(); | ||
}, | ||
logout: function logout(callback) { | ||
return this.instance.logout(callback); | ||
}, | ||
tokens: function tokens() { | ||
return this.instance.tokens(); | ||
}, | ||
resumeSession: function resumeSession() { | ||
return this.instance.resumeSession(); | ||
}, | ||
refreshSession: function refreshSession() { | ||
return this.instance.refreshSession(); | ||
}, | ||
verifyEmail: function verifyEmail(token) { | ||
return this.instance.verifyEmail(token); | ||
}, | ||
resetPassword: function resetPassword(token, newPassword) { | ||
return this.instance.resetPassword(token, newPassword); | ||
}, | ||
requestPasswordReset: function requestPasswordReset(email) { | ||
return this.instance.requestPasswordReset(email); | ||
}, | ||
requestVerificationEmail: function requestVerificationEmail(email) { | ||
return this.instance.requestVerificationEmail(email); | ||
} | ||
}; | ||
exports.default = Accounts; | ||
// TODO Could this be handled better? | ||
// if (typeof window !== 'undefined') { | ||
// window.onload = async () => { | ||
// if (Accounts.instance && Accounts.instance.resumeSession) { | ||
// await Accounts.resumeSession(); | ||
// } | ||
// }; | ||
// } | ||
/***/ }, | ||
/* 2 */ | ||
/***/ function(module, exports) { | ||
module.exports = require("babel-runtime/helpers/extends"); | ||
/***/ }, | ||
/* 3 */ | ||
/***/ function(module, exports) { | ||
module.exports = require("babel-runtime/regenerator"); | ||
/***/ }, | ||
/* 4 */ | ||
/***/ function(module, exports) { | ||
module.exports = require("babel-runtime/core-js/promise"); | ||
/***/ }, | ||
/* 5 */ | ||
/***/ function(module, exports) { | ||
module.exports = require("babel-runtime/helpers/asyncToGenerator"); | ||
/***/ }, | ||
/* 6 */ | ||
/***/ function(module, exports) { | ||
module.exports = require("babel-runtime/helpers/defineProperty"); | ||
/***/ }, | ||
/* 7 */ | ||
/***/ function(module, exports) { | ||
module.exports = require("babel-runtime/helpers/classCallCheck"); | ||
/***/ }, | ||
/* 8 */ | ||
/***/ function(module, exports) { | ||
module.exports = require("babel-runtime/helpers/createClass"); | ||
/***/ }, | ||
/* 9 */ | ||
/***/ function(module, exports) { | ||
module.exports = require("lodash"); | ||
/***/ }, | ||
/* 10 */ | ||
/***/ function(module, exports) { | ||
module.exports = require("jwt-decode"); | ||
/***/ }, | ||
/* 11 */ | ||
/***/ function(module, exports) { | ||
module.exports = require("@accounts/common"); | ||
/***/ }, | ||
/* 12 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _extends2 = __webpack_require__(2); | ||
var _extends3 = _interopRequireDefault(_extends2); | ||
var _common = __webpack_require__(11); | ||
var _AccountsClient = __webpack_require__(1); | ||
var _AccountsClient2 = _interopRequireDefault(_AccountsClient); | ||
var _redirect = __webpack_require__(13); | ||
var _redirect2 = _interopRequireDefault(_redirect); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
exports.default = (0, _extends3.default)({}, _common.config, { | ||
store: null, | ||
reduxLogger: null, | ||
reduxStoreKey: 'accounts', | ||
tokenStorage: global.localStorage, | ||
server: '', | ||
tokenStoragePrefix: '', | ||
title: '', | ||
requestPermissions: [], | ||
requestOfflineToken: {}, | ||
forceApprovalPrompt: {}, | ||
requireEmailVerification: false, | ||
loginPath: '/', | ||
signUpPath: null, | ||
resetPasswordPath: null, | ||
profilePath: '/', | ||
changePasswordPath: null, | ||
homePath: '/', | ||
signOutPath: '/', | ||
// TODO enable all of these | ||
// onSubmitHook: () => {}, | ||
// onPreSignUpHook: () => new Promise(resolve => resolve()), | ||
// onPostSignUpHook: () => {}, | ||
onEnrollAccountHook: function onEnrollAccountHook() { | ||
return (0, _redirect2.default)(_AccountsClient2.default.options().loginPath || '/'); | ||
}, | ||
onResetPasswordHook: function onResetPasswordHook() { | ||
return (0, _redirect2.default)(_AccountsClient2.default.options().loginPath || '/'); | ||
}, | ||
onVerifyEmailHook: function onVerifyEmailHook() { | ||
return (0, _redirect2.default)(_AccountsClient2.default.options().profilePath || '/'); | ||
}, | ||
onSignedInHook: function onSignedInHook() { | ||
return (0, _redirect2.default)(_AccountsClient2.default.options().homePath || '/'); | ||
}, | ||
onSignedOutHook: function onSignedOutHook() { | ||
return (0, _redirect2.default)(_AccountsClient2.default.options().signOutPath || '/'); | ||
}, | ||
loginOnSignUp: true | ||
}); | ||
/***/ }, | ||
/* 13 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _AccountsClient = __webpack_require__(1); | ||
var _AccountsClient2 = _interopRequireDefault(_AccountsClient); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
// $FlowFixMe | ||
exports.default = function (path) { | ||
return _AccountsClient2.default.options().history.push(path); | ||
}; | ||
// eslint-disable-next-line import/no-named-as-default | ||
/***/ }, | ||
/* 14 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _toConsumableArray2 = __webpack_require__(15); | ||
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2); | ||
var _redux = __webpack_require__(16); | ||
var _immutable = __webpack_require__(17); | ||
var _reduxImmutable = __webpack_require__(18); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
exports.default = function (_ref) { | ||
var _ref$reducers = _ref.reducers, | ||
reducers = _ref$reducers === undefined ? {} : _ref$reducers, | ||
_ref$middleware = _ref.middleware, | ||
middleware = _ref$middleware === undefined ? [] : _ref$middleware, | ||
_ref$state = _ref.state, | ||
state = _ref$state === undefined ? (0, _immutable.Map)() : _ref$state; | ||
return (0, _redux.createStore)((0, _reduxImmutable.combineReducers)(reducers), state, (0, _redux.compose)(_redux.applyMiddleware.apply(undefined, (0, _toConsumableArray3.default)(middleware)))); | ||
}; | ||
/***/ }, | ||
/* 15 */ | ||
/***/ function(module, exports) { | ||
module.exports = require("babel-runtime/helpers/toConsumableArray"); | ||
/***/ }, | ||
/* 16 */ | ||
/***/ function(module, exports) { | ||
module.exports = require("redux"); | ||
/***/ }, | ||
/* 17 */ | ||
/***/ function(module, exports) { | ||
module.exports = require("immutable"); | ||
/***/ }, | ||
/* 18 */ | ||
/***/ function(module, exports) { | ||
module.exports = require("redux-immutable"); | ||
/***/ }, | ||
/* 19 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.clearUser = exports.clearTokens = exports.setTokens = exports.setUser = exports.loggingIn = undefined; | ||
var _immutable = __webpack_require__(17); | ||
var PATH = 'js-accounts/'; | ||
var LOGIN = PATH + 'LOGIN'; | ||
var SET_USER = PATH + 'SET_USER'; | ||
var SET_TOKENS = PATH + 'SET_TOKENS'; | ||
var CLEAR_TOKENS = PATH + 'CLEAR_TOKENS'; | ||
var CLEAR_USER = PATH + 'CLEAR_USER'; | ||
var LOGGING_IN = PATH + 'LOGGING_IN'; | ||
var initialState = (0, _immutable.Map)({ | ||
isLoading: false, | ||
user: null, | ||
tokens: null, | ||
loggingIn: false | ||
}); | ||
var reducer = function reducer() { | ||
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState; | ||
var action = arguments[1]; | ||
var nextState = state; | ||
switch (action.type) { | ||
case LOGIN: | ||
{ | ||
break; | ||
} | ||
case SET_USER: | ||
{ | ||
var user = action.payload.user; | ||
return state.set('user', (0, _immutable.Map)(user)); | ||
} | ||
case SET_TOKENS: | ||
{ | ||
var tokens = action.payload.tokens; | ||
return state.set('tokens', (0, _immutable.Map)(tokens)); | ||
} | ||
case CLEAR_TOKENS: | ||
{ | ||
return state.set('tokens', null); | ||
} | ||
case CLEAR_USER: | ||
{ | ||
return state.set('user', null); | ||
} | ||
case LOGGING_IN: | ||
{ | ||
var isLoggingIn = action.payload.isLoggingIn; | ||
return state.set('loggingIn', isLoggingIn); | ||
} | ||
default: | ||
break; | ||
} | ||
return nextState; | ||
}; | ||
exports.default = reducer; | ||
var loggingIn = exports.loggingIn = function loggingIn(isLoggingIn) { | ||
return { | ||
type: LOGGING_IN, | ||
payload: { | ||
isLoggingIn: isLoggingIn | ||
} | ||
}; | ||
}; | ||
var setUser = exports.setUser = function setUser(user) { | ||
return { | ||
type: SET_USER, | ||
payload: { | ||
user: user | ||
} | ||
}; | ||
}; | ||
var setTokens = exports.setTokens = function setTokens(tokens) { | ||
return { | ||
type: SET_TOKENS, | ||
payload: { | ||
tokens: tokens | ||
} | ||
}; | ||
}; | ||
var clearTokens = exports.clearTokens = function clearTokens() { | ||
return { | ||
type: CLEAR_TOKENS | ||
}; | ||
}; | ||
var clearUser = exports.clearUser = function clearUser() { | ||
return { | ||
type: CLEAR_USER | ||
}; | ||
}; | ||
/***/ } | ||
/******/ ]) | ||
}); | ||
; |
{ | ||
"name": "@accounts/client", | ||
"version": "0.0.8", | ||
"version": "0.0.9-alpha.146b94d6", | ||
"description": "Fullstack authentication and accounts-management", | ||
"main": "lib/index.js", | ||
"jsnext:main": "lib-es6/index.js", | ||
"publishConfig": { | ||
@@ -11,3 +12,5 @@ "access": "public" | ||
"start": "webpack -p --config --progress --watch", | ||
"compile": "babel ./src --out-dir ./lib", | ||
"compile": "npm run compile:es6 && npm run compile:umd", | ||
"compile:es6": "babel ./src --out-dir ./lib-es6", | ||
"compile:umd": "webpack", | ||
"postcompile": "npm run flow:prepublish", | ||
@@ -20,3 +23,3 @@ "prepublish": "npm run compile", | ||
"flow:gen": "flow gen-flow-files ./src/index.js > ./lib/index.js.flow", | ||
"flow:prepublish": "for i in `ls ./src/*.js`; do cp $i `echo $i | sed \"s/src/lib/g\" | sed \"s/js/js\\.flow/g\"`; done", | ||
"flow:prepublish": "for i in `ls ./src/*.js`; do cp $i `echo $i | sed \"s/src/lib-es6/g\" | sed \"s/js/js\\.flow/g\"`; done", | ||
"coverage": "npm run testonly -- --coverage", | ||
@@ -57,4 +60,6 @@ "coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" | ||
"babel-plugin-transform-regenerator": "^6.16.1", | ||
"babel-plugin-transform-runtime": "^6.23.0", | ||
"babel-preset-es2015": "^6.18.0", | ||
"babel-preset-es2015-node4": "^2.1.0", | ||
"babel-preset-stage-0": "^6.22.0", | ||
"coveralls": "^2.11.14", | ||
@@ -70,3 +75,3 @@ "flow-bin": "^0.39.0", | ||
"dependencies": { | ||
"@accounts/common": "^0.0.7", | ||
"@accounts/common": "^0.0.9-alpha.146b94d6", | ||
"immutable": "^3.8.1", | ||
@@ -73,0 +78,0 @@ "jwt-decode": "^2.1.0", |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
289016
44
5532
0
20
+ Added@accounts/common@0.0.9(transitive)
- Removed@accounts/common@0.0.7(transitive)