Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/operations/login.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { PayloadRequest, TypedUser } from 'payload'

import {
APIError,
checkLoginPermission,
getFieldsToSign,
incrementLoginAttempts,
Expand Down Expand Up @@ -114,10 +115,23 @@ export const loginWithOTP = async ({ type, collection, otp, req, value }: Args)
dataToUpdate.loginAttempts = 0
}

const userData = await payload.db.findOne({
collection,
joins: false,
where: { id: { equals: user.id } },
})

if (!userData) {
throw new APIError(`User with ID=${user.id} was not found.`)
}

await payload.db.updateOne({
id: user.id,
collection,
data: dataToUpdate,
data: {
...userData,
dataToUpdate,
},
})

return { exp, token, user }
Expand Down
11 changes: 11 additions & 0 deletions src/operations/requestOTP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,21 @@ export const setOTP = async ({
const _otpExpiration = new Date(Date.now() + exp * 1000).toISOString()

try {
const userData = await payload.db.findOne({
collection,
joins: false,
where: { id: { equals: user.id } },
})

if (!userData) {
throw new APIError(`User with ID=${user.id} was not found.`)
}

await payload.db.updateOne({
id: user.id,
collection,
data: {
...userData,
_otp: encrypt({ payload, value: otp }),
_otpExpiration,
},
Expand Down