Skip to content

how to redirect user back to the router came from after authentication #13249

Answered by BernardA
ahakem asked this question in Help
Discussion options

You must be logged in to vote

@ahakem Haven't tested yet, but this should work:

On the private route, do as indicated in this issue:

   componentDidMount() { // and/or componentDidUpdate, depending on what you're trying to do
      if (!auth) {
         Router.push({
               pathname: '/login',
               query: { from: {router.pathname} },
         }))
     }
   }

Then on the login page, after successful login:

    handlePostLoginOk = () => {
          const { router } = this.props;
          if (router.query && router.query.from) {
               router.push(router.query.from);
        }
  }

Replies: 4 comments 14 replies

Comment options

You must be logged in to vote
6 replies
@ahakem
Comment options

@EngrZaks
Comment options

@tilda-deimos
Comment options

@EngrZaks
Comment options

@genevieveikechukwu
Comment options

Comment options

You must be logged in to vote
2 replies
@BernardA
Comment options

@ahakem
Comment options

Comment options

You must be logged in to vote
4 replies
@ahakem
Comment options

@BernardA
Comment options

@ahakem
Comment options

@marvellousadesanya
Comment options

Answer selected by ahakem
Comment options

You must be logged in to vote
2 replies
@ahakem
Comment options

@U-4-E-A
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
9 participants