From aea58a9530a2e633ece0aceb9341235d3ea665f7 Mon Sep 17 00:00:00 2001 From: Ryan Coppolo Date: Tue, 13 Sep 2022 12:49:28 -0400 Subject: [PATCH 1/4] Simplify user settings example confirm logic --- examples/user-settings/javascript/index.js | 44 +++++++++++-------- .../user-settings/typescript/src/index.ts | 44 +++++++++++-------- 2 files changed, 50 insertions(+), 38 deletions(-) diff --git a/examples/user-settings/javascript/index.js b/examples/user-settings/javascript/index.js index 053986e..7b604b2 100644 --- a/examples/user-settings/javascript/index.js +++ b/examples/user-settings/javascript/index.js @@ -66,24 +66,30 @@ const interval = new Interval({ }), ]); - let confirmed = false; - if (resetPassword || user.email !== email) { - const messages = []; - const helpTexts = []; - if (resetPassword) { - messages.push("reset this user's password"); - helpTexts.push('log the user out all current sessions'); - } - if (user.email !== email) { - messages.push('change their email'); - helpTexts.push('send an email to verifiy the new email address'); + const messages = []; + const helpTexts = []; + if (resetPassword) { + messages.push('reset their password'); + helpTexts.push('log the user out all current sessions'); + } + if (user.email !== email) { + messages.push('change their email'); + helpTexts.push('send an email to verifiy the new email address'); + } + const confirmed = await io.confirm( + `Are you sure you want to ${messages + .join(', ') + .replace(/,(?!.*,)/gim, messages.length > 2 ? ', and' : ' and')}?`, + { + helpText: + helpTexts.length > 0 + ? `This will ${helpTexts.join(' and ')}.` + : undefined, } - confirmed = await io.confirm( - `Are you sure you want to ${messages.join(' and ')}?`, - { - helpText: `This will ${helpTexts.join(' and ')}.`, - } - ); + ); + + if (!confirmed) { + return 'No confirmation, did not update the user'; } const updatedUser = { @@ -94,8 +100,8 @@ const interval = new Interval({ }; users[users.indexOf(user)] = updatedUser; - if (confirmed && resetPassword) resetUserPassword(updatedUser); - if (confirmed && user.email !== email) sendVerificationEmail(updatedUser); + resetUserPassword(updatedUser); + sendVerificationEmail(updatedUser); return updatedUser; }, diff --git a/examples/user-settings/typescript/src/index.ts b/examples/user-settings/typescript/src/index.ts index e667c97..1de70cc 100644 --- a/examples/user-settings/typescript/src/index.ts +++ b/examples/user-settings/typescript/src/index.ts @@ -66,24 +66,30 @@ const interval = new Interval({ }), ]); - let confirmed = false; - if (resetPassword || user.email !== email) { - const messages = []; - const helpTexts = []; - if (resetPassword) { - messages.push("reset this user's password"); - helpTexts.push('log the user out all current sessions'); - } - if (user.email !== email) { - messages.push('change their email'); - helpTexts.push('send an email to verifiy the new email address'); + const messages = ['update the user']; + const helpTexts = []; + if (resetPassword) { + messages.push('reset their password'); + helpTexts.push('log the user out all current sessions'); + } + if (user.email !== email) { + messages.push('change their email'); + helpTexts.push('send an email to verifiy the new email address'); + } + const confirmed = await io.confirm( + `Are you sure you want to ${messages + .join(', ') + .replace(/,(?!.*,)/gim, messages.length > 2 ? ', and' : ' and')}?`, + { + helpText: + helpTexts.length > 0 + ? `This will ${helpTexts.join(' and ')}.` + : undefined, } - confirmed = await io.confirm( - `Are you sure you want to ${messages.join(' and ')}?`, - { - helpText: `This will ${helpTexts.join(' and ')}.`, - } - ); + ); + + if (!confirmed) { + return 'No confirmation, did not update the user'; } const updatedUser = { @@ -94,8 +100,8 @@ const interval = new Interval({ }; users[users.indexOf(user)] = updatedUser; - if (confirmed && resetPassword) resetUserPassword(updatedUser); - if (confirmed && user.email !== email) sendVerificationEmail(updatedUser); + resetUserPassword(updatedUser); + sendVerificationEmail(updatedUser); return updatedUser; }, From b46ee0005be3b34015f4cf72bbe56c27ee54361d Mon Sep 17 00:00:00 2001 From: Ryan Coppolo Date: Tue, 13 Sep 2022 15:39:29 -0400 Subject: [PATCH 2/4] Still check for need to reset pass/verify email --- examples/user-settings/javascript/index.js | 4 ++-- examples/user-settings/typescript/src/index.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/user-settings/javascript/index.js b/examples/user-settings/javascript/index.js index 7b604b2..3d68938 100644 --- a/examples/user-settings/javascript/index.js +++ b/examples/user-settings/javascript/index.js @@ -100,8 +100,8 @@ const interval = new Interval({ }; users[users.indexOf(user)] = updatedUser; - resetUserPassword(updatedUser); - sendVerificationEmail(updatedUser); + if (resetPassword) resetUserPassword(updatedUser); + if (user.email !== email) sendVerificationEmail(updatedUser); return updatedUser; }, diff --git a/examples/user-settings/typescript/src/index.ts b/examples/user-settings/typescript/src/index.ts index 1de70cc..a9b97db 100644 --- a/examples/user-settings/typescript/src/index.ts +++ b/examples/user-settings/typescript/src/index.ts @@ -100,8 +100,8 @@ const interval = new Interval({ }; users[users.indexOf(user)] = updatedUser; - resetUserPassword(updatedUser); - sendVerificationEmail(updatedUser); + if (resetPassword) resetUserPassword(updatedUser); + if (user.email !== email) sendVerificationEmail(updatedUser); return updatedUser; }, From 9723013a5d6b1c68d44fd89a1e476a03a40f7269 Mon Sep 17 00:00:00 2001 From: Ryan Coppolo Date: Wed, 14 Sep 2022 09:30:37 -0400 Subject: [PATCH 3/4] Simplify logic further to avoid regex --- examples/user-settings/javascript/index.js | 25 ++++--------------- .../user-settings/typescript/src/index.ts | 23 +++-------------- 2 files changed, 9 insertions(+), 39 deletions(-) diff --git a/examples/user-settings/javascript/index.js b/examples/user-settings/javascript/index.js index 3d68938..0c1eb38 100644 --- a/examples/user-settings/javascript/index.js +++ b/examples/user-settings/javascript/index.js @@ -13,11 +13,6 @@ const users = [...Array(10)].map((_, i) => { }; }); -function sendVerificationEmail(user) { - // replace with a call to your email service - ctx.log('Sending verification email to', user.email); -} - function resetUserPassword(user) { // replace with database update and a call to send a password reset email ctx.log('Resetting password for', user.email); @@ -66,25 +61,16 @@ const interval = new Interval({ }), ]); - const messages = []; - const helpTexts = []; + const messages = ['update the user']; + let helpText; if (resetPassword) { messages.push('reset their password'); - helpTexts.push('log the user out all current sessions'); - } - if (user.email !== email) { - messages.push('change their email'); - helpTexts.push('send an email to verifiy the new email address'); + helpText = 'This will log the user out all current sessions.'; } const confirmed = await io.confirm( - `Are you sure you want to ${messages - .join(', ') - .replace(/,(?!.*,)/gim, messages.length > 2 ? ', and' : ' and')}?`, + `Are you sure you want to ${messages.join(', ')}?`, { - helpText: - helpTexts.length > 0 - ? `This will ${helpTexts.join(' and ')}.` - : undefined, + helpText, } ); @@ -101,7 +87,6 @@ const interval = new Interval({ users[users.indexOf(user)] = updatedUser; if (resetPassword) resetUserPassword(updatedUser); - if (user.email !== email) sendVerificationEmail(updatedUser); return updatedUser; }, diff --git a/examples/user-settings/typescript/src/index.ts b/examples/user-settings/typescript/src/index.ts index a9b97db..11f9b0e 100644 --- a/examples/user-settings/typescript/src/index.ts +++ b/examples/user-settings/typescript/src/index.ts @@ -13,11 +13,6 @@ const users = [...Array(10)].map((_, i) => { }; }); -function sendVerificationEmail(user) { - // replace with a call to your email service - ctx.log('Sending verification email to', user.email); -} - function resetUserPassword(user) { // replace with database update and a call to send a password reset email ctx.log('Resetting password for', user.email); @@ -67,24 +62,15 @@ const interval = new Interval({ ]); const messages = ['update the user']; - const helpTexts = []; + let helpText; if (resetPassword) { messages.push('reset their password'); - helpTexts.push('log the user out all current sessions'); - } - if (user.email !== email) { - messages.push('change their email'); - helpTexts.push('send an email to verifiy the new email address'); + helpText = 'This will log the user out all current sessions'; } const confirmed = await io.confirm( - `Are you sure you want to ${messages - .join(', ') - .replace(/,(?!.*,)/gim, messages.length > 2 ? ', and' : ' and')}?`, + `Are you sure you want to ${messages.join(' and ')}?`, { - helpText: - helpTexts.length > 0 - ? `This will ${helpTexts.join(' and ')}.` - : undefined, + helpText, } ); @@ -101,7 +87,6 @@ const interval = new Interval({ users[users.indexOf(user)] = updatedUser; if (resetPassword) resetUserPassword(updatedUser); - if (user.email !== email) sendVerificationEmail(updatedUser); return updatedUser; }, From 05ca18eaf979474c84a0a0abe4105802ba291dc5 Mon Sep 17 00:00:00 2001 From: Jacob Mischka Date: Wed, 14 Sep 2022 09:31:30 -0500 Subject: [PATCH 4/4] Update sdk-js/examples/user-settings/javascript/index.js --- examples/user-settings/javascript/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/user-settings/javascript/index.js b/examples/user-settings/javascript/index.js index 0c1eb38..da19b60 100644 --- a/examples/user-settings/javascript/index.js +++ b/examples/user-settings/javascript/index.js @@ -68,7 +68,7 @@ const interval = new Interval({ helpText = 'This will log the user out all current sessions.'; } const confirmed = await io.confirm( - `Are you sure you want to ${messages.join(', ')}?`, + `Are you sure you want to ${messages.join(' and ')}?`, { helpText, }