Next.js Discord

Discord Forum

Different response for client side & server side getSession() requests

Unanswered
Mexican Jay posted this in #help-forum
Open in Discord
Mexican JayOP
Yo. Im just setted up a NextAuth with a DiscordProvider & custom Adapter. To responce of the session() callback I've put an additional prop accessToken to use it on api routes. I just noticed that /api/auth/session returns accessToken for the client side getSession() method. Is that possible to fix it?
async session({ session, token, trigger }) {
    session.user = {
        ...session.user,
        accessToken: (token.user as IAuthUser).access_token
    }
    return session;
},

useEffect(() => {
    if (!user) getSession().then((session) => (session ?? { user: null })).catch(() => ({ user: null }))
        .then(({ user }) => {
            console.log(user) // returns { ...user, accessToken: "..."  }
            setUser(authUser);
        })
}, [ user ]);

If you will say - It's ok to let client to see the token. I'll trust you. But I'm for now sure that this is the right way

1 Reply

Mexican JayOP
getSession() // { user: { name, image, email, accessToken, id } }
getServerSession(authProps) // { user: { name, image, email, accessToken, id } }
getServerSession() // { user: { name, image, email } }

I just realized that I can inherit the user prop from the page/layout (server side) component where I can exec getServerSession() without authProps. But using this way I wont get the id of the user. Is that possible to configure default user object type?