mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Config of resendTime to resendPassword logic
This commit is contained in:
parent
da527dd162
commit
ffe3cdf700
@ -506,43 +506,49 @@ export class UserResolver {
|
|||||||
return emailOptIn
|
return emailOptIn
|
||||||
}
|
}
|
||||||
|
|
||||||
@Authorized([RIGHTS.SEND_RESET_PASSWORD_EMAIL])
|
private async getOptInCode(loginUser: LoginUser) {
|
||||||
@Query(() => Boolean)
|
|
||||||
async sendResetPasswordEmail(@Arg('email') email: string): Promise<boolean> {
|
|
||||||
// TODO: this has duplicate code with createUser
|
|
||||||
// TODO: Moriz: I think we do not need this variable.
|
|
||||||
let emailAlreadySend = false
|
|
||||||
|
|
||||||
const loginUserRepository = await getCustomRepository(LoginUserRepository)
|
|
||||||
const loginUser = await loginUserRepository.findOneOrFail({ email })
|
|
||||||
|
|
||||||
const loginEmailOptInRepository = await getRepository(LoginEmailOptIn)
|
const loginEmailOptInRepository = await getRepository(LoginEmailOptIn)
|
||||||
let optInCode = await loginEmailOptInRepository.findOne({
|
let optInCode = await loginEmailOptInRepository.findOne({
|
||||||
userId: loginUser.id,
|
userId: loginUser.id,
|
||||||
emailOptInTypeId: EMAIL_OPT_IN_RESET_PASSWORD,
|
emailOptInTypeId: EMAIL_OPT_IN_RESET_PASSWORD,
|
||||||
})
|
})
|
||||||
if (optInCode) {
|
if (optInCode) {
|
||||||
emailAlreadySend = true
|
const timeElapsed = Date.now() - new Date(optInCode.updatedAt).getTime()
|
||||||
|
if (timeElapsed <= parseInt(CONFIG.RESEND_TIME.toString()) * 60 * 1000) {
|
||||||
|
throw new Error(
|
||||||
|
'email already sent less than ' +
|
||||||
|
parseInt(CONFIG.RESEND_TIME.toString()) +
|
||||||
|
' minutes ago',
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
optInCode.updatedAt = new Date()
|
||||||
|
optInCode.resendCount++
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
optInCode = new LoginEmailOptIn()
|
optInCode = new LoginEmailOptIn()
|
||||||
optInCode.verificationCode = random(64)
|
optInCode.verificationCode = random(64)
|
||||||
optInCode.userId = loginUser.id
|
optInCode.userId = loginUser.id
|
||||||
optInCode.emailOptInTypeId = EMAIL_OPT_IN_RESET_PASSWORD
|
optInCode.emailOptInTypeId = EMAIL_OPT_IN_RESET_PASSWORD
|
||||||
await loginEmailOptInRepository.save(optInCode)
|
|
||||||
}
|
}
|
||||||
|
await loginEmailOptInRepository.save(optInCode)
|
||||||
|
return optInCode
|
||||||
|
}
|
||||||
|
|
||||||
|
@Authorized([RIGHTS.SEND_RESET_PASSWORD_EMAIL])
|
||||||
|
@Query(() => Boolean)
|
||||||
|
async sendResetPasswordEmail(@Arg('email') email: string): Promise<boolean> {
|
||||||
|
// TODO: this has duplicate code with createUser
|
||||||
|
|
||||||
|
const loginUserRepository = await getCustomRepository(LoginUserRepository)
|
||||||
|
const loginUser = await loginUserRepository.findOneOrFail({ email })
|
||||||
|
|
||||||
|
const optInCode = await this.getOptInCode(loginUser)
|
||||||
|
|
||||||
const link = CONFIG.EMAIL_LINK_SETPASSWORD.replace(
|
const link = CONFIG.EMAIL_LINK_SETPASSWORD.replace(
|
||||||
/\$1/g,
|
/\$1/g,
|
||||||
optInCode.verificationCode.toString(),
|
optInCode.verificationCode.toString(),
|
||||||
)
|
)
|
||||||
|
|
||||||
if (emailAlreadySend) {
|
|
||||||
const timeElapsed = Date.now() - new Date(optInCode.updatedAt).getTime()
|
|
||||||
if (timeElapsed <= 10 * 60 * 1000) {
|
|
||||||
throw new Error('email already sent less than 10 minutes before')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const emailSent = await sendEMail({
|
const emailSent = await sendEMail({
|
||||||
from: `Gradido (nicht antworten) <${CONFIG.EMAIL_SENDER}>`,
|
from: `Gradido (nicht antworten) <${CONFIG.EMAIL_SENDER}>`,
|
||||||
to: `${loginUser.firstName} ${loginUser.lastName} <${email}>`,
|
to: `${loginUser.firstName} ${loginUser.lastName} <${email}>`,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user