isDate() returns true for non dates in edge cases that make use of delimiters '#' or '-'
☕️ 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
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 |
@nerkmind1337 is attempting to deploy a commit to the authjs Team on Vercel.
A member of the Team first needs to authorize it.
Did anyone get the chance to look at this? it's been blocking me for months.
guys?
Its been 6 months and this is still broken, please merge my fix.
bumping @balazsorban44 its been almost a year.
@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:
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