[solved] dbAuth & login redirect

I am trying to preserve the originally requested route after authentication

While searching for ‘prior art’ I see that this may be in .38 ?

I didn’t quite understand how to use it, and don’t know if it could be used with the ‘dbAuth’ setup

I’d welcome any tips!

Al;

What you’re after should be set up by default. If you go to a private page, and have your login page setup as the unauthenticated page, it should add a redirectTo query parameter with the route of that private page. Once you login, RW will take care of sending you to that page.

I’m using the scaffolded login page, which routes the user to home on authentication

So I adapted it to use the search by pulling it with useLocation

scaffolded:

  const { isAuthenticated, logIn } = useAuth()
  useEffect(() => {
    if (isAuthenticated) {
        navigate(routes.home())
    }
  }, [isAuthenticated])

adapted:

  const { search } = useLocation()
  const { isAuthenticated, logIn } = useAuth()
  useEffect(() => {
    if (isAuthenticated) {
      if (/redirectTo/.test(search)) {
        const newPath = search.split('=').slice(-1).join()
        navigate(newPath)
      } else {
        navigate(routes.home())
      }
    }
  }, [isAuthenticated])
1 Like