How to update client component after server action was triggered?
SolvedClosed
I have a Next.js app and I'm using supabase for authentification.
In my navbar (which is a client component) I have a login button which should change to a welcome message after a successful login. I need this to be a client component so that it can still work with SSG.
The login happens as a server action which means once it triggers, the user is logged in but the client component doesn't know about it.
Is there any clean way to trigger the onAuthStateChange to pick up the login state change?
The problem is if a server action triggers the session change, the client component won't know about it as they are completely separated. I'm wondering how can I let the client component know that the user or session has been altered.
Initially I tried to pass it as a prop to the client component but that breaks SSG
Vercel Staff
Hi , oh I see. I thought the onAuthStateChange event is triggered anyways.
If you need more help, please share your public repo or a minimal reproducible example. That will let us all work together from the same code to figure out what’s going wrong.
, certainly!
Here's what I tried:
User logs in -> Server action is triggered -> Returns success message -> Trigger a refresh (here it breaks)
The data seems fine in all server components, they instantly get updated but the header client component doesn't
Hi , thanks for sharing the code. Have you tried using the redirect function in the login server action to redirect the user to /private directly on success?
What happens is when the user logs in -> the redirect kicks in and loads the /private route where I can see the correct user data inside the server component, however this does not seem to refresh the Header component which is still unaware of the user being logged in.
If i hard refresh the page by pressing F5, or use alt-tab on the browser window, then it's working.
I was able to fix it by creating a context around the app for the user.
And then changing the login server action to return the user: