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

@keystone-6/auth assumes a non-zero itemId #8843

Open
mmtftr opened this issue Sep 29, 2023 · 5 comments
Open

@keystone-6/auth assumes a non-zero itemId #8843

mmtftr opened this issue Sep 29, 2023 · 5 comments
Labels
🐛 bug Unresolved bug

Comments

@mmtftr
Copy link

mmtftr commented Sep 29, 2023

if (!session.itemId) return;

I had a user with ID===0 where the field was autoincrement. Can we support this use case?
This check should either be "itemId" in session or !session.itemId && session.itemId !== 0)

@dcousens
Copy link
Member

@mmtftr nice find!
Did you want to open a pull request? 💙

@dcousens dcousens added the 🐛 bug Unresolved bug label Sep 30, 2023
@dcousens
Copy link
Member

dcousens commented Sep 30, 2023

As you mentioned, we have a few ways we could approach this, when session.itemid could be null, undefined or missing.

if (!('itemId' in session))
/* 
  Results
    0: true
    '': true
    undefined: true
    null: true
    missing: false
*/

I don't think we can recommend this approach, users might mistakenly returning itemId: undefined | null from session.get, which could leave them with an invalid session.
That and empty string is now accepted, which it shouldn't be.


if (typeof session.itemId !== 'string' && typeof session.itemId !== 'number')
/* 
  Results
    0: true
    '': true
    undefined: false
    null: false
    missing: false
*/

This approach is nearly there, except empty string is now accepted.


if (!session.itemId && session.itemId !== 0)
/* 
  Results
    0: true
    '': false
    undefined: false
    null: false
    missing: false
*/

Not bad, but it leaves the reader questioning what we're actually trying to do.


if (
  session.itemId === undefined 
  || sesion.itemId === null
  || sesion.itemId === ''
)
/* 
  Results
    0: true
    '': false
    undefined: false
    null: false
    missing: false
*/

This may be the most verbose, but, it's clear what we're trying to do?

Interested to see how this plays out in the pull request, and if we have any tests to this effect.

@dcousens
Copy link
Member

dcousens commented Sep 30, 2023

@mmtftr alternatively, we could prevent an identifier of 0 in a way that is a little more consistent

@mmtftr
Copy link
Author

mmtftr commented Sep 30, 2023

I think another important point is that this behavior is not at all documented and these problems with the session object are not reported to the administrator/developer. I had to read through the Keystone source code to find out why I didn't have sessions despite logging in. I am also not familiar with how to debug a Next.js application in dev mode (Using a JavaScript Debug Terminal didn't work, for some reason I couldn't find any Keystone code that was loaded, there was some loaded WASM code).

Thus, I think adding a console warning log or some other kind of warning is appropriate for the latter 2 if statements in the following code snippet:

if (!session) return;
if (!session.itemId) return;
if (session.listKey !== listKey) return;

The first return is an expected case where we don't have a session but the other two cases are when a sessionStrategy returns an unexpected session object that's not compatible with keystone auth. I believe warning the user is the right choice here, I'm not specific about how that warning happens though.

For the actual check, I could write it concisely like [undefined, null, ''].includes(session.itemId) which satisfies the expected behavior.

@dcousens
Copy link
Member

I don't think we want to add warnings by default for something like this, but you can always wrap the session strategy yourself.
@mmtftr did you want to open a pull request?

@dcousens dcousens changed the title Keystone Auth session is not resolved if itemId===0 @keystone-6/auth assumes a non-zero itemId Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Unresolved bug
Projects
None yet
Development

No branches or pull requests

2 participants