Skip to content

Commit

Permalink
Merge pull request #815 from interval/fix-user-settings-logic
Browse files Browse the repository at this point in the history
Simplify user settings example confirm logic
  • Loading branch information
jacobmischka authored Sep 14, 2022
2 parents b963332 + 05ca18e commit c6b6c6f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 48 deletions.
39 changes: 15 additions & 24 deletions examples/user-settings/javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -66,24 +61,21 @@ 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'];
let helpText;
if (resetPassword) {
messages.push('reset their password');
helpText = 'This will log the user out all current sessions.';
}
const confirmed = await io.confirm(
`Are you sure you want to ${messages.join(' and ')}?`,
{
helpText,
}
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 = {
Expand All @@ -94,8 +86,7 @@ const interval = new Interval({
};
users[users.indexOf(user)] = updatedUser;

if (confirmed && resetPassword) resetUserPassword(updatedUser);
if (confirmed && user.email !== email) sendVerificationEmail(updatedUser);
if (resetPassword) resetUserPassword(updatedUser);

return updatedUser;
},
Expand Down
39 changes: 15 additions & 24 deletions examples/user-settings/typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -66,24 +61,21 @@ 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'];
let helpText;
if (resetPassword) {
messages.push('reset their password');
helpText = 'This will log the user out all current sessions';
}
const confirmed = await io.confirm(
`Are you sure you want to ${messages.join(' and ')}?`,
{
helpText,
}
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 = {
Expand All @@ -94,8 +86,7 @@ const interval = new Interval({
};
users[users.indexOf(user)] = updatedUser;

if (confirmed && resetPassword) resetUserPassword(updatedUser);
if (confirmed && user.email !== email) sendVerificationEmail(updatedUser);
if (resetPassword) resetUserPassword(updatedUser);

return updatedUser;
},
Expand Down

0 comments on commit c6b6c6f

Please sign in to comment.