Every time I'm working on an SPA with an API backend I always find myself wondering the best way to do this. The current example I'm running into is as follows:
When a User
has their account created for them (by an admin user), they are sent a welcome email. I want the ability to re-send this welcome email, but only if the user has not already clicked the link in the email and set their password. So user has a confirmedAt
field, so obviously the email can't be sent if this field is set.
So my problem is, on the controller (nodejs), I would need to do a check that says:
if (!user.confirmedAt) {
user.sendEmail()
}
But then on the frontend (react) I would have to conditionally show the button to re-send the email
{!user.confirmedAt ? <SendWelcomeEmailButton /> : null}
Previously I've solved this by defining permissions so that the ability of send-welcome-email
can only be done if the user
does not have confirmedAt
, and these permission rules are shared on frontend and backend.
I'm not a fan of this solution as it puts application logic into permissions, when I think they should remain simple (basic create, read, update, delete, etc.). Now this example isn't so bad, but I have had much more complex logic come up.
What is your preferred way of dealing with this?