Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

How to go back to previous route after authentication in nextjs

 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);
        }
  }
Source by github.com #
 
PREVIOUS NEXT
Tagged: #How #previous #route #authentication #nextjs
ADD COMMENT
Topic
Name
2+1 =