next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

isDate() returns true for non dates in edge cases that make use of delimiters '#' or '-'

Open nerkmind1337 opened this issue 2 years ago • 6 comments

☕️ Reasoning

there is a bug in the isDate function, which incorrectly formats usernames containing hashtags as dates.

isDate should work like this instead.

function isDate(testDate: string | number | Date): boolean {
  const date = new Date(testDate);
  return !isNaN(date.getTime()) && date.toString() !== 'Invalid Date';
}

In this version, we use getTime() to obtain the numeric value representing the date from the Date object. We then check if the obtained value is not NaN and the string representation of the date is not 'Invalid Date'.

I have tested this, but may have missed some edge cases. I'll submit a PR with my fix and see what you all think :)

🧢 Checklist

  • [x] Documentation - unsure where, if there is any, the documentation for this moving part lives.
  • [x] Tests
  • [x] Ready to be merged

🎫 Affected issues

Please scout and link issues that might be solved by this PR.

Fixes: isDate() returns true for non dates in edge cases that make use of delimiters '#' or '-'

📌 Resources

nerkmind1337 avatar May 17 '23 07:05 nerkmind1337

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

Name Status Preview Comments Updated (UTC)
auth-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 17, 2023 7:19am
1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
next-auth-docs ⬜️ Ignored (Inspect) May 17, 2023 7:19am

vercel[bot] avatar May 17 '23 07:05 vercel[bot]

@nerkmind1337 is attempting to deploy a commit to the authjs Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar May 17 '23 07:05 vercel[bot]

Did anyone get the chance to look at this? it's been blocking me for months.

nerkmind1337 avatar Jun 27 '23 08:06 nerkmind1337

guys?

nerkmind1337 avatar Sep 04 '23 10:09 nerkmind1337

Its been 6 months and this is still broken, please merge my fix.

nerkmind1337 avatar Nov 17 '23 07:11 nerkmind1337

bumping @balazsorban44 its been almost a year.

nerkmind1337 avatar Feb 02 '24 08:02 nerkmind1337

@nerkmind1337 this is impossible to merge, it includes a ton of unrelated changes.

Please update your PR to only include the relevant changes.

Might be easier to just take your changes and open a new PR from a fresh branch :pray:

ndom91 avatar Feb 24 '24 18:02 ndom91

Also regarding your issue, did I understand it correctly that username's like Username#123 (like Discord users) get misinterpreted?

I was able to reproduce the issue with the pre-existing isDate function in the Supabase adapter. I.e. this one:

function isDate(date) {
  return (
    new Date(date).toString() !== "Invalid Date" && !isNaN(Date.parse(date))
  )
}

However, your suggested isDate replacement seems to suffer from the same issue:

function isDate2(testDate) {
  const date = new Date(testDate);
  return !isNaN(date.getTime()) && date.toString() !== 'Invalid Date';
}
isDate2('Username#123')
// true

ndom91 avatar Feb 24 '24 18:02 ndom91