Anyone here used supabase with react for implement...
# help
a
Anyone here used supabase with react for implementing private and public route? I am using exact same code from here https://ruanmartinelli.com/posts/supabase-authentication-react but problem is when user sign up happens then user should be navigated to login page but instead user session is generated and it gets automatically logged in. I don't want user to be able to view private routes if sign up is success only show private route if login is success. How can I do that?
s
This feels like a hacky workaround, but if signup is successful, you could call
signOut()
immediately afterwards. I'm not aware of any options you can pass to the signup function which prevents auto-signin, but I'd be interested in hearing how other people handle it. Alternatively, requiring email address confirmation prevents the a session from being created when
signUp
completes
t
Another thing I think you can do is change the src/contexts/auth.js to also listen for SIGN_UP and PASSWORD_RECOVERY (something else that will auto login the user) I'm not very versed in react but the function onAuthStateChange() has events for everything, SIGN_IN, SIGN_UP, PASSWORD_RECOVERY, etc. So you just need to do whatever you need to do on those events.
currently it only sets the User, but you can change that to check for the specific events and do different things.
a
> I'm not aware of any options you can pass to the signup function which prevents auto-signin oh ok I was not aware of this. Sorry I am new to supabase.
it is disabled already
I see that supabase.auth.token is stored as soon as I sign up is there some how I can store it only when login not with sign up?
s
As the description says, if it's enabled, users need to confirm their email address before they'll be able to sign in. Ergo, if it's disabled, users don't need to confirm their email = they're also automatically signed in when they register
a
is it bad user experience if user is auto logged in even if they sign up? and not login ?
s
So, from the notes regarding
signUp
(https://supabase.com/docs/reference/javascript/auth-signup#notes): If "Email Confirmations" is turned on, a user is returned but session will be null If "Email Confirmations" is turned off, both a user and a session will be returned In terms of UX, if I sign up for a website or app and they don't want me to verify my email, I'd expect to be logged in as my intention is usually to access the service immediately. Even if I'm not intending to use the service immediately, there's no harm in me being logged in but closing the tab and then returning later when I'm ready to access the service.
I do think it'd be a good idea to allow configuration of the behaviour though - some use cases might not want users logging in immediately after registering. I'm not sure how much work it'd be, but if it's something you really want, it's probably worth raising a suggestion on Github
a
Ok so you mean to say should I raise a feature request on supabase discussion?
s
If it's a feature you want, yes
a
For now I will go with sign up or login both will work as login only as I am building micro saas product and don;t want to spend much time on MVP. I think I am overthinking. Thanks a lot for help and support 🙂 🙏