Change RESEND_TIME config name to EMAIL_CODE_VALID_TIME.

This commit is contained in:
elweyn 2022-03-14 12:49:34 +01:00
parent c975ac0aa2
commit ac3340ec1c
3 changed files with 21 additions and 12 deletions

View File

@ -40,7 +40,7 @@ EMAIL_SMTP_URL=gmail.com
EMAIL_SMTP_PORT=587 EMAIL_SMTP_PORT=587
EMAIL_LINK_VERIFICATION=http://localhost/checkEmail/{code} EMAIL_LINK_VERIFICATION=http://localhost/checkEmail/{code}
EMAIL_LINK_SETPASSWORD=http://localhost/reset/{code} EMAIL_LINK_SETPASSWORD=http://localhost/reset/{code}
RESEND_TIME=10 EMAIL_CODE_VALID_TIME=10
# Webhook # Webhook
WEBHOOK_ELOPAGE_SECRET=secret WEBHOOK_ELOPAGE_SECRET=secret

View File

@ -55,7 +55,9 @@ const loginServer = {
} }
// TODO: Hannes if I find you... this looks like blasphemy // TODO: Hannes if I find you... this looks like blasphemy
const resendTime = parseInt(process.env.RESEND_TIME ? process.env.RESEND_TIME : 'null') const resendTime = parseInt(
process.env.EMAIL_CODE_VALID_TIME ? process.env.EMAIL_CODE_VALID_TIME : 'null',
)
const email = { const email = {
EMAIL: process.env.EMAIL === 'true' || false, EMAIL: process.env.EMAIL === 'true' || false,
EMAIL_USERNAME: process.env.EMAIL_USERNAME || 'gradido_email', EMAIL_USERNAME: process.env.EMAIL_USERNAME || 'gradido_email',
@ -67,7 +69,7 @@ const email = {
process.env.EMAIL_LINK_VERIFICATION || 'http://localhost/checkEmail/{code}', process.env.EMAIL_LINK_VERIFICATION || 'http://localhost/checkEmail/{code}',
EMAIL_LINK_SETPASSWORD: EMAIL_LINK_SETPASSWORD:
process.env.EMAIL_LINK_SETPASSWORD || 'http://localhost/reset-password/{code}', process.env.EMAIL_LINK_SETPASSWORD || 'http://localhost/reset-password/{code}',
RESEND_TIME: isNaN(resendTime) ? 10 : resendTime, EMAIL_CODE_VALID_TIME: isNaN(resendTime) ? 10 : resendTime,
} }
const webhook = { const webhook = {

View File

@ -158,9 +158,11 @@ const createEmailOptIn = async (
}) })
if (emailOptIn) { if (emailOptIn) {
const timeElapsed = Date.now() - new Date(emailOptIn.updatedAt).getTime() const timeElapsed = Date.now() - new Date(emailOptIn.updatedAt).getTime()
if (timeElapsed <= parseInt(CONFIG.RESEND_TIME.toString()) * 60 * 1000) { if (timeElapsed <= parseInt(CONFIG.EMAIL_CODE_VALID_TIME.toString()) * 60 * 1000) {
throw new Error( throw new Error(
'email already sent less than ' + parseInt(CONFIG.RESEND_TIME.toString()) + ' minutes ago', 'email already sent less than ' +
parseInt(CONFIG.EMAIL_CODE_VALID_TIME.toString()) +
' minutes ago',
) )
} else { } else {
emailOptIn.updatedAt = new Date() emailOptIn.updatedAt = new Date()
@ -189,9 +191,11 @@ const getOptInCode = async (loginUserId: number): Promise<LoginEmailOptIn> => {
// Check for 10 minute delay // Check for 10 minute delay
if (optInCode) { if (optInCode) {
const timeElapsed = Date.now() - new Date(optInCode.updatedAt).getTime() const timeElapsed = Date.now() - new Date(optInCode.updatedAt).getTime()
if (timeElapsed <= parseInt(CONFIG.RESEND_TIME.toString()) * 60 * 1000) { if (timeElapsed <= parseInt(CONFIG.EMAIL_CODE_VALID_TIME.toString()) * 60 * 1000) {
throw new Error( throw new Error(
'email already sent less than ' + parseInt(CONFIG.RESEND_TIME.toString()) + ' minutes ago', 'email already sent less than ' +
parseInt(CONFIG.EMAIL_CODE_VALID_TIME.toString()) +
' minutes ago',
) )
} else { } else {
optInCode.updatedAt = new Date() optInCode.updatedAt = new Date()
@ -486,8 +490,10 @@ export class UserResolver {
// Code is only valid for 10minutes // Code is only valid for 10minutes
const timeElapsed = Date.now() - new Date(optInCode.updatedAt).getTime() const timeElapsed = Date.now() - new Date(optInCode.updatedAt).getTime()
if (timeElapsed > parseInt(CONFIG.RESEND_TIME.toString()) * 60 * 1000) { if (timeElapsed > parseInt(CONFIG.EMAIL_CODE_VALID_TIME.toString()) * 60 * 1000) {
throw new Error('Code is older than ' + parseInt(CONFIG.RESEND_TIME.toString()) + ' minutes') throw new Error(
'Code is older than ' + parseInt(CONFIG.EMAIL_CODE_VALID_TIME.toString()) + ' minutes',
)
} }
// load user // load user
@ -562,11 +568,12 @@ export class UserResolver {
@Query(() => Boolean) @Query(() => Boolean)
async queryOptIn(@Arg('optIn') optIn: string): Promise<boolean> { async queryOptIn(@Arg('optIn') optIn: string): Promise<boolean> {
const optInCode = await LoginEmailOptIn.findOneOrFail({ verificationCode: optIn }) const optInCode = await LoginEmailOptIn.findOneOrFail({ verificationCode: optIn })
console.log('optInCode', optInCode)
// Code is only valid for 10minutes // Code is only valid for 10minutes
const timeElapsed = Date.now() - new Date(optInCode.updatedAt).getTime() const timeElapsed = Date.now() - new Date(optInCode.updatedAt).getTime()
if (timeElapsed > parseInt(CONFIG.RESEND_TIME.toString()) * 60 * 1000) { if (timeElapsed > parseInt(CONFIG.EMAIL_CODE_VALID_TIME.toString()) * 60 * 1000) {
throw new Error('Code is older than ' + parseInt(CONFIG.RESEND_TIME.toString()) + ' minutes') throw new Error(
'Code is older than ' + parseInt(CONFIG.EMAIL_CODE_VALID_TIME.toString()) + ' minutes',
)
} }
return true return true
} }