Skip to content

Instantly share code, notes, and snippets.

@wintorez

wintorez/auth.js

Created Apr 20, 2018
Embed
What would you like to do?
redux auth HOCs using redux-auth-wrapper
import { connectedRouterRedirect } from 'redux-auth-wrapper/history4/redirect';
import { routerActions } from 'react-router-redux';
import connectedAuthWrapper from 'redux-auth-wrapper/connectedAuthWrapper';
import locationHelperBuilder from 'redux-auth-wrapper/history4/locationHelper';
const locationHelper = locationHelperBuilder({});
export const requireAuth = connectedRouterRedirect({
redirectPath: '/login',
allowRedirectBack: false,
authenticatedSelector: state => state.shift.auth.authenticated,
wrapperDisplayName: 'UserIsAuthenticated',
redirectAction: routerActions.replace
});
export const noRequireAuth = connectedRouterRedirect({
redirectPath: (state, ownProps) =>
locationHelper.getRedirectQueryParam(ownProps) || '/home',
allowRedirectBack: false,
authenticatedSelector: state => !state.shift.auth.authenticated,
wrapperDisplayName: 'UserIsNotAuthenticated',
redirectAction: routerActions.replace
});
export const hideWithoutAuth = connectedAuthWrapper({
authenticatedSelector: state => state.shift.auth.authenticated,
wrapperDisplayName: 'VisibleOnlyLoggedIn'
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment