Skip to content

Commit 7e7777d

Browse files
committed
Adjust customization of session callback
The customization was suppose to only add "id" to the "user" object of the session callback response (exposed via /api/auth/session), but when this customization is added, the callback also starts returning otherwise secret information - namely the "sessionToken". This is a problem because the session token is suppose to only be stored in an HttpOnly cookie in the browser and on the server side, making it inaccessible to JavaScript. But with the /api/auth/session endpoint returning the session token it is easily accessible from JavaScript by doing a network request. With this change the session object is explicitly constructed.
1 parent 9bcdd4d commit 7e7777d

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/composition.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,18 @@ export const { signIn, auth, handlers: authHandlers } = NextAuth({
110110
return await logInHandler.handleLogIn({ user, account })
111111
},
112112
async session({ session, user }) {
113-
session.user.id = user.id
114-
return session
113+
// Construct a new session object conforming to DefaultSession
114+
// If "session" is returned it will include everything from AdapterSession,
115+
// which is critical as this contains the sessionToken
116+
return {
117+
user: {
118+
id: user.id,
119+
email: user.email,
120+
name: user.name,
121+
image: user.image
122+
},
123+
expires: session.expires,
124+
}
115125
}
116126
}
117127
})

0 commit comments

Comments
 (0)