-
Notifications
You must be signed in to change notification settings - Fork 389
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
[SDK-2813] Add afterRefresh hook #506
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/auth0/nextjs-auth0/4mQhJEqNvqYyuSTnpmfTAqLGXhW1 [Deployment for 5905f47 canceled] |
src/session/get-access-token.ts
Outdated
session = await accessTokenRequest.afterRefresh(req as NextApiRequest, res as NextApiResponse, session); | ||
} | ||
|
||
sessionCache.set(req, res, session as Session); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this require a cast, given that successfully awaiting afterRefresh
produces a Session?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sessionCache.get
can return undefined
(if a session hasn't been estabilished) and TS isn't smart enough to know that there is a session if client.refresh
is successful
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, my bad, I don't need to cast it - will fix, thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed c80ea31
Description
When the Access Token is refreshed, the session gets a new set of tokens (Access Token, ID Token, Refresh Token).
Our advice for dealing with large cookies is to delete optional data from the session (like ID Token in some cases).
In order to do this and have the tokens refreshed, we need to add another hook (this time to
getAccessToken
) that allows you to modify the session, so that changes like removing the ID Token inafterCallback
aren't reverted ingetAccessToken
when the session is refreshed.Ideally we'd have a
sessionUpdated
hook, that runs accross all scenarios where a session is updated, but we don't have a place to configure a non literal config globally (all config should be able to be defined as an environment variable and I don't want to introduce features that requireinitAuth
) so we'll have to make do with these 3 hooks:afterCallback
: When the session is establised including tokens and userafterRefetch
: When the user is potentially updated from/userinfo
afterRefresh
: When the tokens are potentially updated from the Refresh grantReferences
Fixes #416
See updated FAQ
Testing
Checklist
main