Next.js Discord

Discord Forum

How to refresh session so it displays new name after editing name?

Unanswered
Chum salmon posted this in #help-forum
Open in Discord
Chum salmonOP
I made a form to edit user profile. In that form you can upload in profile image and change name.
The form works perfectly fine as I checked my database, new name and profile image are updated.
However, when I go to the dashboard, the old name that I display using
const { data: session } = useSession();
and <h2 className="card-title">{session?.user?.name}</h2>
is still displaying.
How do I make sure that it always display the current data?
I want to know how to refresh a session so that I can implement it in the form where user edit their profiles.

The only way to make it work right now is to logout, and log back in. Then I see the new name.

6 Replies

Sun bear
You can try to run update after your form is submitted and the user data in database updated:

  const { data: session, status, update } = useSession()
@Sun bear You can try to run update after your form is submitted and the user data in database updated: const { data: session, status, update } = useSession()
Chum salmonOP
Hi I actually have that in my handleSubmit function and it's not working
@Chum salmon Hi I actually have that in my handleSubmit function and it's not working
Sun bear
Do you have a callback defined for session that updates the session in nextauth? So basically fetching the database within session callback?

And you could try to do a refresh() from next/navigation useRouter() as well after form submit
Sun bear
I think authorize is only running on login. Bit your user is already logged in.

You can check this:
https://github.com/mickasmt/next-auth-roles-template/blob/main/auth.ts

Depending on your version instead of jwt callback it could be token
Chum salmonOP
I'll check it out in a bit. thank you!