mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
integrate export const checkEmailVerificationCode = async (
This commit is contained in:
parent
10781b2e6b
commit
bbd163f1e0
@ -141,35 +141,6 @@ const newEmailContact = (email: string, userId: number): DbUserContact => {
|
|||||||
return emailContact
|
return emailContact
|
||||||
}
|
}
|
||||||
|
|
||||||
export const checkEmailVerificationCode = async (
|
|
||||||
emailContact: DbUserContact,
|
|
||||||
optInType: OptInType = OptInType.EMAIL_OPT_IN_REGISTER,
|
|
||||||
): Promise<DbUserContact> => {
|
|
||||||
logger.info(`checkEmailVerificationCode... ${emailContact}`)
|
|
||||||
if (!canEmailResend(emailContact.updatedAt || emailContact.createdAt)) {
|
|
||||||
logger.error(
|
|
||||||
`email already sent less than ${printTimeDuration(
|
|
||||||
CONFIG.EMAIL_CODE_REQUEST_TIME,
|
|
||||||
)} minutes ago`,
|
|
||||||
)
|
|
||||||
throw new Error(
|
|
||||||
`email already sent less than ${printTimeDuration(
|
|
||||||
CONFIG.EMAIL_CODE_REQUEST_TIME,
|
|
||||||
)} minutes ago`,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
emailContact.updatedAt = new Date()
|
|
||||||
emailContact.emailResendCount++
|
|
||||||
emailContact.emailVerificationCode = random(64)
|
|
||||||
emailContact.emailOptInTypeId = optInType
|
|
||||||
await DbUserContact.save(emailContact).catch(() => {
|
|
||||||
logger.error('Unable to save email verification code= ' + emailContact)
|
|
||||||
throw new Error('Unable to save email verification code.')
|
|
||||||
})
|
|
||||||
logger.info(`checkEmailVerificationCode...successful: ${emailContact}`)
|
|
||||||
return emailContact
|
|
||||||
}
|
|
||||||
|
|
||||||
export const activationLink = (verificationCode: BigInt): string => {
|
export const activationLink = (verificationCode: BigInt): string => {
|
||||||
logger.debug(`activationLink(${verificationCode})...`)
|
logger.debug(`activationLink(${verificationCode})...`)
|
||||||
return CONFIG.EMAIL_LINK_SETPASSWORD.replace(/{optin}/g, verificationCode.toString())
|
return CONFIG.EMAIL_LINK_SETPASSWORD.replace(/{optin}/g, verificationCode.toString())
|
||||||
@ -492,21 +463,32 @@ export class UserResolver {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// can be both types: REGISTER and RESET_PASSWORD
|
if (!canEmailResend(user.emailContact.updatedAt || user.emailContact.createdAt)) {
|
||||||
// let optInCode = await LoginEmailOptIn.findOne({
|
logger.error(
|
||||||
// userId: user.id,
|
`email already sent less than ${printTimeDuration(
|
||||||
// })
|
CONFIG.EMAIL_CODE_REQUEST_TIME,
|
||||||
// let optInCode = user.emailContact.emailVerificationCode
|
)} minutes ago`,
|
||||||
const dbUserContact = await checkEmailVerificationCode(
|
|
||||||
user.emailContact,
|
|
||||||
OptInType.EMAIL_OPT_IN_RESET_PASSWORD,
|
|
||||||
)
|
)
|
||||||
|
throw new Error(
|
||||||
|
`email already sent less than ${printTimeDuration(
|
||||||
|
CONFIG.EMAIL_CODE_REQUEST_TIME,
|
||||||
|
)} minutes ago`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// optInCode = await checkOptInCode(optInCode, user, OptInType.EMAIL_OPT_IN_RESET_PASSWORD)
|
user.emailContact.updatedAt = new Date()
|
||||||
logger.info(`optInCode for ${email}=${dbUserContact}`)
|
user.emailContact.emailResendCount++
|
||||||
|
user.emailContact.emailVerificationCode = random(64)
|
||||||
|
user.emailContact.emailOptInTypeId = OptInType.EMAIL_OPT_IN_RESET_PASSWORD
|
||||||
|
await user.emailContact.save().catch(() => {
|
||||||
|
logger.error('Unable to save email verification code= ' + user.emailContact)
|
||||||
|
throw new Error('Unable to save email verification code.')
|
||||||
|
})
|
||||||
|
|
||||||
|
logger.info(`optInCode for ${email}=${user.emailContact}`)
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
const emailSent = await sendResetPasswordEmailMailer({
|
const emailSent = await sendResetPasswordEmailMailer({
|
||||||
link: activationLink(dbUserContact.emailVerificationCode),
|
link: activationLink(user.emailContact.emailVerificationCode),
|
||||||
firstName: user.firstName,
|
firstName: user.firstName,
|
||||||
lastName: user.lastName,
|
lastName: user.lastName,
|
||||||
email,
|
email,
|
||||||
@ -516,7 +498,7 @@ export class UserResolver {
|
|||||||
/* uncomment this, when you need the activation link on the console */
|
/* uncomment this, when you need the activation link on the console */
|
||||||
// In case EMails are disabled log the activation link for the user
|
// In case EMails are disabled log the activation link for the user
|
||||||
if (!emailSent) {
|
if (!emailSent) {
|
||||||
logger.debug(`Reset password link: ${activationLink(dbUserContact.emailVerificationCode)}`)
|
logger.debug(`Reset password link: ${activationLink(user.emailContact.emailVerificationCode)}`)
|
||||||
}
|
}
|
||||||
logger.info(`forgotPassword(${email}) successful...`)
|
logger.info(`forgotPassword(${email}) successful...`)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user