Coder Social home page Coder Social logo

Comments (4)

mjrussell avatar mjrussell commented on September 15, 2024

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.

themouette avatar themouette commented on September 15, 2024

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.

mjrussell avatar mjrussell commented on September 15, 2024

@themouette

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.

mjrussell avatar mjrussell commented on September 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.