redux-auth-wrapper
Advanced tools
Comparing version
@@ -35,4 +35,2 @@ 'use strict'; | ||
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } | ||
var defaults = { | ||
@@ -46,8 +44,3 @@ AuthenticatingComponent: function AuthenticatingComponent() { | ||
return !(0, _lodash2.default)(x); | ||
}, | ||
propMapper: function propMapper(_ref) { | ||
var props = _objectWithoutProperties(_ref, []); | ||
return _extends({}, props); | ||
} // eslint-disable-line no-unused-vars | ||
} | ||
}; | ||
@@ -60,10 +53,7 @@ | ||
wrapperDisplayName = _defaults$args.wrapperDisplayName, | ||
predicate = _defaults$args.predicate, | ||
propMapper = _defaults$args.propMapper; | ||
predicate = _defaults$args.predicate; | ||
var isAuthorized = function isAuthorized(authData) { | ||
return predicate(authData); | ||
}; | ||
// Wraps the component that needs the auth enforcement | ||
// Wraps the component that needs the auth enforcement | ||
function wrapComponent(DecoratedComponent) { | ||
@@ -86,4 +76,2 @@ var _class, _temp; | ||
value: function render() { | ||
// Allow everything but the replace aciton creator to be passed down | ||
// Includes route props from React-Router and authData | ||
var _props = this.props, | ||
@@ -93,8 +81,8 @@ authData = _props.authData, | ||
if (isAuthorized(authData)) { | ||
return _react2.default.createElement(DecoratedComponent, propMapper(this.props)); | ||
if (predicate(authData)) { | ||
return _react2.default.createElement(DecoratedComponent, this.props); | ||
} else if (isAuthenticating) { | ||
return _react2.default.createElement(AuthenticatingComponent, propMapper(this.props)); | ||
return _react2.default.createElement(AuthenticatingComponent, this.props); | ||
} else { | ||
return FailureComponent ? _react2.default.createElement(FailureComponent, propMapper(this.props)) : null; | ||
return FailureComponent ? _react2.default.createElement(FailureComponent, this.props) : null; | ||
} | ||
@@ -101,0 +89,0 @@ } |
@@ -34,3 +34,4 @@ 'use strict'; | ||
exports.default = function (_ref) { | ||
var createRedirectLoc = _ref.createRedirectLoc; | ||
var locationHelperBuilder = _ref.locationHelperBuilder, | ||
getRouterRedirect = _ref.getRouterRedirect; | ||
@@ -43,4 +44,9 @@ | ||
authenticatingSelector = allArgs.authenticatingSelector, | ||
allowRedirectBack = allArgs.allowRedirectBack; | ||
allowRedirectBack = allArgs.allowRedirectBack, | ||
redirectQueryParamName = allArgs.redirectQueryParamName; | ||
var _locationHelperBuilde = locationHelperBuilder({ | ||
redirectQueryParamName: redirectQueryParamName || 'redirect' | ||
}), | ||
createRedirectLoc = _locationHelperBuilde.createRedirectLoc; | ||
@@ -58,6 +64,17 @@ var redirectPathSelector = void 0; | ||
var redirect = function redirect(history) { | ||
return function () { | ||
return history.replace(createRedirectLoc(allowRedirectBack).apply(undefined, arguments)); | ||
var allowRedirectBackFn = void 0; | ||
if (typeof allowRedirectBack === 'boolean') { | ||
allowRedirectBackFn = function allowRedirectBackFn() { | ||
return allowRedirectBack; | ||
}; | ||
} else if (typeof allowRedirectBack === 'function') { | ||
allowRedirectBackFn = allowRedirectBack; | ||
} else { | ||
(0, _invariant2.default)(false, 'redirectPath must be either a boolean or a fiwnction'); | ||
} | ||
var redirect = function redirect(replace) { | ||
return function (props, path) { | ||
return replace(createRedirectLoc(allowRedirectBackFn(props, path))(props, path)); | ||
}; | ||
}; | ||
@@ -71,3 +88,3 @@ | ||
isAuthenticating: authenticatingSelector(state, ownProps), | ||
redirect: redirect(ownProps.history) | ||
redirect: redirect(getRouterRedirect(ownProps)) | ||
}; | ||
@@ -84,4 +101,9 @@ })((0, _authWrapper2.default)(allArgs)(DecoratedComponent)); | ||
allowRedirectBack = allArgs.allowRedirectBack, | ||
redirectAction = allArgs.redirectAction; | ||
redirectAction = allArgs.redirectAction, | ||
redirectQueryParamName = allArgs.redirectQueryParamName; | ||
var _locationHelperBuilde2 = locationHelperBuilder({ | ||
redirectQueryParamName: redirectQueryParamName || 'redirect' | ||
}), | ||
createRedirectLoc = _locationHelperBuilde2.createRedirectLoc; | ||
@@ -99,6 +121,17 @@ var redirectPathSelector = void 0; | ||
var allowRedirectBackFn = void 0; | ||
if (typeof allowRedirectBack === 'boolean') { | ||
allowRedirectBackFn = function allowRedirectBackFn() { | ||
return allowRedirectBack; | ||
}; | ||
} else if (typeof allowRedirectBack === 'function') { | ||
allowRedirectBackFn = allowRedirectBack; | ||
} else { | ||
(0, _invariant2.default)(false, 'redirectPath must be either a boolean or a fiwnction'); | ||
} | ||
var createRedirect = function createRedirect(dispatch) { | ||
return { | ||
redirect: function redirect() { | ||
return dispatch(redirectAction(createRedirectLoc(allowRedirectBack).apply(undefined, arguments))); | ||
redirect: function redirect(props, path) { | ||
return dispatch(redirectAction(createRedirectLoc(allowRedirectBackFn(props, path))(props, path))); | ||
} | ||
@@ -105,0 +138,0 @@ }; |
@@ -18,7 +18,8 @@ 'use strict'; | ||
var _locationHelperBuilde = (0, _locationHelper2.default)({}), | ||
createRedirectLoc = _locationHelperBuilde.createRedirectLoc; | ||
var _redirectUtil = (0, _redirect2.default)({ | ||
createRedirectLoc: createRedirectLoc | ||
locationHelperBuilder: _locationHelper2.default, | ||
getRouterRedirect: function getRouterRedirect(_ref) { | ||
var router = _ref.router; | ||
return router.replace; | ||
} | ||
}); | ||
@@ -25,0 +26,0 @@ |
@@ -18,7 +18,8 @@ 'use strict'; | ||
var _locationHelperBuilde = (0, _locationHelper2.default)({}), | ||
createRedirectLoc = _locationHelperBuilde.createRedirectLoc; | ||
var _redirectUtil = (0, _redirect2.default)({ | ||
createRedirectLoc: createRedirectLoc | ||
locationHelperBuilder: _locationHelper2.default, | ||
getRouterRedirect: function getRouterRedirect(_ref) { | ||
var history = _ref.history; | ||
return history.replace; | ||
} | ||
}); | ||
@@ -25,0 +26,0 @@ |
{ | ||
"name": "redux-auth-wrapper", | ||
"version": "2.0.0-beta", | ||
"version": "2.0.0-beta2", | ||
"description": "A utility library for handling authentication and authorization for redux and react-router", | ||
@@ -60,4 +60,5 @@ "files": [ | ||
"lodash.isempty": "4.4.0", | ||
"prop-types": "15.5.8" | ||
"prop-types": "15.5.8", | ||
"query-string": "4.3.4" | ||
} | ||
} |
@@ -10,8 +10,7 @@ import React, { Component } from 'react' | ||
wrapperDisplayName: 'AuthWrapper', | ||
predicate: x => !isEmpty(x), | ||
propMapper: ({ ...props }) => ({ ...props }) // eslint-disable-line no-unused-vars | ||
predicate: x => !isEmpty(x) | ||
} | ||
export default (args) => { | ||
const { AuthenticatingComponent, FailureComponent, wrapperDisplayName, predicate, propMapper } = { | ||
const { AuthenticatingComponent, FailureComponent, wrapperDisplayName, predicate } = { | ||
...defaults, | ||
@@ -21,4 +20,2 @@ ...args | ||
const isAuthorized = (authData) => predicate(authData) | ||
// Wraps the component that needs the auth enforcement | ||
@@ -28,3 +25,2 @@ function wrapComponent(DecoratedComponent) { | ||
class UserAuthWrapper extends Component { | ||
@@ -44,11 +40,9 @@ | ||
render() { | ||
// Allow everything but the replace aciton creator to be passed down | ||
// Includes route props from React-Router and authData | ||
const { authData, isAuthenticating } = this.props | ||
if (isAuthorized(authData)) { | ||
return <DecoratedComponent {...propMapper(this.props)} /> | ||
if (predicate(authData)) { | ||
return <DecoratedComponent {...this.props} /> | ||
} else if(isAuthenticating) { | ||
return <AuthenticatingComponent {...propMapper(this.props)} /> | ||
return <AuthenticatingComponent {...this.props} /> | ||
} else { | ||
return FailureComponent ? <FailureComponent {...propMapper(this.props)} /> : null | ||
return FailureComponent ? <FailureComponent {...this.props} /> : null | ||
} | ||
@@ -55,0 +49,0 @@ } |
@@ -13,8 +13,12 @@ import { connect } from 'react-redux' | ||
export default ({ createRedirectLoc }) => { | ||
export default ({ locationHelperBuilder, getRouterRedirect }) => { | ||
const connectedRouterRedirect = (args) => { | ||
const allArgs = { ...connectedDefaults, ...args } | ||
const { redirectPath, authSelector, authenticatingSelector, allowRedirectBack } = allArgs | ||
const { redirectPath, authSelector, authenticatingSelector, allowRedirectBack, redirectQueryParamName } = allArgs | ||
const { createRedirectLoc } = locationHelperBuilder({ | ||
redirectQueryParamName: redirectQueryParamName || 'redirect' | ||
}) | ||
let redirectPathSelector | ||
@@ -29,4 +33,14 @@ if (typeof redirectPath === 'string') { | ||
const redirect = (history) => (...args) => history.replace(createRedirectLoc(allowRedirectBack)(...args)) | ||
let allowRedirectBackFn | ||
if (typeof allowRedirectBack === 'boolean') { | ||
allowRedirectBackFn = () => allowRedirectBack | ||
} else if (typeof allowRedirectBack === 'function') { | ||
allowRedirectBackFn = allowRedirectBack | ||
} else { | ||
invariant(false, 'redirectPath must be either a boolean or a fiwnction') | ||
} | ||
const redirect = (replace) => (props, path) => | ||
replace(createRedirectLoc(allowRedirectBackFn(props, path))(props, path)) | ||
return (DecoratedComponent) => | ||
@@ -37,3 +51,3 @@ connect((state, ownProps) => ({ | ||
isAuthenticating: authenticatingSelector(state, ownProps), | ||
redirect: redirect(ownProps.history) | ||
redirect: redirect(getRouterRedirect(ownProps)) | ||
}))(authWrapper(allArgs)(DecoratedComponent)) | ||
@@ -44,4 +58,8 @@ } | ||
const allArgs = { ...connectedDefaults, ...args } | ||
const { redirectPath, authSelector, authenticatingSelector, allowRedirectBack, redirectAction } = allArgs | ||
const { redirectPath, authSelector, authenticatingSelector, allowRedirectBack, redirectAction, redirectQueryParamName } = allArgs | ||
const { createRedirectLoc } = locationHelperBuilder({ | ||
redirectQueryParamName: redirectQueryParamName || 'redirect' | ||
}) | ||
let redirectPathSelector | ||
@@ -56,4 +74,13 @@ if (typeof redirectPath === 'string') { | ||
let allowRedirectBackFn | ||
if (typeof allowRedirectBack === 'boolean') { | ||
allowRedirectBackFn = () => allowRedirectBack | ||
} else if (typeof allowRedirectBack === 'function') { | ||
allowRedirectBackFn = allowRedirectBack | ||
} else { | ||
invariant(false, 'redirectPath must be either a boolean or a fiwnction') | ||
} | ||
const createRedirect = (dispatch) => ({ | ||
redirect: (...args) => dispatch(redirectAction(createRedirectLoc(allowRedirectBack)(...args))) | ||
redirect: (props, path) => dispatch(redirectAction(createRedirectLoc(allowRedirectBackFn(props, path))(props, path))) | ||
}) | ||
@@ -60,0 +87,0 @@ |
import locationHelperBuilder from '../history3/locationHelper' | ||
import redirectUtil from '../helper/redirect' | ||
const { createRedirectLoc } = locationHelperBuilder({}) | ||
export const { connectedRouterRedirect, connectedReduxRedirect } = redirectUtil({ | ||
createRedirectLoc | ||
locationHelperBuilder, | ||
getRouterRedirect: ({ router }) => router.replace | ||
}) |
import locationHelperBuilder from '../history4/locationHelper' | ||
import redirectUtil from '../helper/redirect' | ||
const { createRedirectLoc } = locationHelperBuilder({}) | ||
export const { connectedRouterRedirect, connectedReduxRedirect } = redirectUtil({ | ||
createRedirectLoc | ||
locationHelperBuilder, | ||
getRouterRedirect: ({ history }) => history.replace | ||
}) |
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
56955
2.68%639
7.21%0
-100%5
25%+ Added
+ Added
+ Added