Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to go back to previous route in next.js

//if user not logged in
if (!auth.user) {
  window.localStorage.setItem("path", router.asPath); //save current route
  router.replace("/login"); //send user to login page
  return <div> redirecting to login... </div>
}

//when user login
if (auth.user) {
  const path = localStorage.getItem("path")
  router.replace(path || "/"); //navigate to last route
  return <div> Loading... </div>
  );
}
Comment

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);
        }
  }
Comment

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);
        }
  }
Comment

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);
        }
  }
Comment

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);
        }
  }
Comment

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);
        }
  }
Comment

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);
        }
  }
Comment

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);
        }
  }
Comment

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);
        }
  }
Comment

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);
        }
  }
Comment

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);
        }
  }
Comment

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);
        }
  }
Comment

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);
        }
  }
Comment

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);
        }
  }
Comment

PREVIOUS NEXT
Code Example
Javascript :: node fs get size 
Javascript :: onclick a hyperlink and display the id of clicked hyperlink js 
Javascript :: function multiply(a b) a * b javascript 
Javascript :: buiding react project 
Javascript :: javascript loop through delimited string 
Javascript :: jquery keypress div color change 
Javascript :: nodejs mysql escaping query 
Javascript :: Change tilte alert 
Javascript :: adonis model inheritance 
Javascript :: plumsail on change event value 
Javascript :: jsdoc run for all files in folder 
Javascript :: convert string to file angular 
Javascript :: js point in rect 
Javascript :: javascript What is the Comment (native) function 
Javascript :: document.querySelectorAll(".preview") + forEach 
Javascript :: Why is this forEach code snippet invalid in AngularJS 
Javascript :: Getting PointerEvent instead of jQuery.Event on ng-click in AngularJS 
Javascript :: Angular.js : recursive call to an $mdDialog controller 
Javascript :: EXPO useEffect not called on navigating to same screen 
Javascript :: react js graph with more than one y axis 
Javascript :: fill array with random numbers javascript using arrow function 
Javascript :: socket.io authentication 
Javascript :: react native communications 
Javascript :: javascript get multiple attributes 
Javascript :: get value from createReadStream 
Javascript :: Gamification Details Component is not declared in any Angular module 
Javascript :: wait for element to be loaded 
Javascript :: auto load in element show 
Javascript :: success res node.js 
Javascript :: combining not selector with other jquery 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =