-
-
Notifications
You must be signed in to change notification settings - Fork 117
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
Login with magic code #1818
Login with magic code #1818
Changes from 1 commit
2abe1cb
e130705
cfc01c5
c357bc7
2f29f30
33bbdc3
8fb7cdb
7a0e794
3efa20d
841cd9a
8d41679
c65e62b
e663b75
5c26cd6
0b6da66
2ad5d04
6155eef
9658807
a8b8102
5c7e17f
33a9d47
908b70e
4fa221b
766ca22
b0b8a38
390f79b
8085a71
dd2e75c
6527e22
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -329,14 +329,17 @@ export const getAuthOptions = (req, res) => ({ | |
// we need to find the most recent verification request for this email/identifier | ||
const verificationRequest = await prisma.verificationToken.findFirst({ | ||
where: { | ||
identifier | ||
identifier, | ||
attempts: { | ||
lt: 2 // count starts at 0 | ||
} | ||
}, | ||
orderBy: { | ||
createdAt: 'desc' | ||
} | ||
}) | ||
|
||
if (!verificationRequest) return null | ||
if (!verificationRequest) throw new Error('No verification request found') | ||
|
||
if (verificationRequest.token === token) { // if correct delete the token and continue | ||
await prisma.verificationToken.delete({ | ||
|
@@ -345,17 +348,14 @@ export const getAuthOptions = (req, res) => ({ | |
return verificationRequest | ||
} | ||
|
||
const newAttempts = verificationRequest.attempts + 1 | ||
if (newAttempts > 3) { // the moment the user has tried 3 times, delete the token | ||
await prisma.verificationToken.delete({ | ||
where: { id: verificationRequest.id } | ||
}) | ||
} else { // otherwise, just increment the failed attempts | ||
await prisma.verificationToken.update({ | ||
where: { id: verificationRequest.id }, | ||
data: { attempts: newAttempts } | ||
}) | ||
} | ||
await prisma.verificationToken.update({ | ||
where: { id: verificationRequest.id }, | ||
data: { attempts: { increment: 1 } } | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your changes were perfectly fine, but the increment wasn't an atomic read-write; it read from the db in one tx, then updated it in another. While it's still possible with my fixes for someone to get more than 3 attempts in quick succession, this makes it a little less likely. Ideally, this code would not have any races at all, but it's a bit tricky because |
||
|
||
await prisma.verificationToken.deleteMany({ | ||
where: { id: verificationRequest.id, attempts: { gte: 2 } } | ||
}) | ||
|
||
return null | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This forces a redirect to the login page (my change that is) if all 3 attempts have been tried which lets us avoid a the "go back or login again" on the error page. Instead, the button on the error page just goes back in the browser history.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a way better UX, thanks!