I was trying to implement deep links(not sure if this is the correct term) in my react project what I want to do is this :
the admin gets a mail: "this user can't login: http://localhost:3000/admin/customer-profile/b1d4a11f-4f6c-4dc1-98a9-ac0c30486c16"
the admin is not logged in
the admin clicks the link and is forwarded to the login page
the admin enters his login details
(the new part)
so in the fifth point the admin is redirected to the page link was for instead of going to the homepage.
Below are some snippets to give more info regarding this:
<div className="App">
{ready && <Router basename="/admin">
<Switch>
<Route path="/login">
<Login />
</Route>
<Route path="/privacypolicy">
<PrivacyPolicy />
</Route>
<Route path="/loginhelp">
<Help />
</Route>
<Route path="/termsconditions">
<TermsConditions />
</Route>
<PrivateRoute path="/logout">
<Logout />
</PrivateRoute>
<Route path="/">
<SideBar />
<Switch>
<PrivateRoute path="/tier-list">
<TierList />
</PrivateRoute>
<PrivateRoute path="/privacy-policy">
<PrivacyPolicy />
</PrivateRoute>
<PrivateRoute path="/help">
<Help />
</PrivateRoute>
</Switch>
</Route>
</Switch>
</Router>}
</div>
Below is how PrivateRoute looks like :
function PrivateRoute({ children, ...rest }: any) {
return (
<Route
{...rest}
render={({ location }) =>
userInfo != null ? (
children
) : (
<Redirect
to={{
pathname: "/login",
state: { from: location }
}}
/>
)
}
/>
)
}
Any ideas How I shall modify this part to achieve the functionality?
Okay so I solved the problem. What I did was that I passed a pathName prop to the Login component and then if the user was logged in I redirect him to that page but if not then rendered the login page.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.