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

refactor(OTP): simplify code by using upsert() #1283

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Changes from 1 commit
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
60 changes: 24 additions & 36 deletions src/services/identity/UsersService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,20 +189,21 @@ class UsersService {
async sendEmailOtp(email: string) {
const parsedEmail = email.toLowerCase()
const { otp, hashedOtp } = await this.otpService.generateLoginOtpWithHash()
const data = {
hashedOtp,
attempts: 0,
expiresAt: this.getOtpExpiry(),
}

// Reset attempts to login
const otpEntry = await this.otpRepository.findOne({
// Reset attempts to login
timotheeg marked this conversation as resolved.
Show resolved Hide resolved
const [otpEntry, created] = await this.otpRepository.findOrCreate({
where: { email: parsedEmail },
defaults: data,
})
if (!otpEntry) {
// create new entry
await this.createOtpEntry(parsedEmail, OtpType.Email, hashedOtp)
} else {
await otpEntry?.update({
hashedOtp,
attempts: 0,
expiresAt: this.getOtpExpiry(),
})

if (!created) {
await otpEntry?.update(data)
timotheeg marked this conversation as resolved.
Show resolved Hide resolved
}

const subject = "One-Time Password (OTP) for IsomerCMS"
Expand All @@ -216,15 +217,22 @@ class UsersService {

async sendSmsOtp(mobileNumber: string) {
const { otp, hashedOtp } = await this.otpService.generateLoginOtpWithHash()
const data = {
hashedOtp,
attempts: 0,
expiresAt: this.getOtpExpiry(),
}

// Reset attempts to login
const otpEntry = await this.otpRepository.findOne({
where: { mobileNumber },
const [otpEntry, created] = await this.otpRepository.findOrCreate({
where: {
mobileNumber,
},
defaults: data,
})
if (!otpEntry) {
await this.createOtpEntry(mobileNumber, OtpType.Mobile, hashedOtp)
} else {
await otpEntry?.update({ hashedOtp, attempts: 0 })
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this update not updating expiresAt, like what sendEmailOtp() does?


if (!created) {
await otpEntry?.update(data)
}

const message = `Your OTP is ${otp}. It will expire in ${milliSecondsToMinutes(
Expand Down Expand Up @@ -433,26 +441,6 @@ class UsersService {
private getOtpExpiry() {
return new Date(Date.now() + OTP_EXPIRY)
}

private async createOtpEntry(
key: string,
keyType: OtpType,
hashedOtp: string
) {
if (keyType === OtpType.Email) {
await this.otpRepository.create({
email: key.toLowerCase(),
hashedOtp,
expiresAt: this.getOtpExpiry(),
})
} else {
await this.otpRepository.create({
mobileNumber: key,
hashedOtp,
expiresAt: this.getOtpExpiry(),
})
}
}
}

export default UsersService
Loading