Customise session objects via callbacks in next-auth

Viewed 316

I'm using "next": "^11.0.1", "next-auth": "^3.27.3". I need to customise my session object provided by the hooks to access user information at client-side. This is my callback code:

callbacks: {    
    async jwt (token, user) {
      console.log(user)
      if (user){
        token = { ...token, username: user?.user, password: user?.password }
        return token
      }
      else {
        return token
      }
    },
    async session( session, token) {
      // Send properties to the client, like an access_token from a provider.
      console.log("session: ", session)
      console.log("passed token:", token)
      return session
    },
},

This is my app

const SeatingPage: NextPage = () => {
    const [session, loading] = useSession()

    useEffect(() => {
      if (loading) return
      if (!session) signIn()
    }, [session, loading])

    if (!session) return <p>loading</p>
    return <p>Hello world</p>
}

If I understand it right, the token parameter in jwt() is what I return in my authorize(). The app runs the jwt() twice for some reason. However, only in the first call token has the right value being {user: "xyz", password: "xyz"} the second time token is an empty object. And session() never gets the correct parameter { ...token, username: user?.user, password: user?.password } but always the base token.

0 Answers
Related