ackee-redux-token-auth
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -1,24 +0,13 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.authSessionResume = exports.authSessionPause = exports.authSessionEnd = exports.authSessionStart = exports.accessTokenUnavailable = exports.accessTokenAvailable = exports.fetchAuthUserFailure = exports.fetchAuthUserSuccess = exports.fetchAuthUserRequest = exports.stopTokenRefresh = exports.startTokenRefresh = exports.refreshTokens = exports.setTokens = exports.logout = exports.stopLogin = exports.login = void 0; | ||
var _actionType = require("./actionType"); | ||
var login = function login(credentials) { | ||
import { AUTH_LOGIN, AUTH_LOGIN_SUCCESS, AUTH_LOGIN_FAILURE, AUTH_LOGOUT, SET_AUTH_TOKENS, PROCESS_TOKEN_REFRESH, AUTH_REFRESH_TOKEN, AUTH_REFRESH_TOKEN_FAILURE, AUTH_REFRESH_TOKEN_SUCCESS, FETCH_AUTH_USER_REQUEST, FETCH_AUTH_USER_SUCCESS, FETCH_AUTH_USER_FAILURE, AUTH_SESSION_START, AUTH_SESSION_PAUSE, AUTH_SESSION_RESUME, AUTH_SESSION_END, ACCESS_TOKEN_AVAILABLE, ACCESS_TOKEN_UNAVAILABLE } from './actionType'; | ||
export const login = credentials => { | ||
return { | ||
type: _actionType.AUTH_LOGIN, | ||
credentials: credentials | ||
type: AUTH_LOGIN, | ||
credentials | ||
}; | ||
}; | ||
exports.login = login; | ||
var stopLogin = function stopLogin(error, user) { | ||
export const stopLogin = (error, user) => { | ||
if (error) { | ||
return { | ||
type: _actionType.AUTH_LOGIN_FAILURE, | ||
error: error | ||
type: AUTH_LOGIN_FAILURE, | ||
error | ||
}; | ||
@@ -28,51 +17,26 @@ } | ||
return { | ||
type: _actionType.AUTH_LOGIN_SUCCESS, | ||
user: user | ||
type: AUTH_LOGIN_SUCCESS, | ||
user | ||
}; | ||
}; | ||
exports.stopLogin = stopLogin; | ||
var logout = function logout() { | ||
return { | ||
type: _actionType.AUTH_LOGOUT | ||
}; | ||
}; | ||
exports.logout = logout; | ||
var setTokens = function setTokens(tokens) { | ||
return { | ||
type: _actionType.SET_AUTH_TOKENS, | ||
tokens: tokens | ||
}; | ||
}; | ||
exports.setTokens = setTokens; | ||
var refreshTokens = function refreshTokens(tokens) { | ||
return { | ||
type: _actionType.PROCESS_TOKEN_REFRESH, | ||
tokens: tokens | ||
}; | ||
}; | ||
exports.refreshTokens = refreshTokens; | ||
var startTokenRefresh = function startTokenRefresh(refreshToken) { | ||
return { | ||
type: _actionType.AUTH_REFRESH_TOKEN, | ||
refreshToken: refreshToken | ||
}; | ||
}; | ||
exports.startTokenRefresh = startTokenRefresh; | ||
var stopTokenRefresh = function stopTokenRefresh(error) { | ||
var tokens = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
export const logout = () => ({ | ||
type: AUTH_LOGOUT | ||
}); | ||
export const setTokens = tokens => ({ | ||
type: SET_AUTH_TOKENS, | ||
tokens | ||
}); | ||
export const refreshTokens = tokens => ({ | ||
type: PROCESS_TOKEN_REFRESH, | ||
tokens | ||
}); | ||
export const startTokenRefresh = refreshToken => ({ | ||
type: AUTH_REFRESH_TOKEN, | ||
refreshToken | ||
}); | ||
export const stopTokenRefresh = (error, tokens = {}) => { | ||
if (error) { | ||
return { | ||
type: _actionType.AUTH_REFRESH_TOKEN_FAILURE, | ||
error: error | ||
type: AUTH_REFRESH_TOKEN_FAILURE, | ||
error | ||
}; | ||
@@ -82,82 +46,35 @@ } | ||
return { | ||
type: _actionType.AUTH_REFRESH_TOKEN_SUCCESS, | ||
tokens: tokens | ||
type: AUTH_REFRESH_TOKEN_SUCCESS, | ||
tokens | ||
}; | ||
}; | ||
exports.stopTokenRefresh = stopTokenRefresh; | ||
var fetchAuthUserRequest = function fetchAuthUserRequest() { | ||
return { | ||
type: _actionType.FETCH_AUTH_USER_REQUEST | ||
}; | ||
}; | ||
exports.fetchAuthUserRequest = fetchAuthUserRequest; | ||
var fetchAuthUserSuccess = function fetchAuthUserSuccess(user) { | ||
return { | ||
type: _actionType.FETCH_AUTH_USER_SUCCESS, | ||
user: user | ||
}; | ||
}; | ||
exports.fetchAuthUserSuccess = fetchAuthUserSuccess; | ||
var fetchAuthUserFailure = function fetchAuthUserFailure(error) { | ||
return { | ||
type: _actionType.FETCH_AUTH_USER_FAILURE, | ||
error: error | ||
}; | ||
}; | ||
exports.fetchAuthUserFailure = fetchAuthUserFailure; | ||
var accessTokenAvailable = function accessTokenAvailable(accessToken) { | ||
return { | ||
type: _actionType.ACCESS_TOKEN_AVAILABLE, | ||
payload: accessToken | ||
}; | ||
}; | ||
exports.accessTokenAvailable = accessTokenAvailable; | ||
var accessTokenUnavailable = function accessTokenUnavailable() { | ||
return { | ||
type: _actionType.ACCESS_TOKEN_UNAVAILABLE | ||
}; | ||
}; | ||
exports.accessTokenUnavailable = accessTokenUnavailable; | ||
var authSessionStart = function authSessionStart() { | ||
return { | ||
type: _actionType.AUTH_SESSION_START | ||
}; | ||
}; | ||
exports.authSessionStart = authSessionStart; | ||
var authSessionEnd = function authSessionEnd() { | ||
return { | ||
type: _actionType.AUTH_SESSION_END | ||
}; | ||
}; | ||
exports.authSessionEnd = authSessionEnd; | ||
var authSessionPause = function authSessionPause() { | ||
return { | ||
type: _actionType.AUTH_SESSION_PAUSE | ||
}; | ||
}; | ||
exports.authSessionPause = authSessionPause; | ||
var authSessionResume = function authSessionResume() { | ||
return { | ||
type: _actionType.AUTH_SESSION_RESUME | ||
}; | ||
}; | ||
exports.authSessionResume = authSessionResume; | ||
export const fetchAuthUserRequest = () => ({ | ||
type: FETCH_AUTH_USER_REQUEST | ||
}); | ||
export const fetchAuthUserSuccess = user => ({ | ||
type: FETCH_AUTH_USER_SUCCESS, | ||
user | ||
}); | ||
export const fetchAuthUserFailure = error => ({ | ||
type: FETCH_AUTH_USER_FAILURE, | ||
error | ||
}); | ||
export const accessTokenAvailable = accessToken => ({ | ||
type: ACCESS_TOKEN_AVAILABLE, | ||
payload: accessToken | ||
}); | ||
export const accessTokenUnavailable = () => ({ | ||
type: ACCESS_TOKEN_UNAVAILABLE | ||
}); | ||
export const authSessionStart = () => ({ | ||
type: AUTH_SESSION_START | ||
}); | ||
export const authSessionEnd = () => ({ | ||
type: AUTH_SESSION_END | ||
}); | ||
export const authSessionPause = () => ({ | ||
type: AUTH_SESSION_PAUSE | ||
}); | ||
export const authSessionResume = () => ({ | ||
type: AUTH_SESSION_RESUME | ||
}); |
@@ -1,42 +0,18 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.AUTH_SESSION_RESUME = exports.AUTH_SESSION_PAUSE = exports.AUTH_SESSION_END = exports.AUTH_SESSION_START = exports.ACCESS_TOKEN_UNAVAILABLE = exports.ACCESS_TOKEN_AVAILABLE = exports.FETCH_AUTH_USER_FAILURE = exports.FETCH_AUTH_USER_SUCCESS = exports.FETCH_AUTH_USER_REQUEST = exports.PROCESS_TOKEN_REFRESH = exports.AUTH_REFRESH_TOKEN_FAILURE = exports.AUTH_REFRESH_TOKEN_SUCCESS = exports.AUTH_REFRESH_TOKEN = exports.SET_AUTH_TOKENS = exports.AUTH_LOGOUT = exports.AUTH_LOGIN_FAILURE = exports.AUTH_LOGIN_SUCCESS = exports.AUTH_LOGIN = void 0; | ||
var AUTH_LOGIN = '@@redux-token-auth/AUTH_LOGIN'; | ||
exports.AUTH_LOGIN = AUTH_LOGIN; | ||
var AUTH_LOGIN_SUCCESS = '@@redux-token-auth/AUTH_LOGIN_SUCCESS'; | ||
exports.AUTH_LOGIN_SUCCESS = AUTH_LOGIN_SUCCESS; | ||
var AUTH_LOGIN_FAILURE = '@@redux-token-auth/AUTH_LOGIN_FAILURE'; | ||
exports.AUTH_LOGIN_FAILURE = AUTH_LOGIN_FAILURE; | ||
var AUTH_LOGOUT = '@@redux-token-auth/AUTH_LOGOUT'; | ||
exports.AUTH_LOGOUT = AUTH_LOGOUT; | ||
var SET_AUTH_TOKENS = '@@redux-token-auth/SET_AUTH_TOKENS'; | ||
exports.SET_AUTH_TOKENS = SET_AUTH_TOKENS; | ||
var AUTH_REFRESH_TOKEN = '@@redux-token-auth/AUTH_REFRESH_TOKEN'; | ||
exports.AUTH_REFRESH_TOKEN = AUTH_REFRESH_TOKEN; | ||
var AUTH_REFRESH_TOKEN_SUCCESS = '@@redux-token-auth/AUTH_REFRESH_TOKEN_SUCCESS'; | ||
exports.AUTH_REFRESH_TOKEN_SUCCESS = AUTH_REFRESH_TOKEN_SUCCESS; | ||
var AUTH_REFRESH_TOKEN_FAILURE = '@@redux-token-auth/AUTH_REFRESH_TOKEN_FAILURE'; | ||
exports.AUTH_REFRESH_TOKEN_FAILURE = AUTH_REFRESH_TOKEN_FAILURE; | ||
var PROCESS_TOKEN_REFRESH = '@@redux-token-auth/PROCESS_TOKEN_REFRESH'; | ||
exports.PROCESS_TOKEN_REFRESH = PROCESS_TOKEN_REFRESH; | ||
var FETCH_AUTH_USER_REQUEST = '@@redux-token-auth/FETCH_AUTH_USER_REQUEST'; | ||
exports.FETCH_AUTH_USER_REQUEST = FETCH_AUTH_USER_REQUEST; | ||
var FETCH_AUTH_USER_SUCCESS = '@@redux-token-auth/FETCH_AUTH_USER_SUCCESS'; | ||
exports.FETCH_AUTH_USER_SUCCESS = FETCH_AUTH_USER_SUCCESS; | ||
var FETCH_AUTH_USER_FAILURE = '@@redux-token-auth/FETCH_AUTH_USER_FAILURE'; | ||
exports.FETCH_AUTH_USER_FAILURE = FETCH_AUTH_USER_FAILURE; | ||
var ACCESS_TOKEN_AVAILABLE = '@@redux-token-auth/ACCESS_TOKEN_AVAILABLE'; | ||
exports.ACCESS_TOKEN_AVAILABLE = ACCESS_TOKEN_AVAILABLE; | ||
var ACCESS_TOKEN_UNAVAILABLE = '@@redux-token-auth/ACCESS_TOKEN_UNAVAILABLE'; | ||
exports.ACCESS_TOKEN_UNAVAILABLE = ACCESS_TOKEN_UNAVAILABLE; | ||
var AUTH_SESSION_START = '@@redux-token-auth/AUTH_SESSION_START'; | ||
exports.AUTH_SESSION_START = AUTH_SESSION_START; | ||
var AUTH_SESSION_END = '@@redux-token-auth/AUTH_SESSION_END'; | ||
exports.AUTH_SESSION_END = AUTH_SESSION_END; | ||
var AUTH_SESSION_PAUSE = '@@redux-token-auth/AUTH_SESSION_PAUSE'; | ||
exports.AUTH_SESSION_PAUSE = AUTH_SESSION_PAUSE; | ||
var AUTH_SESSION_RESUME = '@@redux-token-auth/AUTH_SESSION_RESUME'; | ||
exports.AUTH_SESSION_RESUME = AUTH_SESSION_RESUME; | ||
export const AUTH_LOGIN = '@@redux-token-auth/AUTH_LOGIN'; | ||
export const AUTH_LOGIN_SUCCESS = '@@redux-token-auth/AUTH_LOGIN_SUCCESS'; | ||
export const AUTH_LOGIN_FAILURE = '@@redux-token-auth/AUTH_LOGIN_FAILURE'; | ||
export const AUTH_LOGOUT = '@@redux-token-auth/AUTH_LOGOUT'; | ||
export const SET_AUTH_TOKENS = '@@redux-token-auth/SET_AUTH_TOKENS'; | ||
export const AUTH_REFRESH_TOKEN = '@@redux-token-auth/AUTH_REFRESH_TOKEN'; | ||
export const AUTH_REFRESH_TOKEN_SUCCESS = '@@redux-token-auth/AUTH_REFRESH_TOKEN_SUCCESS'; | ||
export const AUTH_REFRESH_TOKEN_FAILURE = '@@redux-token-auth/AUTH_REFRESH_TOKEN_FAILURE'; | ||
export const PROCESS_TOKEN_REFRESH = '@@redux-token-auth/PROCESS_TOKEN_REFRESH'; | ||
export const FETCH_AUTH_USER_REQUEST = '@@redux-token-auth/FETCH_AUTH_USER_REQUEST'; | ||
export const FETCH_AUTH_USER_SUCCESS = '@@redux-token-auth/FETCH_AUTH_USER_SUCCESS'; | ||
export const FETCH_AUTH_USER_FAILURE = '@@redux-token-auth/FETCH_AUTH_USER_FAILURE'; | ||
export const ACCESS_TOKEN_AVAILABLE = '@@redux-token-auth/ACCESS_TOKEN_AVAILABLE'; | ||
export const ACCESS_TOKEN_UNAVAILABLE = '@@redux-token-auth/ACCESS_TOKEN_UNAVAILABLE'; | ||
export const AUTH_SESSION_START = '@@redux-token-auth/AUTH_SESSION_START'; | ||
export const AUTH_SESSION_END = '@@redux-token-auth/AUTH_SESSION_END'; | ||
export const AUTH_SESSION_PAUSE = '@@redux-token-auth/AUTH_SESSION_PAUSE'; | ||
export const AUTH_SESSION_RESUME = '@@redux-token-auth/AUTH_SESSION_RESUME'; |
@@ -1,10 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.logger = exports.reducerName = void 0; | ||
var reducerName = 'auth'; | ||
exports.reducerName = reducerName; | ||
var logger = console; | ||
exports.logger = logger; | ||
export const reducerName = 'auth'; | ||
export const logger = console; |
@@ -1,8 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.tokens = void 0; | ||
var tokens = { | ||
export const tokens = { | ||
persistence: { | ||
@@ -13,3 +7,2 @@ NONE: 'NONE', | ||
} | ||
}; | ||
exports.tokens = tokens; | ||
}; |
128
lib/index.js
@@ -1,119 +0,9 @@ | ||
"use strict"; | ||
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _exportNames = { | ||
actionTypes: true, | ||
authUser: true, | ||
isLoggedIn: true, | ||
isLoggingIn: true, | ||
loginErrors: true, | ||
login: true, | ||
stopLogin: true, | ||
logout: true, | ||
setTokens: true, | ||
refreshTokens: true, | ||
reducer: true, | ||
constants: true | ||
}; | ||
Object.defineProperty(exports, "authUser", { | ||
enumerable: true, | ||
get: function get() { | ||
return _selectors.authUser; | ||
} | ||
}); | ||
Object.defineProperty(exports, "isLoggedIn", { | ||
enumerable: true, | ||
get: function get() { | ||
return _selectors.isLoggedIn; | ||
} | ||
}); | ||
Object.defineProperty(exports, "isLoggingIn", { | ||
enumerable: true, | ||
get: function get() { | ||
return _selectors.isLoggingIn; | ||
} | ||
}); | ||
Object.defineProperty(exports, "loginErrors", { | ||
enumerable: true, | ||
get: function get() { | ||
return _selectors.loginErrors; | ||
} | ||
}); | ||
Object.defineProperty(exports, "login", { | ||
enumerable: true, | ||
get: function get() { | ||
return _actions.login; | ||
} | ||
}); | ||
Object.defineProperty(exports, "stopLogin", { | ||
enumerable: true, | ||
get: function get() { | ||
return _actions.stopLogin; | ||
} | ||
}); | ||
Object.defineProperty(exports, "logout", { | ||
enumerable: true, | ||
get: function get() { | ||
return _actions.logout; | ||
} | ||
}); | ||
Object.defineProperty(exports, "setTokens", { | ||
enumerable: true, | ||
get: function get() { | ||
return _actions.setTokens; | ||
} | ||
}); | ||
Object.defineProperty(exports, "refreshTokens", { | ||
enumerable: true, | ||
get: function get() { | ||
return _actions.refreshTokens; | ||
} | ||
}); | ||
Object.defineProperty(exports, "reducer", { | ||
enumerable: true, | ||
get: function get() { | ||
return _reducer.reducer; | ||
} | ||
}); | ||
exports.constants = exports.actionTypes = void 0; | ||
var _actionTypes = _interopRequireWildcard(require("./actionType")); | ||
exports.actionTypes = _actionTypes; | ||
Object.keys(_actionTypes).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function get() { | ||
return _actionTypes[key]; | ||
} | ||
}); | ||
}); | ||
var _selectors = require("./selectors"); | ||
var _actions = require("./actions"); | ||
var _reducer = require("./reducer"); | ||
var _sagas = require("./sagas"); | ||
Object.keys(_sagas).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function get() { | ||
return _sagas[key]; | ||
} | ||
}); | ||
}); | ||
var _constants = _interopRequireWildcard(require("./constants")); | ||
exports.constants = _constants; | ||
export * from './actionType'; | ||
import * as _actionTypes from './actionType'; | ||
export { _actionTypes as actionTypes }; | ||
export { authUser, isLoggedIn, isLoggingIn, loginErrors } from './selectors'; | ||
export { login, stopLogin, logout, setTokens, refreshTokens } from './actions'; | ||
export { reducer } from './reducer'; | ||
export * from './sagas'; | ||
import * as _constants from './constants'; | ||
export { _constants as constants }; |
@@ -1,15 +0,4 @@ | ||
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.reducer = void 0; | ||
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread")); | ||
var _actionType = require("./actionType"); | ||
var initialState = { | ||
import _objectSpread from "@babel/runtime/helpers/objectSpread"; | ||
import { AUTH_LOGIN, AUTH_LOGIN_SUCCESS, AUTH_LOGIN_FAILURE, AUTH_LOGOUT, SET_AUTH_TOKENS, AUTH_REFRESH_TOKEN, AUTH_REFRESH_TOKEN_FAILURE, AUTH_REFRESH_TOKEN_SUCCESS, FETCH_AUTH_USER_REQUEST, FETCH_AUTH_USER_SUCCESS, FETCH_AUTH_USER_FAILURE } from './actionType'; | ||
const initialState = { | ||
user: null, | ||
@@ -23,10 +12,6 @@ isLoggedIn: false, | ||
}; | ||
var reducer = function reducer() { | ||
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState; | ||
var action = arguments.length > 1 ? arguments[1] : undefined; | ||
export const reducer = (state = initialState, action) => { | ||
switch (action.type) { | ||
case _actionType.AUTH_LOGIN: | ||
return (0, _objectSpread2.default)({}, state, { | ||
case AUTH_LOGIN: | ||
return _objectSpread({}, state, { | ||
isLoggedIn: false, | ||
@@ -38,4 +23,4 @@ isLoggingIn: true, | ||
case _actionType.AUTH_LOGIN_SUCCESS: | ||
return (0, _objectSpread2.default)({}, state, { | ||
case AUTH_LOGIN_SUCCESS: | ||
return _objectSpread({}, state, { | ||
isLoggedIn: true, | ||
@@ -48,4 +33,4 @@ isLoggingIn: false, | ||
case _actionType.AUTH_LOGIN_FAILURE: | ||
return (0, _objectSpread2.default)({}, state, { | ||
case AUTH_LOGIN_FAILURE: | ||
return _objectSpread({}, state, { | ||
isLoggedIn: false, | ||
@@ -58,10 +43,10 @@ isLoggingIn: false, | ||
case _actionType.SET_AUTH_TOKENS: | ||
return (0, _objectSpread2.default)({}, state, { | ||
case SET_AUTH_TOKENS: | ||
return _objectSpread({}, state, { | ||
isLoggedIn: true, | ||
tokens: (0, _objectSpread2.default)({}, state.tokens, action.tokens) | ||
tokens: _objectSpread({}, state.tokens, action.tokens) | ||
}); | ||
case _actionType.AUTH_LOGOUT: | ||
return (0, _objectSpread2.default)({}, state, { | ||
case AUTH_LOGOUT: | ||
return _objectSpread({}, state, { | ||
isLoggedIn: false, | ||
@@ -72,25 +57,25 @@ user: null, | ||
case _actionType.AUTH_REFRESH_TOKEN: | ||
return (0, _objectSpread2.default)({}, state, { | ||
case AUTH_REFRESH_TOKEN: | ||
return _objectSpread({}, state, { | ||
isRefreshing: true | ||
}); | ||
case _actionType.AUTH_REFRESH_TOKEN_SUCCESS: | ||
return (0, _objectSpread2.default)({}, state, { | ||
case AUTH_REFRESH_TOKEN_SUCCESS: | ||
return _objectSpread({}, state, { | ||
isRefreshing: false, | ||
tokens: (0, _objectSpread2.default)({}, state.tokens, action.tokens) | ||
tokens: _objectSpread({}, state.tokens, action.tokens) | ||
}); | ||
case _actionType.AUTH_REFRESH_TOKEN_FAILURE: | ||
return (0, _objectSpread2.default)({}, state, { | ||
case AUTH_REFRESH_TOKEN_FAILURE: | ||
return _objectSpread({}, state, { | ||
isRefreshing: false | ||
}); | ||
case _actionType.FETCH_AUTH_USER_REQUEST: | ||
return (0, _objectSpread2.default)({}, state, { | ||
case FETCH_AUTH_USER_REQUEST: | ||
return _objectSpread({}, state, { | ||
isUserFetching: true | ||
}); | ||
case _actionType.FETCH_AUTH_USER_SUCCESS: | ||
return (0, _objectSpread2.default)({}, state, { | ||
case FETCH_AUTH_USER_SUCCESS: | ||
return _objectSpread({}, state, { | ||
user: action.user, | ||
@@ -100,4 +85,4 @@ isUserFetching: false | ||
case _actionType.FETCH_AUTH_USER_FAILURE: | ||
return (0, _objectSpread2.default)({}, state, { | ||
case FETCH_AUTH_USER_FAILURE: | ||
return _objectSpread({}, state, { | ||
isUserFetching: false | ||
@@ -109,4 +94,2 @@ }); | ||
} | ||
}; | ||
exports.reducer = reducer; | ||
}; |
@@ -1,154 +0,45 @@ | ||
"use strict"; | ||
import _objectSpread from "@babel/runtime/helpers/objectSpread"; | ||
import { select, call, take } from 'redux-saga/effects'; | ||
import { authTokens, authUser, isLoggedIn, isRefreshing } from '../selectors'; | ||
import { AUTH_REFRESH_TOKEN_FAILURE, AUTH_REFRESH_TOKEN_SUCCESS } from '../actionType'; | ||
import config from './config'; | ||
import { tryToRefreshTokens } from './processTokenRefresh'; | ||
export default function* authorizedFn(fn) { | ||
const processFn = function* () { | ||
const tokens = yield select(authTokens); | ||
const user = yield select(authUser); | ||
return yield call(() => fn(_objectSpread({}, tokens, { | ||
user | ||
}))); | ||
}; // If token refreshing is being processed, wait for it to finish | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = authorizedFn; | ||
if (yield select(isRefreshing)) { | ||
yield take([AUTH_REFRESH_TOKEN_FAILURE, AUTH_REFRESH_TOKEN_SUCCESS]); | ||
} | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
try { | ||
const result = yield processFn(); | ||
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread")); | ||
if (config.detectShouldRefresh(null, result)) { | ||
yield tryToRefreshTokens(); | ||
var _effects = require("redux-saga/effects"); | ||
if (yield select(isLoggedIn)) { | ||
return yield processFn(); | ||
} | ||
} | ||
var _selectors = require("../selectors"); | ||
return result; | ||
} catch (e) { | ||
if (config.detectShouldRefresh(e, null)) { | ||
yield tryToRefreshTokens(); | ||
var _actionType = require("../actionType"); | ||
if (yield select(isLoggedIn)) { | ||
return yield processFn(); | ||
} | ||
} // None of my business, pass error along | ||
var _config = _interopRequireDefault(require("./config")); | ||
var _processTokenRefresh = require("./processTokenRefresh"); | ||
var _marked = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(authorizedFn); | ||
function authorizedFn(fn) { | ||
var processFn, result; | ||
return _regenerator.default.wrap(function authorizedFn$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
processFn = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function processFn() { | ||
var tokens, user; | ||
return _regenerator.default.wrap(function processFn$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return (0, _effects.select)(_selectors.authTokens); | ||
case 2: | ||
tokens = _context.sent; | ||
_context.next = 5; | ||
return (0, _effects.select)(_selectors.authUser); | ||
case 5: | ||
user = _context.sent; | ||
_context.next = 8; | ||
return (0, _effects.call)(function () { | ||
return fn((0, _objectSpread2.default)({}, tokens, { | ||
user: user | ||
})); | ||
}); | ||
case 8: | ||
return _context.abrupt("return", _context.sent); | ||
case 9: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, processFn, this); | ||
}); // If token refreshing is being processed, wait for it to finish | ||
_context2.next = 3; | ||
return (0, _effects.select)(_selectors.isRefreshing); | ||
case 3: | ||
if (!_context2.sent) { | ||
_context2.next = 6; | ||
break; | ||
} | ||
_context2.next = 6; | ||
return (0, _effects.take)([_actionType.AUTH_REFRESH_TOKEN_FAILURE, _actionType.AUTH_REFRESH_TOKEN_SUCCESS]); | ||
case 6: | ||
_context2.prev = 6; | ||
_context2.next = 9; | ||
return processFn(); | ||
case 9: | ||
result = _context2.sent; | ||
if (!_config.default.detectShouldRefresh(null, result)) { | ||
_context2.next = 19; | ||
break; | ||
} | ||
_context2.next = 13; | ||
return (0, _processTokenRefresh.tryToRefreshTokens)(); | ||
case 13: | ||
_context2.next = 15; | ||
return (0, _effects.select)(_selectors.isLoggedIn); | ||
case 15: | ||
if (!_context2.sent) { | ||
_context2.next = 19; | ||
break; | ||
} | ||
_context2.next = 18; | ||
return processFn(); | ||
case 18: | ||
return _context2.abrupt("return", _context2.sent); | ||
case 19: | ||
return _context2.abrupt("return", result); | ||
case 22: | ||
_context2.prev = 22; | ||
_context2.t0 = _context2["catch"](6); | ||
if (!_config.default.detectShouldRefresh(_context2.t0, null)) { | ||
_context2.next = 33; | ||
break; | ||
} | ||
_context2.next = 27; | ||
return (0, _processTokenRefresh.tryToRefreshTokens)(); | ||
case 27: | ||
_context2.next = 29; | ||
return (0, _effects.select)(_selectors.isLoggedIn); | ||
case 29: | ||
if (!_context2.sent) { | ||
_context2.next = 33; | ||
break; | ||
} | ||
_context2.next = 32; | ||
return processFn(); | ||
case 32: | ||
return _context2.abrupt("return", _context2.sent); | ||
case 33: | ||
throw _context2.t0; | ||
case 34: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _marked, this, [[6, 22]]); | ||
throw e; | ||
} | ||
} |
@@ -1,224 +0,68 @@ | ||
"use strict"; | ||
import { put, call, all, actionChannel } from 'redux-saga/effects'; | ||
import * as types from '../../actionType'; | ||
import { accessTokenAvailable, accessTokenUnavailable, authSessionStart, authSessionEnd, authSessionPause, authSessionResume } from '../../actions'; | ||
import { simpleCircuit, deepCircuit } from './circuits'; | ||
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); | ||
function* tokenAvailabilityCircuit() { | ||
const tokenAvailabilityUnits = [{ | ||
pattern: types.SET_AUTH_TOKENS, | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
*task(action) { | ||
const { | ||
token | ||
} = action.tokens.accessToken; | ||
yield put(accessTokenAvailable(token)); | ||
} | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.getAuthStateChannel = getAuthStateChannel; | ||
exports.default = _callee; | ||
}, { | ||
pattern: [types.AUTH_REFRESH_TOKEN, types.AUTH_LOGOUT, types.AUTH_LOGIN_FAILURE, types.FETCH_AUTH_USER_FAILURE], | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
*task() { | ||
yield put(accessTokenUnavailable()); | ||
} | ||
var _effects = require("redux-saga/effects"); | ||
}]; | ||
yield call(simpleCircuit, tokenAvailabilityUnits); | ||
} | ||
var types = _interopRequireWildcard(require("../../actionType")); | ||
function* authSessionCircuit() { | ||
const authSessionUnits = [{ | ||
pattern: types.SET_AUTH_TOKENS, | ||
var _actions = require("../../actions"); | ||
*task() { | ||
yield put(authSessionStart()); | ||
} | ||
var _circuits = require("./circuits"); | ||
}, { | ||
pattern: [types.AUTH_LOGOUT, types.AUTH_LOGIN_FAILURE, types.FETCH_AUTH_USER_FAILURE], | ||
var _marked = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(tokenAvailabilityCircuit), | ||
_marked2 = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(authSessionCircuit), | ||
_marked3 = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(getAuthStateChannel), | ||
_marked4 = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(_callee); | ||
*task() { | ||
yield put(authSessionEnd()); | ||
} | ||
function tokenAvailabilityCircuit() { | ||
var tokenAvailabilityUnits; | ||
return _regenerator.default.wrap(function tokenAvailabilityCircuit$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
tokenAvailabilityUnits = [{ | ||
pattern: types.SET_AUTH_TOKENS, | ||
task: | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function task(action) { | ||
var token; | ||
return _regenerator.default.wrap(function task$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
token = action.tokens.accessToken.token; | ||
_context.next = 3; | ||
return (0, _effects.put)((0, _actions.accessTokenAvailable)(token)); | ||
}]; | ||
const authSessionInterruptionUnits = [{ | ||
pattern: types.AUTH_REFRESH_TOKEN, | ||
case 3: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, task, this); | ||
}) | ||
}, { | ||
pattern: [types.AUTH_REFRESH_TOKEN, types.AUTH_LOGOUT], | ||
task: | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function task() { | ||
return _regenerator.default.wrap(function task$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_context2.next = 2; | ||
return (0, _effects.put)((0, _actions.accessTokenUnavailable)()); | ||
*task() { | ||
yield put(authSessionPause()); | ||
} | ||
case 2: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, task, this); | ||
}) | ||
}]; | ||
_context3.next = 3; | ||
return (0, _effects.fork)(_circuits.simpleCircuit, tokenAvailabilityUnits); | ||
}, { | ||
pattern: types.AUTH_REFRESH_TOKEN_SUCCESS, | ||
case 3: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
*task() { | ||
yield put(authSessionResume()); | ||
} | ||
}, _marked, this); | ||
} | ||
function authSessionCircuit() { | ||
var authSessionUnits, authSessionInterruptionUnits; | ||
return _regenerator.default.wrap(function authSessionCircuit$(_context8) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
case 0: | ||
authSessionUnits = [{ | ||
pattern: types.SET_AUTH_TOKENS, | ||
task: | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function task() { | ||
return _regenerator.default.wrap(function task$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
_context4.next = 2; | ||
return (0, _effects.put)((0, _actions.authSessionStart)()); | ||
case 2: | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
} | ||
}, task, this); | ||
}) | ||
}, { | ||
pattern: types.AUTH_LOGOUT, | ||
task: | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function task() { | ||
return _regenerator.default.wrap(function task$(_context5) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
_context5.next = 2; | ||
return (0, _effects.put)((0, _actions.authSessionEnd)()); | ||
case 2: | ||
case "end": | ||
return _context5.stop(); | ||
} | ||
} | ||
}, task, this); | ||
}) | ||
}]; | ||
authSessionInterruptionUnits = [{ | ||
pattern: types.AUTH_REFRESH_TOKEN, | ||
task: | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function task() { | ||
return _regenerator.default.wrap(function task$(_context6) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
_context6.next = 2; | ||
return (0, _effects.put)((0, _actions.authSessionPause)()); | ||
case 2: | ||
case "end": | ||
return _context6.stop(); | ||
} | ||
} | ||
}, task, this); | ||
}) | ||
}, { | ||
pattern: types.AUTH_REFRESH_TOKEN_SUCCESS, | ||
task: | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function task() { | ||
return _regenerator.default.wrap(function task$(_context7) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
case 0: | ||
_context7.next = 2; | ||
return (0, _effects.put)((0, _actions.authSessionResume)()); | ||
case 2: | ||
case "end": | ||
return _context7.stop(); | ||
} | ||
} | ||
}, task, this); | ||
}) | ||
}]; | ||
_context8.next = 4; | ||
return (0, _effects.fork)(_circuits.deepCircuit, [authSessionUnits[0]].concat(authSessionInterruptionUnits, [authSessionUnits[1]])); | ||
case 4: | ||
case "end": | ||
return _context8.stop(); | ||
} | ||
} | ||
}, _marked2, this); | ||
}]; | ||
yield call(deepCircuit, [authSessionUnits[0], ...authSessionInterruptionUnits, authSessionUnits[1]]); | ||
} | ||
function getAuthStateChannel() { | ||
var authStateChannel; | ||
return _regenerator.default.wrap(function getAuthStateChannel$(_context9) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
case 0: | ||
_context9.next = 2; | ||
return (0, _effects.actionChannel)([types.AUTH_SESSION_START, types.AUTH_SESSION_PAUSE, types.AUTH_SESSION_RESUME, types.AUTH_SESSION_END, types.ACCESS_TOKEN_AVAILABLE, types.ACCESS_TOKEN_UNAVAILABLE]); | ||
case 2: | ||
authStateChannel = _context9.sent; | ||
return _context9.abrupt("return", authStateChannel); | ||
case 4: | ||
case "end": | ||
return _context9.stop(); | ||
} | ||
} | ||
}, _marked3, this); | ||
export function* getAuthStateChannel() { | ||
const authStateChannel = yield actionChannel([types.AUTH_SESSION_START, types.AUTH_SESSION_PAUSE, types.AUTH_SESSION_RESUME, types.AUTH_SESSION_END, types.ACCESS_TOKEN_AVAILABLE, types.ACCESS_TOKEN_UNAVAILABLE]); | ||
return authStateChannel; | ||
} | ||
function _callee() { | ||
return _regenerator.default.wrap(function _callee$(_context10) { | ||
while (1) { | ||
switch (_context10.prev = _context10.next) { | ||
case 0: | ||
_context10.next = 2; | ||
return (0, _effects.all)([tokenAvailabilityCircuit(), authSessionCircuit()]); | ||
case 2: | ||
case "end": | ||
return _context10.stop(); | ||
} | ||
} | ||
}, _marked4, this); | ||
export default function* () { | ||
yield all([tokenAvailabilityCircuit(), authSessionCircuit()]); | ||
} |
@@ -1,28 +0,2 @@ | ||
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.simpleCircuit = simpleCircuit; | ||
exports.deepCircuit = deepCircuit; | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
var _effects = require("redux-saga/effects"); | ||
var _marked = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(runUnit), | ||
_marked2 = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(runUnits), | ||
_marked3 = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(simpleCircuit), | ||
_marked4 = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(deepCircuit); | ||
import { call, take, race } from 'redux-saga/effects'; | ||
/** | ||
@@ -32,23 +6,9 @@ * | ||
*/ | ||
function runUnit(_ref) { | ||
var pattern, task, result; | ||
return _regenerator.default.wrap(function runUnit$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
pattern = _ref.pattern, task = _ref.task; | ||
_context.next = 3; | ||
return (0, _effects.take)(pattern); | ||
case 3: | ||
result = _context.sent; | ||
_context.next = 6; | ||
return (0, _effects.call)(task, result); | ||
case 6: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _marked, this); | ||
function* runUnit({ | ||
pattern, | ||
task | ||
}) { | ||
const result = yield take(pattern); | ||
yield call(task, result); | ||
} | ||
@@ -61,78 +21,6 @@ /** | ||
function runUnits() { | ||
var units, | ||
_iteratorNormalCompletion, | ||
_didIteratorError, | ||
_iteratorError, | ||
_iterator, | ||
_step, | ||
unit, | ||
_args2 = arguments; | ||
return _regenerator.default.wrap(function runUnits$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
units = _args2.length > 0 && _args2[0] !== undefined ? _args2[0] : []; | ||
_iteratorNormalCompletion = true; | ||
_didIteratorError = false; | ||
_iteratorError = undefined; | ||
_context2.prev = 4; | ||
_iterator = units[Symbol.iterator](); | ||
case 6: | ||
if (_iteratorNormalCompletion = (_step = _iterator.next()).done) { | ||
_context2.next = 13; | ||
break; | ||
} | ||
unit = _step.value; | ||
_context2.next = 10; | ||
return (0, _effects.call)(runUnit, unit); | ||
case 10: | ||
_iteratorNormalCompletion = true; | ||
_context2.next = 6; | ||
break; | ||
case 13: | ||
_context2.next = 19; | ||
break; | ||
case 15: | ||
_context2.prev = 15; | ||
_context2.t0 = _context2["catch"](4); | ||
_didIteratorError = true; | ||
_iteratorError = _context2.t0; | ||
case 19: | ||
_context2.prev = 19; | ||
_context2.prev = 20; | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
} | ||
case 22: | ||
_context2.prev = 22; | ||
if (!_didIteratorError) { | ||
_context2.next = 25; | ||
break; | ||
} | ||
throw _iteratorError; | ||
case 25: | ||
return _context2.finish(22); | ||
case 26: | ||
return _context2.finish(19); | ||
case 27: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _marked2, this, [[4, 15, 19, 27], [20,, 22, 26]]); | ||
function* runUnits(units = []) { | ||
for (const unit of units) { | ||
yield call(runUnit, unit); | ||
} | ||
} | ||
@@ -153,30 +41,6 @@ /** | ||
function simpleCircuit() { | ||
var units, | ||
_args3 = arguments; | ||
return _regenerator.default.wrap(function simpleCircuit$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
units = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : []; | ||
case 1: | ||
if (!true) { | ||
_context3.next = 6; | ||
break; | ||
} | ||
_context3.next = 4; | ||
return runUnits(units); | ||
case 4: | ||
_context3.next = 1; | ||
break; | ||
case 6: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _marked3, this); | ||
export function* simpleCircuit(units = []) { | ||
while (true) { | ||
yield runUnits(units); | ||
} | ||
} | ||
@@ -192,59 +56,19 @@ /** | ||
export function* deepCircuit(units = []) { | ||
if (units.length < 3) { | ||
throw new Error(`The 'units' array must include at least 3 units, not only ${units.length} unit/s.`); | ||
} | ||
function deepCircuit() { | ||
var units, | ||
firstUnit, | ||
intermediateUnits, | ||
lastUnit, | ||
result, | ||
_args4 = arguments; | ||
return _regenerator.default.wrap(function deepCircuit$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
units = _args4.length > 0 && _args4[0] !== undefined ? _args4[0] : []; | ||
const firstUnit = units[0]; | ||
const intermediateUnits = units.slice(1, units.length - 1); | ||
const lastUnit = units[units.length - 1]; | ||
if (!(units.length < 3)) { | ||
_context4.next = 3; | ||
break; | ||
} | ||
throw new Error("The 'units' array must include at least 3 units, not only ".concat(units.length, " unit/s.")); | ||
case 3: | ||
firstUnit = units[0]; | ||
intermediateUnits = units.slice(1, units.length - 1); | ||
lastUnit = units[units.length - 1]; | ||
case 6: | ||
if (!true) { | ||
_context4.next = 16; | ||
break; | ||
} | ||
_context4.next = 9; | ||
return runUnit(firstUnit); | ||
case 9: | ||
_context4.next = 11; | ||
return (0, _effects.race)({ | ||
deepUnits: (0, _effects.call)(simpleCircuit, intermediateUnits), | ||
lastUnitResult: (0, _effects.take)(lastUnit.pattern) | ||
}); | ||
case 11: | ||
result = _context4.sent; | ||
_context4.next = 14; | ||
return (0, _effects.call)(lastUnit.task, result.lastUnitResult); | ||
case 14: | ||
_context4.next = 6; | ||
break; | ||
case 16: | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _marked4, this); | ||
while (true) { | ||
yield runUnit(firstUnit); | ||
const result = yield race({ | ||
deepUnits: call(simpleCircuit, intermediateUnits), | ||
lastUnitResult: take(lastUnit.pattern) | ||
}); | ||
yield call(lastUnit.task, result.lastUnitResult); | ||
} | ||
} |
@@ -1,31 +0,2 @@ | ||
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "authState", { | ||
enumerable: true, | ||
get: function get() { | ||
return _authState.default; | ||
} | ||
}); | ||
Object.defineProperty(exports, "getAuthStateChannel", { | ||
enumerable: true, | ||
get: function get() { | ||
return _authState.getAuthStateChannel; | ||
} | ||
}); | ||
Object.defineProperty(exports, "withAuthSession", { | ||
enumerable: true, | ||
get: function get() { | ||
return _withAuthSession.default; | ||
} | ||
}); | ||
var _authState = _interopRequireWildcard(require("./authState")); | ||
var _withAuthSession = _interopRequireDefault(require("./withAuthSession")); | ||
export { default as authState, getAuthStateChannel } from './authState'; | ||
export { default as withAuthSession } from './withAuthSession'; |
@@ -1,50 +0,11 @@ | ||
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = withAuthSession; | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
var _effects = require("redux-saga/effects"); | ||
var _actionType = require("../../actionType"); | ||
var _marked = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(withAuthSession); | ||
function withAuthSession(task) { | ||
return _regenerator.default.wrap(function withAuthSession$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
if (!true) { | ||
_context.next = 7; | ||
break; | ||
} | ||
_context.next = 3; | ||
return (0, _effects.take)(_actionType.AUTH_SESSION_START); | ||
case 3: | ||
_context.next = 5; | ||
return (0, _effects.race)({ | ||
task: (0, _effects.call)(task), | ||
abort: (0, _effects.take)(_actionType.AUTH_SESSION_END) | ||
}); | ||
case 5: | ||
_context.next = 0; | ||
break; | ||
case 7: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _marked, this); | ||
import { take, race, call } from 'redux-saga/effects'; | ||
import { AUTH_SESSION_START, AUTH_SESSION_END } from '../../actionType'; | ||
export default function* withAuthSession(task) { | ||
while (true) { | ||
yield take(AUTH_SESSION_START); | ||
yield race({ | ||
task: call(task), | ||
abort: take(AUTH_SESSION_END) | ||
}); | ||
} | ||
} |
@@ -1,8 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var config = { | ||
const config = { | ||
// key for a local storage | ||
@@ -16,3 +10,2 @@ tokensKey: 'tokens', | ||
}; | ||
var _default = config; | ||
exports.default = _default; | ||
export default config; |
@@ -1,24 +0,8 @@ | ||
"use strict"; | ||
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = configure; | ||
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread")); | ||
var _config = require("../config"); | ||
var _config2 = _interopRequireDefault(require("./config")); | ||
var Consts = _interopRequireWildcard(require("../constants")); | ||
var _RefreshTokensTimeout = _interopRequireDefault(require("./tokens/RefreshTokensTimeout")); | ||
var defaultOptions = { | ||
tokens: (0, _objectSpread2.default)({}, _RefreshTokensTimeout.default.options, { | ||
import _objectSpread from "@babel/runtime/helpers/objectSpread"; | ||
import { logger } from '../config'; | ||
import config from './config'; | ||
import * as Consts from '../constants'; | ||
import RefreshTokensTimeout from './tokens/RefreshTokensTimeout'; | ||
const defaultOptions = { | ||
tokens: _objectSpread({}, RefreshTokensTimeout.options, { | ||
persistence: Consts.tokens.persistence.LOCAL | ||
@@ -28,21 +12,18 @@ }) | ||
var isNotFn = function isNotFn(val) { | ||
return typeof val !== 'function'; | ||
}; | ||
const isNotFn = val => typeof val !== 'function'; | ||
function configure() { | ||
var customConfig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var customOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var authenticate = customConfig.authenticate, | ||
refreshTokens = customConfig.refreshTokens, | ||
shouldRefresh = customConfig.shouldRefresh, | ||
getAuthUser = customConfig.getAuthUser; | ||
_config2.default.options = (0, _objectSpread2.default)({}, defaultOptions, customOptions, { | ||
tokens: (0, _objectSpread2.default)({}, defaultOptions.tokens, customOptions.tokens) | ||
export default function configure(customConfig = {}, customOptions = {}) { | ||
const { | ||
authenticate, | ||
refreshTokens, | ||
shouldRefresh, | ||
getAuthUser | ||
} = customConfig; | ||
config.options = _objectSpread({}, defaultOptions, customOptions, { | ||
tokens: _objectSpread({}, defaultOptions.tokens, customOptions.tokens) | ||
}); | ||
if (isNotFn(authenticate)) { | ||
_config2.default.remoteLogin = function (credentials) { | ||
_config.logger.error("Cannot authenticate use with ".concat(credentials, ": Supply authenticate function first.")); | ||
config.remoteLogin = credentials => { | ||
logger.error(`Cannot authenticate use with ${credentials}: Supply authenticate function first.`); | ||
return { | ||
@@ -54,32 +35,29 @@ user: null, | ||
} else { | ||
_config2.default.remoteLogin = authenticate; | ||
config.remoteLogin = authenticate; | ||
} | ||
if (isNotFn(refreshTokens)) { | ||
_config2.default.remoteRefreshTokens = function (tokens) { | ||
_config.logger.error('Cannot refresh tokens. No refresh tokens fn supplied.'); | ||
config.remoteRefreshTokens = tokens => { | ||
logger.error('Cannot refresh tokens. No refresh tokens fn supplied.'); | ||
return tokens; | ||
}; | ||
} else { | ||
_config2.default.remoteRefreshTokens = refreshTokens; | ||
config.remoteRefreshTokens = refreshTokens; | ||
} | ||
if (isNotFn(shouldRefresh)) { | ||
_config2.default.detectShouldRefresh = function (error) { | ||
return !!error; | ||
}; | ||
config.detectShouldRefresh = () => true; | ||
} else { | ||
_config2.default.detectShouldRefresh = shouldRefresh; | ||
config.detectShouldRefresh = shouldRefresh; | ||
} | ||
if (isNotFn(getAuthUser)) { | ||
_config2.default.remoteGetAuthUser = function () { | ||
if (_config2.default.options.tokens.persistence === Consts.tokens.persistence.LOCAL) { | ||
_config.logger.error("'getAuthUser' is not a function. Tokens persistence is set to '".concat(Consts.tokens.persistence.LOCAL, "'. Change persistence to '").concat(Consts.tokens.persistence.NONE, "' or provide function for fetching authorized user.")); | ||
config.remoteGetAuthUser = () => { | ||
if (config.options.tokens.persistence === Consts.tokens.persistence.LOCAL) { | ||
logger.error(`'getAuthUser' is not a function. Tokens persistence is set to '${Consts.tokens.persistence.LOCAL}'. Change persistence to '${Consts.tokens.persistence.NONE}' or provide function for fetching authorized user.`); | ||
} | ||
}; | ||
} else { | ||
_config2.default.remoteGetAuthUser = getAuthUser; | ||
config.remoteGetAuthUser = getAuthUser; | ||
} | ||
} |
@@ -1,90 +0,21 @@ | ||
"use strict"; | ||
import { take, put } from 'redux-saga/effects'; | ||
import { FETCH_AUTH_USER_REQUEST } from '../actionType'; | ||
import { fetchAuthUserFailure, fetchAuthUserSuccess } from '../actions'; | ||
import config from './config'; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = _callee; | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
var _effects = require("redux-saga/effects"); | ||
var _actionType = require("../actionType"); | ||
var _actions = require("../actions"); | ||
var _config = _interopRequireDefault(require("./config")); | ||
var _marked = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(fetchAuthUser), | ||
_marked2 = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(_callee); | ||
function fetchAuthUser() { | ||
var user; | ||
return _regenerator.default.wrap(function fetchAuthUser$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.prev = 0; | ||
_context.next = 3; | ||
return _config.default.remoteGetAuthUser(); | ||
case 3: | ||
user = _context.sent; | ||
_context.next = 6; | ||
return (0, _effects.put)((0, _actions.fetchAuthUserSuccess)(user)); | ||
case 6: | ||
_context.next = 13; | ||
break; | ||
case 8: | ||
_context.prev = 8; | ||
_context.t0 = _context["catch"](0); | ||
_context.next = 12; | ||
return (0, _effects.put)((0, _actions.fetchAuthUserFailure)(_context.t0)); | ||
case 12: | ||
throw _context.t0; | ||
case 13: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _marked, this, [[0, 8]]); | ||
function* fetchAuthUser() { | ||
try { | ||
const user = yield config.remoteGetAuthUser(); | ||
yield put(fetchAuthUserSuccess(user)); | ||
} catch (e) { | ||
yield put(fetchAuthUserFailure(e)); | ||
throw e; | ||
} | ||
} | ||
function _callee() { | ||
return _regenerator.default.wrap(function _callee$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
if (!true) { | ||
_context2.next = 7; | ||
break; | ||
} | ||
_context2.next = 3; | ||
return (0, _effects.take)(_actionType.FETCH_AUTH_USER_REQUEST); | ||
case 3: | ||
_context2.next = 5; | ||
return fetchAuthUser(); | ||
case 5: | ||
_context2.next = 0; | ||
break; | ||
case 7: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _marked2, this); | ||
export default function* () { | ||
while (true) { | ||
yield take(FETCH_AUTH_USER_REQUEST); | ||
yield fetchAuthUser(); | ||
} | ||
} |
@@ -1,87 +0,23 @@ | ||
"use strict"; | ||
import { put, call, takeEvery } from 'redux-saga/effects'; | ||
import { stopLogin, setTokens } from '../actions'; | ||
import { AUTH_LOGIN } from '../actionType'; | ||
import { logger } from '../config'; | ||
import config from './config'; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
const handleLogin = function* (action) { | ||
try { | ||
const { | ||
user, | ||
tokens | ||
} = yield call(() => config.remoteLogin(action.credentials)); | ||
yield put(stopLogin(null, user)); | ||
yield put(setTokens(tokens)); | ||
} catch (e) { | ||
yield put(stopLogin(e)); | ||
logger.warn(`Failed to login user: ${e.message}`); | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = _callee; | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
var _effects = require("redux-saga/effects"); | ||
var _actions = require("../actions"); | ||
var _actionType = require("../actionType"); | ||
var _config = require("../config"); | ||
var _config2 = _interopRequireDefault(require("./config")); | ||
var _marked = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(_callee); | ||
var handleLogin = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function handleLogin(action) { | ||
var _ref, user, tokens; | ||
return _regenerator.default.wrap(function handleLogin$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.prev = 0; | ||
_context.next = 3; | ||
return (0, _effects.call)(function () { | ||
return _config2.default.remoteLogin(action.credentials); | ||
}); | ||
case 3: | ||
_ref = _context.sent; | ||
user = _ref.user; | ||
tokens = _ref.tokens; | ||
_context.next = 8; | ||
return (0, _effects.put)((0, _actions.stopLogin)(null, user)); | ||
case 8: | ||
_context.next = 10; | ||
return (0, _effects.put)((0, _actions.setTokens)(tokens)); | ||
case 10: | ||
_context.next = 17; | ||
break; | ||
case 12: | ||
_context.prev = 12; | ||
_context.t0 = _context["catch"](0); | ||
_context.next = 16; | ||
return (0, _effects.put)((0, _actions.stopLogin)(_context.t0)); | ||
case 16: | ||
_config.logger.warn("Failed to login user: ".concat(_context.t0.message)); | ||
case 17: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, handleLogin, this, [[0, 12]]); | ||
}); | ||
function _callee() { | ||
return _regenerator.default.wrap(function _callee$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_context2.next = 2; | ||
return (0, _effects.takeEvery)(_actionType.AUTH_LOGIN, handleLogin); | ||
case 2: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _marked, this); | ||
export default function* () { | ||
yield takeEvery(AUTH_LOGIN, handleLogin); | ||
} |
@@ -1,72 +0,12 @@ | ||
"use strict"; | ||
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.saga = saga; | ||
Object.defineProperty(exports, "withAuthSession", { | ||
enumerable: true, | ||
get: function get() { | ||
return _authState.withAuthSession; | ||
} | ||
}); | ||
Object.defineProperty(exports, "getAuthStateChannel", { | ||
enumerable: true, | ||
get: function get() { | ||
return _authState.getAuthStateChannel; | ||
} | ||
}); | ||
Object.defineProperty(exports, "configure", { | ||
enumerable: true, | ||
get: function get() { | ||
return _configure.default; | ||
} | ||
}); | ||
Object.defineProperty(exports, "authorizedFn", { | ||
enumerable: true, | ||
get: function get() { | ||
return _authorizedFn.default; | ||
} | ||
}); | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
var _effects = require("redux-saga/effects"); | ||
var _tokens = _interopRequireWildcard(require("./tokens")); | ||
var _handleLogin = _interopRequireDefault(require("./handleLogin")); | ||
var _processTokenRefresh = _interopRequireDefault(require("./processTokenRefresh")); | ||
var _fetchAuthUser = _interopRequireDefault(require("./fetchAuthUser")); | ||
var _authState = require("./authState"); | ||
var _configure = _interopRequireDefault(require("./configure")); | ||
var _authorizedFn = _interopRequireDefault(require("./authorizedFn")); | ||
var _marked = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(saga); | ||
function saga() { | ||
return _regenerator.default.wrap(function saga$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return (0, _effects.all)([(0, _authState.authState)(), (0, _handleLogin.default)(), (0, _processTokenRefresh.default)(), (0, _fetchAuthUser.default)(), (0, _tokens.default)(), (0, _tokens.tryToRetrieveTokens)()]); | ||
case 2: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _marked, this); | ||
import { all } from 'redux-saga/effects'; | ||
import tokens, { tryToRetrieveTokens } from './tokens'; | ||
import handleLogin from './handleLogin'; | ||
import processTokenRefresh from './processTokenRefresh'; | ||
import fetchAuthUser from './fetchAuthUser'; | ||
import { authState } from './authState'; | ||
export { withAuthSession, getAuthStateChannel } from './authState'; | ||
export { default as configure } from './configure'; | ||
export { default as authorizedFn } from './authorizedFn'; | ||
export function* saga() { | ||
yield all([authState(), tokens(), handleLogin(), processTokenRefresh(), fetchAuthUser(), tryToRetrieveTokens()]); | ||
} |
@@ -1,96 +0,22 @@ | ||
"use strict"; | ||
import _objectSpread from "@babel/runtime/helpers/objectSpread"; | ||
import { put, select, takeEvery } from 'redux-saga/effects'; | ||
import { logout, startTokenRefresh, setTokens, stopTokenRefresh } from '../actions'; | ||
import { PROCESS_TOKEN_REFRESH } from '../actionType'; | ||
import { authTokens } from '../selectors'; | ||
import config from './config'; | ||
export function* tryToRefreshTokens(action) { | ||
const tokens = yield select(authTokens); | ||
yield put(startTokenRefresh(tokens)); | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.tryToRefreshTokens = tryToRefreshTokens; | ||
exports.default = _callee; | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread")); | ||
var _effects = require("redux-saga/effects"); | ||
var _actions = require("../actions"); | ||
var _actionType = require("../actionType"); | ||
var _selectors = require("../selectors"); | ||
var _config = _interopRequireDefault(require("./config")); | ||
var _marked = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(tryToRefreshTokens), | ||
_marked2 = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(_callee); | ||
function tryToRefreshTokens(action) { | ||
var tokens, refreshedTokens; | ||
return _regenerator.default.wrap(function tryToRefreshTokens$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return (0, _effects.select)(_selectors.authTokens); | ||
case 2: | ||
tokens = _context.sent; | ||
_context.next = 5; | ||
return (0, _effects.put)((0, _actions.startTokenRefresh)(tokens)); | ||
case 5: | ||
_context.prev = 5; | ||
_context.next = 8; | ||
return _config.default.remoteRefreshTokens((0, _objectSpread2.default)({}, action.tokens, tokens)); | ||
case 8: | ||
refreshedTokens = _context.sent; | ||
_context.next = 11; | ||
return (0, _effects.put)((0, _actions.setTokens)(refreshedTokens)); | ||
case 11: | ||
_context.next = 13; | ||
return (0, _effects.put)((0, _actions.stopTokenRefresh)(null, refreshedTokens)); | ||
case 13: | ||
_context.next = 21; | ||
break; | ||
case 15: | ||
_context.prev = 15; | ||
_context.t0 = _context["catch"](5); | ||
_context.next = 19; | ||
return (0, _effects.put)((0, _actions.stopTokenRefresh)(_context.t0)); | ||
case 19: | ||
_context.next = 21; | ||
return (0, _effects.put)((0, _actions.logout)()); | ||
case 21: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _marked, this, [[5, 15]]); | ||
try { | ||
const refreshedTokens = yield config.remoteRefreshTokens(_objectSpread({}, action.tokens, tokens)); | ||
yield put(setTokens(refreshedTokens)); | ||
yield put(stopTokenRefresh(null, refreshedTokens)); | ||
} catch (refreshError) { | ||
yield put(stopTokenRefresh(refreshError)); | ||
yield put(logout()); | ||
} | ||
} | ||
function _callee() { | ||
return _regenerator.default.wrap(function _callee$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_context2.next = 2; | ||
return (0, _effects.takeEvery)(_actionType.PROCESS_TOKEN_REFRESH, tryToRefreshTokens); | ||
case 2: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _marked2, this); | ||
export default function* () { | ||
yield takeEvery(PROCESS_TOKEN_REFRESH, tryToRefreshTokens); | ||
} |
@@ -1,23 +0,2 @@ | ||
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "default", { | ||
enumerable: true, | ||
get: function get() { | ||
return _tokens.default; | ||
} | ||
}); | ||
Object.defineProperty(exports, "tryToRetrieveTokens", { | ||
enumerable: true, | ||
get: function get() { | ||
return _tryToRetrieveTokens.default; | ||
} | ||
}); | ||
var _tokens = _interopRequireDefault(require("./tokens")); | ||
var _tryToRetrieveTokens = _interopRequireDefault(require("./tryToRetrieveTokens")); | ||
export { default } from './tokens'; | ||
export { default as tryToRetrieveTokens } from './tryToRetrieveTokens'; |
@@ -1,112 +0,82 @@ | ||
"use strict"; | ||
import _objectSpread from "@babel/runtime/helpers/objectSpread"; | ||
import _defineProperty from "@babel/runtime/helpers/defineProperty"; | ||
import { eventChannel } from 'redux-saga'; | ||
import { findMinExpiration, calcTimeout, validateTimeoutValue } from './utilities'; | ||
export default class RefreshTokensTimeout { | ||
constructor(options) { | ||
this.options = _objectSpread({}, RefreshTokensTimeout.options, options); | ||
this.timerId = null; | ||
} | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
calcTimeoutValue(tokens) { | ||
const expiration = findMinExpiration(tokens); | ||
const timeoutValue = calcTimeout(expiration, this.options.requestDurationEstimate); | ||
return timeoutValue; | ||
} | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
clearTimeout() { | ||
if (this.timerId) { | ||
window.clearTimeout(this.timerId); | ||
this.timerId = null; | ||
} | ||
} | ||
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread")); | ||
makeTimeoutChannel(timeout) { | ||
const timeoutChannel = eventChannel(emitter => { | ||
this.timerId = window.setTimeout(() => { | ||
// emitter callback must be provided | ||
// with an argument | ||
emitter(timeout); | ||
}, timeout); // return unsubscribe method | ||
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); | ||
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
var _reduxSaga = require("redux-saga"); | ||
var _utilities = require("./utilities"); | ||
var RefreshTokensTimeout = | ||
/*#__PURE__*/ | ||
function () { | ||
function RefreshTokensTimeout(options) { | ||
(0, _classCallCheck2.default)(this, RefreshTokensTimeout); | ||
this.options = (0, _objectSpread2.default)({}, RefreshTokensTimeout.options, options); | ||
this.timerId = null; | ||
return this.clearTimeout; | ||
}); | ||
return timeoutChannel; | ||
} | ||
/** | ||
* validate tokens expiration with 'requestDurationEstimate' | ||
* and 'minRequiredExpiration' in count | ||
* @param {Object} tokens | ||
* @returns {Boolean} | ||
*/ | ||
(0, _createClass2.default)(RefreshTokensTimeout, [{ | ||
key: "calcTimeoutValue", | ||
value: function calcTimeoutValue(tokens) { | ||
var expiration = (0, _utilities.findMinExpiration)(tokens); | ||
var timeoutValue = (0, _utilities.calcTimeout)(expiration, this.options.requestDurationEstimate); | ||
return timeoutValue; | ||
} | ||
}, { | ||
key: "clearTimeout", | ||
value: function clearTimeout() { | ||
if (this.timerId) { | ||
window.clearTimeout(this.timerId); | ||
this.timerId = null; | ||
} | ||
} | ||
}, { | ||
key: "makeTimeoutChannel", | ||
value: function makeTimeoutChannel(timeout) { | ||
var _this = this; | ||
var timeoutChannel = (0, _reduxSaga.eventChannel)(function (emitter) { | ||
_this.timerId = window.setTimeout(function () { | ||
// emitter callback must be provided | ||
// with an argument | ||
emitter(timeout); | ||
}, timeout); // return unsubscribe method | ||
validateExpiration(tokens) { | ||
const timeoutValue = this.calcTimeoutValue(tokens); | ||
return _this.clearTimeout; | ||
try { | ||
validateTimeoutValue({ | ||
timeoutValue, | ||
options: this.options, | ||
tokens | ||
}); | ||
return timeoutChannel; | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
/** | ||
* validate tokens expiration with 'requestDurationEstimate' | ||
* and 'minRequiredExpiration' in count | ||
* @param {Object} tokens | ||
* @returns {Boolean} | ||
*/ | ||
} | ||
/** | ||
* 1. reset any previous timeout | ||
* 2. find token with the smallest expiration value | ||
* 3. calculate timeout value | ||
* 4. set timeout for refreshing tokens | ||
* @param {Object} [tokens={}] | ||
*/ | ||
}, { | ||
key: "validateExpiration", | ||
value: function validateExpiration(tokens) { | ||
var timeoutValue = this.calcTimeoutValue(tokens); | ||
try { | ||
(0, _utilities.validateTimeoutValue)({ | ||
timeoutValue: timeoutValue, | ||
options: this.options, | ||
tokens: tokens | ||
}); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
/** | ||
* 1. reset any previous timeout | ||
* 2. find token with the smallest expiration value | ||
* 3. calculate timeout value | ||
* 4. set timeout for refreshing tokens | ||
* @param {Object} [tokens={}] | ||
*/ | ||
setTimeout(tokens) { | ||
this.clearTimeout(); | ||
const timeoutValue = this.calcTimeoutValue(tokens); | ||
validateTimeoutValue({ | ||
timeoutValue, | ||
options: this.options, | ||
tokens | ||
}); | ||
const timeoutChannel = this.makeTimeoutChannel(timeoutValue); | ||
return timeoutChannel; | ||
} | ||
}, { | ||
key: "setTimeout", | ||
value: function setTimeout(tokens) { | ||
this.clearTimeout(); | ||
var timeoutValue = this.calcTimeoutValue(tokens); | ||
(0, _utilities.validateTimeoutValue)({ | ||
timeoutValue: timeoutValue, | ||
options: this.options, | ||
tokens: tokens | ||
}); | ||
var timeoutChannel = this.makeTimeoutChannel(timeoutValue); | ||
return timeoutChannel; | ||
} | ||
}]); | ||
return RefreshTokensTimeout; | ||
}(); | ||
} | ||
exports.default = RefreshTokensTimeout; | ||
(0, _defineProperty2.default)(RefreshTokensTimeout, "options", { | ||
_defineProperty(RefreshTokensTimeout, "options", { | ||
// refresh tokens 0.5s before token expires | ||
@@ -113,0 +83,0 @@ requestDurationEstimate: 500, |
@@ -1,117 +0,39 @@ | ||
"use strict"; | ||
import localforage from 'localforage'; | ||
import * as Consts from '../../constants'; | ||
import config from '../config'; | ||
export function* clearTokens() { | ||
window.sessionStorage.removeItem(config.tokensKey); | ||
yield localforage.removeItem(config.tokensKey); | ||
} | ||
export function* storeTokens(tokens) { | ||
switch (config.options.tokens.persistence) { | ||
case Consts.tokens.persistence.LOCAL: | ||
window.sessionStorage.removeItem(config.tokensKey); | ||
yield localforage.setItem(config.tokensKey, tokens); | ||
break; | ||
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); | ||
case Consts.tokens.persistence.SESSION: | ||
yield localforage.removeItem(config.tokensKey); | ||
window.sessionStorage.setItem(config.tokensKey, JSON.stringify(tokens)); | ||
break; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.clearTokens = clearTokens; | ||
exports.storeTokens = storeTokens; | ||
exports.retrieveTokens = retrieveTokens; | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
var _localforage = _interopRequireDefault(require("localforage")); | ||
var Consts = _interopRequireWildcard(require("../../constants")); | ||
var _config = _interopRequireDefault(require("../config")); | ||
var _marked = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(clearTokens), | ||
_marked2 = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(storeTokens), | ||
_marked3 = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(retrieveTokens); | ||
function clearTokens() { | ||
return _regenerator.default.wrap(function clearTokens$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
window.sessionStorage.removeItem(_config.default.tokensKey); | ||
_context.next = 3; | ||
return _localforage.default.removeItem(_config.default.tokensKey); | ||
case 3: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _marked, this); | ||
default: | ||
} | ||
} | ||
export function* retrieveTokens() { | ||
switch (config.options.tokens.persistence) { | ||
case Consts.tokens.persistence.LOCAL: | ||
window.sessionStorage.removeItem(config.tokensKey); | ||
return yield localforage.getItem(config.tokensKey); | ||
function storeTokens(tokens) { | ||
return _regenerator.default.wrap(function storeTokens$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
_context2.t0 = _config.default.options.tokens.persistence; | ||
_context2.next = _context2.t0 === Consts.tokens.persistence.LOCAL ? 3 : _context2.t0 === Consts.tokens.persistence.SESSION ? 7 : 11; | ||
break; | ||
case 3: | ||
window.sessionStorage.removeItem(_config.default.tokensKey); | ||
_context2.next = 6; | ||
return _localforage.default.setItem(_config.default.tokensKey, tokens); | ||
case 6: | ||
return _context2.abrupt("break", 11); | ||
case 7: | ||
_context2.next = 9; | ||
return _localforage.default.removeItem(_config.default.tokensKey); | ||
case 9: | ||
window.sessionStorage.setItem(_config.default.tokensKey, JSON.stringify(tokens)); | ||
return _context2.abrupt("break", 11); | ||
case 11: | ||
case "end": | ||
return _context2.stop(); | ||
case Consts.tokens.persistence.SESSION: | ||
{ | ||
yield localforage.removeItem(config.tokensKey); | ||
const stringifiedTokens = window.sessionStorage.getItem(config.tokensKey); | ||
return JSON.parse(stringifiedTokens); | ||
} | ||
} | ||
}, _marked2, this); | ||
} | ||
function retrieveTokens() { | ||
var stringifiedTokens; | ||
return _regenerator.default.wrap(function retrieveTokens$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
_context3.t0 = _config.default.options.tokens.persistence; | ||
_context3.next = _context3.t0 === Consts.tokens.persistence.LOCAL ? 3 : _context3.t0 === Consts.tokens.persistence.SESSION ? 7 : 11; | ||
break; | ||
case 3: | ||
window.sessionStorage.removeItem(_config.default.tokensKey); | ||
_context3.next = 6; | ||
return _localforage.default.getItem(_config.default.tokensKey); | ||
case 6: | ||
return _context3.abrupt("return", _context3.sent); | ||
case 7: | ||
_context3.next = 9; | ||
return _localforage.default.removeItem(_config.default.tokensKey); | ||
case 9: | ||
stringifiedTokens = window.sessionStorage.getItem(_config.default.tokensKey); | ||
return _context3.abrupt("return", JSON.parse(stringifiedTokens)); | ||
case 11: | ||
return _context3.abrupt("return", null); | ||
case 12: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _marked3, this); | ||
default: | ||
return null; | ||
} | ||
} |
@@ -1,118 +0,30 @@ | ||
"use strict"; | ||
import { take, put, cancel, fork, takeEvery, race, all } from 'redux-saga/effects'; | ||
import { refreshTokens } from '../../actions'; | ||
import { SET_AUTH_TOKENS, ACCESS_TOKEN_UNAVAILABLE, AUTH_REFRESH_TOKEN_FAILURE } from '../../actionType'; | ||
import { logger } from '../../config'; | ||
import config from '../config'; | ||
import RefreshTokensTimeout from './RefreshTokensTimeout'; | ||
import { storeTokens, clearTokens } from './storageHandlers'; | ||
import { hasExpirationProperty } from './utilities'; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = tokensActionsHandlers; | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
var _effects = require("redux-saga/effects"); | ||
var _actions = require("../../actions"); | ||
var _actionType = require("../../actionType"); | ||
var _config = require("../../config"); | ||
var _config2 = _interopRequireDefault(require("../config")); | ||
var _RefreshTokensTimeout = _interopRequireDefault(require("./RefreshTokensTimeout")); | ||
var _storageHandlers = require("./storageHandlers"); | ||
var _utilities = require("./utilities"); | ||
var _marked = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(handleTimeoutChannel), | ||
_marked2 = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(cancelTimeoutChannnelTask), | ||
_marked3 = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(clearTokensHandler), | ||
_marked4 = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(setTokensHandler), | ||
_marked5 = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(tokensActionsHandlers); | ||
function handleTimeoutChannel(timeoutChannel) { | ||
return _regenerator.default.wrap(function handleTimeoutChannel$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
if (!true) { | ||
_context.next = 7; | ||
break; | ||
} | ||
_context.next = 3; | ||
return (0, _effects.take)(timeoutChannel); | ||
case 3: | ||
_context.next = 5; | ||
return (0, _effects.put)((0, _actions.refreshTokens)()); | ||
case 5: | ||
_context.next = 0; | ||
break; | ||
case 7: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _marked, this); | ||
function* handleTimeoutChannel(timeoutChannel) { | ||
while (true) { | ||
yield take(timeoutChannel); | ||
yield put(refreshTokens()); | ||
} | ||
} | ||
var timeoutChannelTask = null; | ||
let timeoutChannelTask = null; | ||
function cancelTimeoutChannnelTask() { | ||
return _regenerator.default.wrap(function cancelTimeoutChannnelTask$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
if (!timeoutChannelTask) { | ||
_context2.next = 4; | ||
break; | ||
} | ||
_context2.next = 3; | ||
return (0, _effects.cancel)(timeoutChannelTask); | ||
case 3: | ||
timeoutChannelTask = null; | ||
case 4: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _marked2, this); | ||
function* cancelTimeoutChannnelTask() { | ||
if (timeoutChannelTask) { | ||
yield cancel(timeoutChannelTask); | ||
timeoutChannelTask = null; | ||
} | ||
} | ||
function clearTokensHandler(refreshTokensTimeout) { | ||
return _regenerator.default.wrap(function clearTokensHandler$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
refreshTokensTimeout.clearTimeout(); | ||
_context3.next = 3; | ||
return (0, _storageHandlers.clearTokens)(); | ||
case 3: | ||
_context3.next = 5; | ||
return cancelTimeoutChannnelTask(); | ||
case 5: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _marked3, this); | ||
function* clearTokensHandler(refreshTokensTimeout) { | ||
refreshTokensTimeout.clearTimeout(); | ||
yield clearTokens(); | ||
yield cancelTimeoutChannnelTask(); | ||
} | ||
@@ -127,101 +39,44 @@ | ||
function setTokensHandler(_ref, refreshTokensTimeout) { | ||
var tokens, min, minRequired, cantSet, channel; | ||
return _regenerator.default.wrap(function setTokensHandler$(_context4) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
tokens = _ref.tokens; | ||
clearLocaleStorageTokens(); // store tokens to a local storage | ||
function* setTokensHandler({ | ||
tokens | ||
}, refreshTokensTimeout) { | ||
clearLocaleStorageTokens(); // store tokens to a local storage | ||
_context4.next = 4; | ||
return (0, _storageHandlers.storeTokens)(tokens); | ||
yield storeTokens(tokens); | ||
case 4: | ||
if (!(0, _utilities.hasExpirationProperty)(tokens)) { | ||
_context4.next = 19; | ||
break; | ||
} | ||
if (hasExpirationProperty(tokens)) { | ||
if (!refreshTokensTimeout.validateExpiration(tokens)) { | ||
const min = config.options.tokens.minRequiredExpiration; | ||
const minRequired = `Minimal required access token expiration is ${min}ms (at ${new Date(Date.now() + min)}).`; | ||
const cantSet = `Access token expiration at ${tokens.accessToken.expiration} it too low.`; | ||
logger.warn(`${minRequired}\n${cantSet}`); | ||
return; | ||
} // cancel any previous timeout | ||
if (refreshTokensTimeout.validateExpiration(tokens)) { | ||
_context4.next = 11; | ||
break; | ||
} | ||
min = _config2.default.options.tokens.minRequiredExpiration; | ||
minRequired = "Minimal required access token expiration is ".concat(min, "ms (at ").concat(new Date(Date.now() + min), ")."); | ||
cantSet = "Access token expiration at ".concat(tokens.accessToken.expiration, " it too low."); | ||
yield cancelTimeoutChannnelTask(); // create new timeout for refreshing tokens | ||
_config.logger.warn("".concat(minRequired, "\n").concat(cantSet)); | ||
const channel = refreshTokensTimeout.setTimeout(tokens); // and wait for token to expire | ||
return _context4.abrupt("return"); | ||
case 11: | ||
_context4.next = 13; | ||
return cancelTimeoutChannnelTask(); | ||
case 13: | ||
// create new timeout for refreshing tokens | ||
channel = refreshTokensTimeout.setTimeout(tokens); // and wait for token to expire | ||
_context4.next = 16; | ||
return (0, _effects.fork)(handleTimeoutChannel, channel); | ||
case 16: | ||
timeoutChannelTask = _context4.sent; | ||
_context4.next = 21; | ||
break; | ||
case 19: | ||
_context4.next = 21; | ||
return cancelTimeoutChannnelTask(); | ||
case 21: | ||
case "end": | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _marked4, this); | ||
timeoutChannelTask = yield fork(handleTimeoutChannel, channel); | ||
} else { | ||
yield cancelTimeoutChannnelTask(); | ||
} | ||
} | ||
function tokensActionsHandlers() { | ||
var _config$options$token, requestDurationEstimate, minRequiredExpiration, refreshTokensTimeout; | ||
return _regenerator.default.wrap(function tokensActionsHandlers$(_context6) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
_config$options$token = _config2.default.options.tokens, requestDurationEstimate = _config$options$token.requestDurationEstimate, minRequiredExpiration = _config$options$token.minRequiredExpiration; | ||
refreshTokensTimeout = new _RefreshTokensTimeout.default({ | ||
requestDurationEstimate: requestDurationEstimate, | ||
minRequiredExpiration: minRequiredExpiration | ||
}); | ||
_context6.next = 4; | ||
return (0, _effects.all)([(0, _effects.takeEvery)(_actionType.SET_AUTH_TOKENS, | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function setAuthTokens(action) { | ||
return _regenerator.default.wrap(function setAuthTokens$(_context5) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
_context5.next = 2; | ||
return (0, _effects.race)({ | ||
task: setTokensHandler(action, refreshTokensTimeout), | ||
abort: (0, _effects.take)(_actionType.AUTH_LOGOUT) | ||
}); | ||
case 2: | ||
case "end": | ||
return _context5.stop(); | ||
} | ||
} | ||
}, setAuthTokens, this); | ||
})), (0, _effects.takeEvery)(_actionType.AUTH_LOGOUT, clearTokensHandler, refreshTokensTimeout)]); | ||
case 4: | ||
case "end": | ||
return _context6.stop(); | ||
} | ||
} | ||
}, _marked5, this); | ||
export default function* tokensActionsHandlers() { | ||
const { | ||
requestDurationEstimate, | ||
minRequiredExpiration | ||
} = config.options.tokens; | ||
const refreshTokensTimeout = new RefreshTokensTimeout({ | ||
requestDurationEstimate, | ||
minRequiredExpiration | ||
}); | ||
yield all([takeEvery(SET_AUTH_TOKENS, function* setAuthTokens(action) { | ||
yield race({ | ||
task: setTokensHandler(action, refreshTokensTimeout), | ||
abort: take([ACCESS_TOKEN_UNAVAILABLE, AUTH_REFRESH_TOKEN_FAILURE]) | ||
}); | ||
}), takeEvery([ACCESS_TOKEN_UNAVAILABLE, AUTH_REFRESH_TOKEN_FAILURE], clearTokensHandler, refreshTokensTimeout)]); | ||
} |
@@ -1,95 +0,31 @@ | ||
"use strict"; | ||
import { take, race, put } from 'redux-saga/effects'; | ||
import { AUTH_LOGIN_SUCCESS } from '../../actionType'; | ||
import { refreshTokens, setTokens, fetchAuthUserRequest } from '../../actions'; | ||
import * as Consts from '../../constants'; | ||
import config from '../config'; | ||
import { retrieveTokens, clearTokens } from './storageHandlers'; | ||
import { isAnyTokenExpired } from './utilities'; | ||
export default function* tryToRetrieveTokens() { | ||
if (config.options.tokens.persistence === Consts.tokens.persistence.NONE) { | ||
yield clearTokens(); | ||
return; | ||
} | ||
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); | ||
const { | ||
tokens | ||
} = yield race({ | ||
tokens: retrieveTokens(), | ||
loginSuccess: take(AUTH_LOGIN_SUCCESS) | ||
}); | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
if (!tokens) { | ||
return; | ||
} | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = tryToRetrieveTokens; | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
var _effects = require("redux-saga/effects"); | ||
var _actionType = require("../../actionType"); | ||
var _actions = require("../../actions"); | ||
var Consts = _interopRequireWildcard(require("../../constants")); | ||
var _config = _interopRequireDefault(require("../config")); | ||
var _storageHandlers = require("./storageHandlers"); | ||
var _utilities = require("./utilities"); | ||
var _marked = | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(tryToRetrieveTokens); | ||
function tryToRetrieveTokens() { | ||
var _ref, tokens; | ||
return _regenerator.default.wrap(function tryToRetrieveTokens$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
if (!(_config.default.options.tokens.persistence === Consts.tokens.persistence.NONE)) { | ||
_context.next = 4; | ||
break; | ||
} | ||
_context.next = 3; | ||
return (0, _storageHandlers.clearTokens)(); | ||
case 3: | ||
return _context.abrupt("return"); | ||
case 4: | ||
_context.next = 6; | ||
return (0, _effects.race)({ | ||
tokens: (0, _storageHandlers.retrieveTokens)(), | ||
loginSuccess: (0, _effects.take)(_actionType.AUTH_LOGIN_SUCCESS) | ||
}); | ||
case 6: | ||
_ref = _context.sent; | ||
tokens = _ref.tokens; | ||
if (tokens) { | ||
_context.next = 10; | ||
break; | ||
} | ||
return _context.abrupt("return"); | ||
case 10: | ||
if (!(0, _utilities.isAnyTokenExpired)(tokens)) { | ||
_context.next = 15; | ||
break; | ||
} | ||
_context.next = 13; | ||
return (0, _effects.put)((0, _actions.refreshTokens)(tokens)); | ||
case 13: | ||
_context.next = 19; | ||
break; | ||
case 15: | ||
_context.next = 17; | ||
return (0, _effects.put)((0, _actions.setTokens)(tokens)); | ||
case 17: | ||
_context.next = 19; | ||
return (0, _effects.put)((0, _actions.fetchAuthUserRequest)()); | ||
case 19: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _marked, this); | ||
if (isAnyTokenExpired(tokens)) { | ||
yield put(refreshTokens(tokens)); | ||
} else { | ||
yield put(setTokens(tokens)); | ||
yield put(fetchAuthUserRequest()); | ||
} | ||
} |
@@ -1,33 +0,22 @@ | ||
"use strict"; | ||
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.findMinExpiration = findMinExpiration; | ||
exports.calcTimeout = calcTimeout; | ||
exports.validateTimeoutValue = validateTimeoutValue; | ||
exports.hasExpirationProperty = exports.isAnyTokenExpired = void 0; | ||
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); | ||
var isTokenExpired = function isTokenExpired(token) { | ||
const isTokenExpired = token => { | ||
return token.expiration && new Date(token.expiration) <= new Date(); | ||
}; | ||
var isAnyTokenExpired = function isAnyTokenExpired(_ref) { | ||
var refreshToken = _ref.refreshToken, | ||
tokens = (0, _objectWithoutProperties2.default)(_ref, ["refreshToken"]); | ||
export const isAnyTokenExpired = (_ref) => { | ||
let { | ||
refreshToken | ||
} = _ref, | ||
tokens = _objectWithoutProperties(_ref, ["refreshToken"]); | ||
return Object.values(tokens).some(isTokenExpired); | ||
}; | ||
export const hasExpirationProperty = (_ref2) => { | ||
let { | ||
refreshTokens | ||
} = _ref2, | ||
tokens = _objectWithoutProperties(_ref2, ["refreshTokens"]); | ||
exports.isAnyTokenExpired = isAnyTokenExpired; | ||
var hasExpirationProperty = function hasExpirationProperty(_ref2) { | ||
var refreshTokens = _ref2.refreshTokens, | ||
tokens = (0, _objectWithoutProperties2.default)(_ref2, ["refreshTokens"]); | ||
return Object.values(tokens).some(function (token) { | ||
return Boolean(token.expiration); | ||
}); | ||
return Object.values(tokens).some(token => Boolean(token.expiration)); | ||
}; | ||
@@ -38,15 +27,16 @@ /** | ||
export function findMinExpiration(_ref3) { | ||
let { | ||
refreshToken | ||
} = _ref3, | ||
tokens = _objectWithoutProperties(_ref3, ["refreshToken"]); | ||
exports.hasExpirationProperty = hasExpirationProperty; | ||
const values = Object.values(tokens); | ||
let minExpiration = values[0].expiration; | ||
function findMinExpiration(_ref3) { | ||
var refreshToken = _ref3.refreshToken, | ||
tokens = (0, _objectWithoutProperties2.default)(_ref3, ["refreshToken"]); | ||
var values = Object.values(tokens); | ||
var minExpiration = values[0].expiration; | ||
for (const _ref4 of values) { | ||
const { | ||
expiration | ||
} = _ref4; | ||
for (var _i = 0; _i < values.length; _i++) { | ||
var _ref5 = values[_i]; | ||
var expiration = _ref5.expiration; | ||
if (expiration < minExpiration) { | ||
@@ -66,19 +56,16 @@ minExpiration = expiration; | ||
function calcTimeout(expiration) { | ||
var requestDurationEstimate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; | ||
var timeout = new Date(expiration) - new Date(); | ||
export function calcTimeout(expiration, requestDurationEstimate = 0) { | ||
const timeout = new Date(expiration) - new Date(); | ||
return Math.max(timeout - requestDurationEstimate, 0); | ||
} | ||
function validateTimeoutValue(_ref6) { | ||
var timeoutValue = _ref6.timeoutValue, | ||
tokens = _ref6.tokens, | ||
options = _ref6.options; | ||
export function validateTimeoutValue({ | ||
timeoutValue, | ||
tokens, | ||
options | ||
}) { | ||
if (timeoutValue === 0) { | ||
throw new Error("Can't set 0 timeout for refreshing tokens. Check the token 'expiration' propererty.\nTokens: ".concat(JSON.stringify(tokens, null, 2))); | ||
throw new Error(`Can't set 0 timeout for refreshing tokens. Check the token 'expiration' propererty.\nTokens: ${JSON.stringify(tokens, null, 2)}`); | ||
} else if (timeoutValue < options.minRequiredExpiration) { | ||
throw new RangeError("Token 'timeoutValue' value is too low. Minimum required value is: ".concat(options.minRequiredExpiration + options.requestDurationEstimate, "ms.")); | ||
throw new RangeError(`Token 'timeoutValue' value is too low. Minimum required value is: ${options.minRequiredExpiration + options.requestDurationEstimate}ms.`); | ||
} | ||
} |
@@ -1,50 +0,20 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.isUserFetching = exports.isLoggingIn = exports.loginErrors = exports.isLoggedIn = exports.authUser = exports.isRefreshing = exports.authTokens = void 0; | ||
var _config = require("./config"); | ||
var authTokens = function authTokens(state) { | ||
return state[_config.reducerName].tokens; | ||
import { reducerName } from './config'; | ||
export const authTokens = state => { | ||
return state[reducerName].tokens; | ||
}; | ||
exports.authTokens = authTokens; | ||
var isRefreshing = function isRefreshing(state) { | ||
return state[_config.reducerName].isRefreshing; | ||
export const isRefreshing = state => { | ||
return state[reducerName].isRefreshing; | ||
}; | ||
exports.isRefreshing = isRefreshing; | ||
var authUser = function authUser(state) { | ||
return state[_config.reducerName].user; | ||
export const authUser = state => { | ||
return state[reducerName].user; | ||
}; | ||
exports.authUser = authUser; | ||
var isLoggedIn = function isLoggedIn(state) { | ||
return state[_config.reducerName].isLoggedIn; | ||
export const isLoggedIn = state => { | ||
return state[reducerName].isLoggedIn; | ||
}; | ||
exports.isLoggedIn = isLoggedIn; | ||
var loginErrors = function loginErrors(state) { | ||
return state[_config.reducerName].loginError; | ||
export const loginErrors = state => { | ||
return state[reducerName].loginError; | ||
}; | ||
exports.loginErrors = loginErrors; | ||
var isLoggingIn = function isLoggingIn(state) { | ||
return state[_config.reducerName].isLoggingIn; | ||
export const isLoggingIn = state => { | ||
return state[reducerName].isLoggingIn; | ||
}; | ||
exports.isLoggingIn = isLoggingIn; | ||
var isUserFetching = function isUserFetching(state) { | ||
return state[_config.reducerName].isUserFetching; | ||
}; | ||
exports.isUserFetching = isUserFetching; | ||
export const isUserFetching = state => state[reducerName].isUserFetching; |
{ | ||
"name": "ackee-redux-token-auth", | ||
"version": "2.0.1", | ||
"description": "Redux and redux saga logic for token authentication with an auto token refresh.", | ||
"version": "2.0.2", | ||
"description": "The library aims to handle authentication logic with token based flow.", | ||
"main": "lib/index.js", | ||
"module": "es/index.js", | ||
"scripts": { | ||
"build:lib": "cross-env BABEL_ENV=lib babel src --out-dir lib", | ||
"build:es": "cross-env BABEL_ENV=es babel src --out-dir es", | ||
"build:dist": "webpack --config webpack.config.js", | ||
"build": "npm run build:dist && npm run build:es && npm run build:lib", | ||
"build:lib": "cross-env BABEL_ENV=lib && rm -rf ./lib && babel src --out-dir lib", | ||
"build:es": "cross-env BABEL_ENV=es && rm -rf ./es && babel src --out-dir es", | ||
"build:dist": "rm -rf ./dist && webpack --config webpack.config.js", | ||
"build": "rm -rf ./dist ./es ./lib && npm run build:dist && npm run build:es && npm run build:lib", | ||
"prepublish": "npm run build", | ||
@@ -13,0 +13,0 @@ "start": "npm run build:es && onchange 'src/**/*.js' -- npm run build:es" |
@@ -1,2 +0,2 @@ | ||
import { put, fork, all, actionChannel } from 'redux-saga/effects'; | ||
import { put, call, all, actionChannel } from 'redux-saga/effects'; | ||
@@ -25,3 +25,8 @@ import * as types from '../../actionType'; | ||
{ | ||
pattern: [types.AUTH_REFRESH_TOKEN, types.AUTH_LOGOUT], | ||
pattern: [ | ||
types.AUTH_REFRESH_TOKEN, | ||
types.AUTH_LOGOUT, | ||
types.AUTH_LOGIN_FAILURE, | ||
types.FETCH_AUTH_USER_FAILURE, | ||
], | ||
*task() { | ||
@@ -33,3 +38,3 @@ yield put(accessTokenUnavailable()); | ||
yield fork(simpleCircuit, tokenAvailabilityUnits); | ||
yield call(simpleCircuit, tokenAvailabilityUnits); | ||
} | ||
@@ -46,3 +51,3 @@ | ||
{ | ||
pattern: types.AUTH_LOGOUT, | ||
pattern: [types.AUTH_LOGOUT, types.AUTH_LOGIN_FAILURE, types.FETCH_AUTH_USER_FAILURE], | ||
*task() { | ||
@@ -69,3 +74,3 @@ yield put(authSessionEnd()); | ||
yield fork(deepCircuit, [authSessionUnits[0], ...authSessionInterruptionUnits, authSessionUnits[1]]); | ||
yield call(deepCircuit, [authSessionUnits[0], ...authSessionInterruptionUnits, authSessionUnits[1]]); | ||
} | ||
@@ -72,0 +77,0 @@ |
@@ -9,4 +9,4 @@ ## `getAuthStateChannel()` | ||
- `ACCESS_TOKEN_UNAVAILABLE` - It's triggered any time access token becomes unavailable (e.g. on logout or on failed token refresh). | ||
- `AUTH_SESSION_START` - It's triggered once on successful login. | ||
- `AUTH_SESSION_END` - It's triggered once on logout. | ||
- `AUTH_SESSION_START` - It's triggered once when tokens are set. | ||
- `AUTH_SESSION_END` - It's triggered once on logout, when fetching auth. user fails or when login fails. | ||
- `AUTH_SESSION_PAUSE` - Triggered when tokens refreshing start. | ||
@@ -13,0 +13,0 @@ - `AUTH_SESSION_RESUME` - Triggered when tokens refreshing successfully finished. |
@@ -49,3 +49,3 @@ import { logger } from '../config'; | ||
if (isNotFn(shouldRefresh)) { | ||
config.detectShouldRefresh = error => !!error; | ||
config.detectShouldRefresh = () => true; | ||
} else { | ||
@@ -52,0 +52,0 @@ config.detectShouldRefresh = shouldRefresh; |
import { all } from 'redux-saga/effects'; | ||
import tokens, { tryToRetrieveTokens } from './tokens'; | ||
@@ -14,3 +13,3 @@ import handleLogin from './handleLogin'; | ||
export function* saga() { | ||
yield all([authState(), handleLogin(), processTokenRefresh(), fetchAuthUser(), tokens(), tryToRetrieveTokens()]); | ||
yield all([authState(), tokens(), handleLogin(), processTokenRefresh(), fetchAuthUser(), tryToRetrieveTokens()]); | ||
} |
import { take, put, cancel, fork, takeEvery, race, all } from 'redux-saga/effects'; | ||
import { refreshTokens } from '../../actions'; | ||
import { SET_AUTH_TOKENS, AUTH_LOGOUT } from '../../actionType'; | ||
import { SET_AUTH_TOKENS, ACCESS_TOKEN_UNAVAILABLE, AUTH_REFRESH_TOKEN_FAILURE } from '../../actionType'; | ||
import { logger } from '../../config'; | ||
@@ -84,7 +84,7 @@ | ||
task: setTokensHandler(action, refreshTokensTimeout), | ||
abort: take(AUTH_LOGOUT), | ||
abort: take([ACCESS_TOKEN_UNAVAILABLE, AUTH_REFRESH_TOKEN_FAILURE]), | ||
}); | ||
}), | ||
takeEvery(AUTH_LOGOUT, clearTokensHandler, refreshTokensTimeout), | ||
takeEvery([ACCESS_TOKEN_UNAVAILABLE, AUTH_REFRESH_TOKEN_FAILURE], clearTokensHandler, refreshTokensTimeout), | ||
]); | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
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
76164
60
1650
1