How to distinguish users by where they were redirected before they logged in
Hi I have a question regarding authentication. I want to have two types of users depending on where they log from and redirect them to different subpages depending on what kind of user they are.
I read that the common solution is to use localStorage but that solution doesn't work for my app flow.
14 Replies
Hi @indiehacker. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Based on the information provided in the knowledge sources, Wasp currently doesn't have a built-in feature to distinguish users and redirect them to different subpages depending on where they logged in from. However, there are a few relevant points and potential workarounds to consider:
1. The
onAuthSucceededRedirectTo
property in Wasp only supports static URLs for redirection after successful login Wasp Auth Overview (https://wasp-lang.dev/docs/auth/overview#onauthsucceededredirectto-string).
2. There's an open GitHub issue discussing the need for more flexible redirection options after login GitHub Issue #1360 (https://github.com/wasp-lang/wasp/issues/1360).
3. The localStorage solution you mentioned is indeed a common workaround, but as you've noted, it doesn't work for your specific app flow.
4. An alternative approach suggested by a community member involves not using authRequired: true
in the Wasp file, but instead using the useAuth
hook. This would allow you to:
1. Check if the user is logged in when the page loads
2. If not, save some parameters and then redirect using the <Redirect />
component from react-router-dom
Discord Support Thread (https://discord.com/channels/686873244791210014/1207292303476002846)
5. There's an open GitHub issue to improve the OAuth flow and enable propagating query parameters through the chain of redirects, which could potentially help with your use case in the future GitHub Issue #1772 (https://github.com/wasp-lang/wasp/issues/1772).
Given these limitations, you might need to implement a custom solution. One possible approach could be:
1. Create a custom field in your User entity to store the user type.
2. Implement custom logic in your login/signup process to set this field based on where the user is coming from.
3. After successful authentication, check this field and redirect the user accordingly using client-side routing.
Keep in mind that this is a workaround and not a built-in feature of Wasp. The Wasp team is aware of the need for more flexible authentication and redirection options, and improvements may be coming in future updates.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @indiehacker, 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?None of this solutions work for me. Imagine you have a shop, with admin user and buyers. Each of them need their respective logins and being redirected to different sites. This seems not to be solved yet.
Usually common practice would be to add to query from where user came from, like
source=whatsapp
and so on. then on your server you can use that to return different redirect url's for exampleThanks for the quick reply! Can the source be a subpage? How can I pass this in the auth process as a query param so that it is saved after authenticating?
"source" here is just identifier for developer
basically it's a flag for you to understand where this request came from, if you can understand your own source, you can control where to redirect user
In wasp I'm not sure if it is possible to rewrite auth entirely, but you could add additional field, - - add hidden field with _source to form
- store source for user
- check source on after login
or use before and after auth hooks to do this with some sort of storage for this user's request, there are some hints to store something for request and then access it in onAfterSignup/onAfterLogin on this page
https://wasp-lang.dev/docs/auth/auth-hooks#executing-code-before-the-oauth-redirect
but I'm not sure what would be the best way
Maybe of course there is something more straightforward but that's up to wasp team 😅
Auth Hooks | Wasp
Auth hooks allow you to "hook into" the auth process at various stages and run your custom code. For example, if you want to forbid certain emails from signing up, or if you wish to send a welcome email to the user after they sign up, auth hooks are the way to go.
Ok, I'll check out the link you provided. Thanks a lot for the help 🙂
Wohooo @indiehacker, you just became a Waspeteer level 1!
And if you create a property of source to the user and after the login (to the page that redirects you) check if you have that property with some value. As it will be
null
, you take from the localStorage
the value and assign it later?
In my mind it works
None of this approaches work unfortunately
It would be nice if there were a built in solution for this
I want to have two types of users depending on where they log from and redirect them to different subpages depending on what kind of user they are.Could you give us more details on how implemented this? Do you have a
User
entity, what kind of fields does it have? Do you have two different login pages?
The redirect logic should probably be based on some property in the DB rather than using a subpage to redirect - it's more secure, since people can fake everything on the client, but not on the server.Is it possible two have to different login pages with the built in auth system from wasp without having to build it custom? That would solve my problem
At that point it sounds much better to have two seperate apps
Can point both to the one postgres database (at least I can't imagine why not)
I would rather have everything in one app if it's possible
Nope, you'd need to build custom login page(s) in this case 😢
Could you explain what would you do differently in those two login pages, so maybe we can improve something in Wasp in terms of flexibility?