getSession() call doesn't seem to update useSession() #2380
Replies: 2 comments 1 reply
-
I believe I'm come to a bit of a workaround. Can anyone let me know if this is the best way to approach this problem? import { getSession } from 'next-auth/client'
import { useEffect, useState } from 'react'
import axios from "axios"
function newUser() {
let [realSession, setRealSession] = useState()
useEffect(() => {
async function fetch() {
let session = await getSession()
setRealSession(session)
}
fetch()
}, [])
const handleUpdate = async () => {
axios.get('http://localhost:3000/api/update-username').then(async () => {
let newSession = await getSession()
setRealSession(newSession)
})
}
return (
<div>
<p>Hi there, you are new around here</p>
<button onClick={() => handleUpdate()}>change username</button>
<h1>{realSession && realSession.user. custom_username}</h1>
</div>
)
}
export default newUser; |
Beta Was this translation helpful? Give feedback.
-
Thank you for opening this discussion! 🙏 I actually don't understand why people are tripping on this. 😕 When Lines 161 to 167 in 2155c93 Lines 82 to 159 in 2155c93 Granted, the current solution is not really elegant and ignores some basic React rules, I am not surprised if we have a weird bug that makes this not work... Hopefully, the new version of the client code that is more React oriented will be more stable in this manner. Have a glimpse at it here: https://github.com/nextauthjs/next-auth/blob/next/src/client/react.js I still hope an |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to update the session when a user changes username with a button click.
From research done, I've learned that: I must use Database with JWT turned off as you mentioned:
What I am trying to achieve now is mentioned in #2269:
Currently, I have an API call. When the button is clicked, I update a custom column in the users table generated by next-auth. I have updated the model so all of this works.
I understand that I can force the page to refresh with Router but I'm ideally looking for a way to update Session in this component when this Axios call is completed.
So far:
<h1>
changes it's value appropriately.Please let me know what I'm missing or doing wrong.
Thanks for your work and time.
Beta Was this translation helpful? Give feedback.
All reactions