Skip to content

Commit e70057f

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 e70057f

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/composition.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Pool } from "pg"
2-
import NextAuth from "next-auth"
2+
import NextAuth, { DefaultSession } from "next-auth"
33
import GithubProvider from "next-auth/providers/github"
44
import PostgresAdapter from "@auth/pg-adapter"
55
import RedisKeyedMutexFactory from "@/common/mutex/RedisKeyedMutexFactory"
@@ -110,8 +110,13 @@ 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+
return {
114+
user: {
115+
...session.user,
116+
id: user.id,
117+
},
118+
expires: session.expires,
119+
}
115120
}
116121
}
117122
})

0 commit comments

Comments
 (0)