next auth session callback 1

next auth session callback

callbacks: {
    jwt: async (token, user, account, profile, isNewUser) => {
        //  "user" parameter is the object received from "authorize"
        //  "token" is being send below to "session" callback...
        //  ...so we set "user" param of "token" to object from "authorize"...
        //  ...and return it...
        user && (token.user = user);
        return Promise.resolve(token)   // ...here
    },
    session: async (session, user, sessionToken) => {
        //  "session" is current session object
        //  below we set "user" param of "session" to value received from "jwt" callback
        session.user = user.user;
        return Promise.resolve(session)
    }
}

Here is what the above code is Doing:
1. We are using “authorize” callback to get user object from database.
2. We are using “jwt” callback to set “user” param of “token” to object received from “authorize” callback.
3. We are using “session” callback to set “user” param of “session” to value received from “jwt” callback.

Now, when we call “auth.getSession()” we will get “user” param in session object.

Similar Posts