Comments (4)
Thanks for bringing this up, I dont use server-side rendering, but I think it would probably be worth my time to write a mini example for using this library with SSR.
I might have missed something, but server side, a onEnter callback is required on component to handle redirection correctly.
The universal-react-redux-boilerplate tends to confirm.
That example uses only the onEnter
method for auth so it can fall prey to some of the issues discussed in the motivation section (mainly redux-store updates not resulting in redirects).
I'll try to work on something in the next few days (if you have a very basic example feel free to PR it into the examples to give me a head start).
You are probably right in that there might need to be a combination of onEnter + HOC for SSR. By first using a new static method onEnter for the initial check, and then an HOC to to enforce checks on store updates or nested route changes.
Would be nice to use onEnter solely on the server render but Im not sure if there is a way to check for this. Thanks for the issue! Im going to do some more digging into this and report back
from redux-auth-wrapper.
If it can help, for now, I monkey path the HOC and it works (I did not check for redirect):
import { UserAuthWrapper } from 'redux-auth-wrapper';
/**
* HOC to protect a route.
*/
export const RequireAuth = UserAuthWrapper({
authSelector: state => state.authentication.user,
redirectAction: routeActions.replace,
failureRedirectPath: '/public',
wrapperDisplayName: 'RequireAuth',
allowRedirectBack: true
});
RequireAuth.onEnter = (store, nextState, replace) => {
const user = store.getState().authentication.user;
if (!user) {
replace({
pathname: '/public',
state: { redirect: nextState.location.pathname },
});
}
};
And on the route:
export default ({ store }) => {
const connect = (fn) => (nextState, replaceState) => fn(store, nextState, replaceState);
return (
<Route>
<Route path="/public" component={Publi)} />
<Route path="*" component={RequireAuth(App)}
onEnter={connect(RequireAuth.onEnter)} />
</Route>
);
};
from redux-auth-wrapper.
Thanks! Yeah definitely along the lines with what I was thinking. I wonder if the onEnter should have a check to only run if not on the client, something like:
function isClient() {
return typeof window != 'undefined' && window.document;
}
RequireAuth.onEnter = (store, nextState, replace) => {
if (!isClient()) {
const user = store.getState().authentication.user;
if (!user) {
replace({
pathname: '/public',
state: { redirect: nextState.location.pathname },
});
}
}
};
Maybe this is something that should just be in the example or the docs though? Probably better to let each user decide where they want to run it.
The problem with not including an isClient
check is that you essentially are running the check twice on the client if the route is triggered, once in onEnter and another if the onEnter passes and the HOC is returned.
from redux-auth-wrapper.
Release v0.3.0 with onEnter property and info in the README https://github.com/mjrussell/redux-auth-wrapper#server-side-rendering
from redux-auth-wrapper.
Related Issues (20)
- [Possible Bug] Async Validation in a multi-step form HOT 1
- How to make chunk js with react-router HOT 1
- Error in UserAuthWrapper
- Hande logout when expired token in axios interceptors but not redirect to login page HOT 2
- Misleading naming of authenticatedSelector HOT 2
- Example with react 16.x HOT 1
- Reducer not yet defined HOT 2
- Usage with react-cookies
- not running when am trying do npm start HOT 1
- connectedReduxRedirect is missing HOT 1
- Deprecated lifecycle method in redirect.js HOT 7
- Doesn't work with latest react HOT 3
- Type {} is missing the following properties from type Readonly<InjectedAuthProps> HOT 1
- ES6 Module - Error when using with Electron HOT 2
- redirect.js replace UNSAFE_componentWillMount with componentDidMount HOT 6
- No question mark befor query param on redirect HOT 2
- Versioning Order HOT 4
- React-router v6 HOT 9
- Infinite loop (redirect ) with nesting wrappers HOT 1
- Call for mantainers! HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from redux-auth-wrapper.