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

[SDK-2813] Add afterRefresh hook #506

Merged
merged 3 commits into from
Sep 28, 2021
Merged

[SDK-2813] Add afterRefresh hook #506

merged 3 commits into from
Sep 28, 2021

Conversation

adamjmcgrath
Copy link
Contributor

@adamjmcgrath adamjmcgrath commented Sep 28, 2021

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 in afterCallback aren't reverted in getAccessToken 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 require initAuth) so we'll have to make do with these 3 hooks:

  • afterCallback: When the session is establised including tokens and user
  • afterRefetch: When the user is potentially updated from /userinfo
  • afterRefresh: When the tokens are potentially updated from the Refresh grant

References

Fixes #416
See updated FAQ

Testing

  • This change adds test coverage for new/changed/fixed functionality

Checklist

  • I have added documentation for new/changed functionality in this PR or in auth0.com/docs
  • All active GitHub checks for tests, formatting, and security are passing
  • The correct base branch is being used, if not main

@adamjmcgrath adamjmcgrath added the review:medium Medium review label Sep 28, 2021
@adamjmcgrath adamjmcgrath requested a review from a team as a code owner September 28, 2021 15:08
@vercel
Copy link

vercel bot commented Sep 28, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/auth0/nextjs-auth0/4mQhJEqNvqYyuSTnpmfTAqLGXhW1
✅ Preview: Canceled

[Deployment for 5905f47 canceled]

session = await accessTokenRequest.afterRefresh(req as NextApiRequest, res as NextApiResponse, session);
}

sessionCache.set(req, res, session as Session);
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

@adamjmcgrath adamjmcgrath Sep 28, 2021

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed c80ea31

Widcket
Widcket previously approved these changes Sep 28, 2021
@adamjmcgrath adamjmcgrath merged commit 216e994 into main Sep 28, 2021
@adamjmcgrath adamjmcgrath deleted the after-refresh-hook branch September 28, 2021 15:36
@adamjmcgrath adamjmcgrath mentioned this pull request Oct 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
review:medium Medium review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Modifications to session after login are overwritten over time
2 participants