From ac3340ec1cd6a80b40ca905052208acdfd42ca8e Mon Sep 17 00:00:00 2001 From: elweyn Date: Mon, 14 Mar 2022 12:49:34 +0100 Subject: [PATCH] Change RESEND_TIME config name to EMAIL_CODE_VALID_TIME. --- backend/.env.dist | 2 +- backend/src/config/index.ts | 6 +++-- backend/src/graphql/resolver/UserResolver.ts | 25 +++++++++++++------- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/backend/.env.dist b/backend/.env.dist index b1b16972f..3c93f1576 100644 --- a/backend/.env.dist +++ b/backend/.env.dist @@ -40,7 +40,7 @@ EMAIL_SMTP_URL=gmail.com EMAIL_SMTP_PORT=587 EMAIL_LINK_VERIFICATION=http://localhost/checkEmail/{code} EMAIL_LINK_SETPASSWORD=http://localhost/reset/{code} -RESEND_TIME=10 +EMAIL_CODE_VALID_TIME=10 # Webhook WEBHOOK_ELOPAGE_SECRET=secret \ No newline at end of file diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index 79101856c..9e7f3b6c4 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -55,7 +55,9 @@ const loginServer = { } // 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 = { EMAIL: process.env.EMAIL === 'true' || false, EMAIL_USERNAME: process.env.EMAIL_USERNAME || 'gradido_email', @@ -67,7 +69,7 @@ const email = { process.env.EMAIL_LINK_VERIFICATION || 'http://localhost/checkEmail/{code}', EMAIL_LINK_SETPASSWORD: 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 = { diff --git a/backend/src/graphql/resolver/UserResolver.ts b/backend/src/graphql/resolver/UserResolver.ts index 3a70c46ee..f36868f92 100644 --- a/backend/src/graphql/resolver/UserResolver.ts +++ b/backend/src/graphql/resolver/UserResolver.ts @@ -158,9 +158,11 @@ const createEmailOptIn = async ( }) if (emailOptIn) { 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( - '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 { emailOptIn.updatedAt = new Date() @@ -189,9 +191,11 @@ const getOptInCode = async (loginUserId: number): Promise => { // Check for 10 minute delay if (optInCode) { 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( - '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 { optInCode.updatedAt = new Date() @@ -486,8 +490,10 @@ export class UserResolver { // Code is only valid for 10minutes const timeElapsed = Date.now() - new Date(optInCode.updatedAt).getTime() - if (timeElapsed > parseInt(CONFIG.RESEND_TIME.toString()) * 60 * 1000) { - throw new Error('Code is older than ' + parseInt(CONFIG.RESEND_TIME.toString()) + ' minutes') + if (timeElapsed > parseInt(CONFIG.EMAIL_CODE_VALID_TIME.toString()) * 60 * 1000) { + throw new Error( + 'Code is older than ' + parseInt(CONFIG.EMAIL_CODE_VALID_TIME.toString()) + ' minutes', + ) } // load user @@ -562,11 +568,12 @@ export class UserResolver { @Query(() => Boolean) async queryOptIn(@Arg('optIn') optIn: string): Promise { const optInCode = await LoginEmailOptIn.findOneOrFail({ verificationCode: optIn }) - console.log('optInCode', optInCode) // Code is only valid for 10minutes const timeElapsed = Date.now() - new Date(optInCode.updatedAt).getTime() - if (timeElapsed > parseInt(CONFIG.RESEND_TIME.toString()) * 60 * 1000) { - throw new Error('Code is older than ' + parseInt(CONFIG.RESEND_TIME.toString()) + ' minutes') + if (timeElapsed > parseInt(CONFIG.EMAIL_CODE_VALID_TIME.toString()) * 60 * 1000) { + throw new Error( + 'Code is older than ' + parseInt(CONFIG.EMAIL_CODE_VALID_TIME.toString()) + ' minutes', + ) } return true }