Skip to content

Conversation

@alv92
Copy link

@alv92 alv92 commented Jan 23, 2024

☕️ Reasoning

What needed fixing:

Hi!

Currently, when signing in using a provider, we're populating the Account with refresh and access tokens. These are left static for the entire lifetime of the Account, which I think shouldn't be the case as that will turn at some point on revoked access if the user clicks “sign me out of all my devices” on their provider account, their admin triggers a password loss procedure or if we change the scopes we request on our token.

This is why in this PR I aim to update the Account with newly issued refresh and access tokens with each subsequent login from the user.

This is a PoC (tested locally, works) and I'd love to get some insights on how feasible this solution is to be implemented in Next Auth, both on core and on v4 since we need it in several of our projects.

Changes:

I created a new adapter function called updateAccount() (on prisma adapter only as a PoC) which takes an Account and a UserID as props. This function will search for that account on the database and update it with the updated account information on login. In case that Account does not exist, it will create a new entry, which means this function could potentially be used as well for linking an account as well.

The function is optional to make sure no other adapters are being broken after merging.

🧢 Checklist

  • Documentation
  • Tests
  • Ready to be merged

🎫 Affected issues

📌 Resources

@vercel
Copy link

vercel bot commented Jan 23, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
auth-docs ❌ Failed (Inspect) Jan 31, 2024 3:00pm
1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
next-auth-docs ⬜️ Ignored (Inspect) Visit Preview Jan 31, 2024 3:00pm

@github-actions github-actions bot added adapters Changes related to the core code concerning database adapters core Refers to `@auth/core` prisma @auth/prisma-adapter labels Jan 23, 2024
Copy link
Member

@balazsorban44 balazsorban44 left a comment

Choose a reason for hiding this comment

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

This looks interesting, and probably something we would like to consider! However the correct flow is to create a feature request first:

https://github.com/nextauthjs/next-auth/discussions/new?category=ideas

I also recommend splitting this to multiple PRs so the adapter can be addressed separately.

@alv92
Copy link
Author

alv92 commented Jan 24, 2024

@balazsorban44 thanks for the quick reply! I've added the discussion as requested. Should I delete this pull request and create a new one for each change?

@balazsorban44
Copy link
Member

We can keep the PR for now!

I think if we introduce this behind an experimental flag BTW, we could merge this suggestion as-is.

Some code that might be useful. To throw a proper error if the flag is missing, something like:

function experimentalStructuredAuthEnabled(config: NextAuthConfig) {
if (config.experimental?.structuredAuth === true) return
throw new ExperimentalAuthError(
"`experimental.structuredAuth` must be enabled to use `unstable_auth`"
)
}

Then you an extend the experimental config here:

experimental: Record<string, boolean>

@balazsorban44 balazsorban44 changed the title feat/update account on login feat: update account on login Jan 24, 2024
@github-actions github-actions bot added the prisma @auth/prisma-adapter label Jan 25, 2024
@alv92
Copy link
Author

alv92 commented Jan 25, 2024

Awesome, sounds great!

Is the next-auth/packages/next-auth/src/lib/future/index.ts file available on main? Do I need to fork from your branch instead and point my PR there?

@balazsorban44
Copy link
Member

It's just to showcase a possible implementation. you can copy paste it and adjust the naming accordingly for this PR

@alv92
Copy link
Author

alv92 commented Jan 30, 2024

@balazsorban44 I added the experimental error handling function for updateAccount 😄 I went ahead and updated the documentation as well

@alv92 alv92 requested a review from balazsorban44 January 31, 2024 15:23
@alv92
Copy link
Author

alv92 commented Feb 16, 2024

Hi @balazsorban44! Is there anything else that should be done before we can proceed with merging these changes? 😄

@stale
Copy link

stale bot commented Jan 31, 2025

It looks like this issue did not receive any activity for 60 days. It will be closed in 7 days if no further activity occurs. If you think your issue is still relevant, commenting will keep it open. Thanks!

@stale stale bot added the stale Did not receive any activity for 60 days label Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

adapters Changes related to the core code concerning database adapters core Refers to `@auth/core` next-auth prisma @auth/prisma-adapter stale Did not receive any activity for 60 days

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants