Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Forward idToken to callbacks #837

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/server/lib/oauth/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default async (req, provider, csrfToken, callback) => {
results.id_token,
async (error, profileData) => {
const { profile, account, OAuthProfile } = await _getProfile(error, profileData, accessToken, refreshToken, provider, user)
callback(error, profile, account, OAuthProfile)
callback(error, profile, account, OAuthProfile, results.id_token)
}
)
} else {
Expand All @@ -96,7 +96,7 @@ export default async (req, provider, csrfToken, callback) => {
results,
async (error, profileData) => {
const { profile, account, OAuthProfile } = await _getProfile(error, profileData, accessToken, refreshToken, provider)
callback(error, profile, account, OAuthProfile)
callback(error, profile, account, OAuthProfile, results.id_token)
}
)
}
Expand Down
6 changes: 3 additions & 3 deletions src/server/routes/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default async (req, res, options, done) => {

if (type === 'oauth') {
try {
oAuthCallback(req, provider, csrfToken, async (error, profile, account, OAuthProfile) => {
oAuthCallback(req, provider, csrfToken, async (error, profile, account, OAuthProfile, idToken) => {
try {
if (error) {
logger.error('CALLBACK_OAUTH_ERROR', error)
Expand Down Expand Up @@ -68,7 +68,7 @@ export default async (req, res, options, done) => {
}

try {
const signInCallbackResponse = await callbacks.signIn(userOrProfile, account, OAuthProfile)
const signInCallbackResponse = await callbacks.signIn(userOrProfile, account, OAuthProfile, idToken)
if (signInCallbackResponse === false) {
return redirect(`${baseUrl}${basePath}/error?error=AccessDenied`)
}
Expand All @@ -90,7 +90,7 @@ export default async (req, res, options, done) => {
picture: user.image,
sub: user.id?.toString()
}
const jwtPayload = await callbacks.jwt(defaultJwtPayload, user, account, OAuthProfile, isNewUser)
const jwtPayload = await callbacks.jwt(defaultJwtPayload, user, account, OAuthProfile, isNewUser, idToken)

// Sign and encrypt token
const newEncodedJwt = await jwt.encode({ ...jwt, token: jwtPayload })
Expand Down
6 changes: 4 additions & 2 deletions www/docs/configuration/callbacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ You can specify a handler for any of the callbacks below.
```js title="pages/api/auth/[...nextauth].js"
...
callbacks: {
signIn: async (user, account, profile) => {
signIn: async (user, account, profile, idToken) => {
return Promise.resolve(true)
},
redirect: async (url, baseUrl) => {
Expand All @@ -25,7 +25,7 @@ You can specify a handler for any of the callbacks below.
session: async (session, user) => {
return Promise.resolve(session)
},
jwt: async (token, user, account, profile, isNewUser) => {
jwt: async (token, user, account, profile, isNewUser, idToken) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iaincollins This (and probably any other public-facing function signature using more than 2 params) should at some point (maybe v4) become an object to leverage named parameters It is much easier for the user to pick only those params that they are going to use.

return Promise.resolve(token)
}
...
Expand All @@ -44,6 +44,7 @@ callbacks: {
* @param {object} user User object
* @param {object} account Provider account
* @param {object} profile Provider profile
* @param {string} idToken OpenID Connect `id_token` returned by compliant Providers
* @return {boolean} Return `true` (or a modified JWT) to allow sign in
* Return `false` to deny access
*/
Expand Down Expand Up @@ -165,6 +166,7 @@ callbacks: {
* @param {object} account Provider account (only available on sign in)
* @param {object} profile Provider profile (only available on sign in)
* @param {boolean} isNewUser True if new user (only available on sign in)
* @param {string} idToken OpenID Connect `id_token` returned by compliant Providers (only available on sign in)
* @return {object} JSON Web Token that will be saved
*/
jwt: async (token, user, account, profile, isNewUser) => {
Expand Down
4 changes: 2 additions & 2 deletions www/docs/configuration/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ You can specify a handler for any of the callbacks below.

```js
callbacks: {
signIn: async (user, account, profile) => {
signIn: async (user, account, profile, idToken) => {
return Promise.resolve(true)
},
redirect: async (url, baseUrl) => {
Expand All @@ -239,7 +239,7 @@ callbacks: {
session: async (session, user) => {
return Promise.resolve(session)
},
jwt: async (token, user, account, profile, isNewUser) => {
jwt: async (token, user, account, profile, isNewUser, idToken) => {
return Promise.resolve(token)
}
}
Expand Down