W
Wasp•5mo ago
lgu2610

Save accessed path before 401 /login redirect

Unidentified user goes to /:someid /:someid it's with auth: true User gets redirected to login Goal: after success login, redirect user to that path that accessed initially
4 Replies
kapa.ai
kapa.ai•5mo ago
Hi @lgu2610. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Currently, Wasp does not natively support redirecting users back to their originally requested path after a 401 redirect to the login page. The built-in auth configuration only allows you to specify a static redirect URL after successful login using the onAuthSucceededRedirectTo field, which defaults to "/" and does not handle dynamic paths or query parameters automatically. This is documented in the official docs and confirmed in community discussions and GitHub issues. Users have requested this feature, and it is recognized as a desirable improvement, but it is not yet implemented in Wasp itself Auth Overview: onAuthSucceededRedirectTo, Discord: onAuthSucceededRedirectTo previous site. Workaround:
A common workaround is to manually store the originally requested path (including query parameters) in localStorage before the redirect to /login, and then, after successful login, read this value and redirect the user accordingly. However, this requires you to avoid using authRequired: true in your Wasp page definitions and instead handle authentication and redirection logic manually in your React components using the useAuth hook and react-router-dom's Redirect component. This approach is described in community discussions, but it is not as seamless as a built-in solution and may require significant changes to your page logic Discord: Preserving Query Parameters in Google Social Auth Redirects. Summary:
- Wasp does not currently support automatic redirect to the originally requested path after login. - Only static redirect URLs are supported via onAuthSucceededRedirectTo. - Workarounds exist using localStorage and manual redirects, but require custom logic. - This feature is requested and may be supported in the future, but there is no ETA. If you need to implement this now, you will have to manage the redirect logic yourself as described above. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @lgu2610, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
lgu2610
lgu2610OP•5mo ago
@kapa.ai any way to catch that param before the 401?
kapa.ai
kapa.ai•5mo ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Based on the knowledge sources, there is currently no built-in way in Wasp to catch and store the originally requested path (including query parameters) before the 401 redirect occurs when using authRequired: true on a page. The redirect to /login happens automatically, and the original path is lost before you have a chance to intercept it in client-side code. Community members have discussed this limitation. One suggested workaround is to avoid using authRequired: true and instead use the useAuth hook in your page component. This way, you can check if the user is authenticated, and if not, you can manually save the current path (and any query parameters) to localStorage before redirecting to the login page. This approach gives you control to "catch" the param before the redirect, but it requires you to handle authentication and redirection logic yourself in your React components, rather than relying on Wasp's built-in mechanism. An example and further discussion can be found in the Discord thread on preserving query parameters in social auth redirects:
One thing you could do is: not use authRequired: true in the Wasp file, but instead use the useAuth hook which would give you the ability to: 1. Check if the user is logged in when the page loads, if yes, then let them through 2. If not, first save some params and then redirect using the <Redirect /> from react-router-dom lib
We have an example for using useAuth + Redirect here: Wasp Social Auth Docs
Discord: Preserving Query Parameters in Google Social Auth Redirects
In summary:
- With authRequired: true, you cannot catch the param before the 401/redirect. - If you need to catch and store the param, you must implement custom logic using useAuth and handle the redirect yourself.
- This is a known limitation and a requested feature, but not yet natively supported. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @lgu2610, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
miho
miho•5mo ago
You can use the useAuth hook instead of auth: true and manually do the redirect after you saved the location in localStorage 🙂

Did you find this page helpful?