The Three Things to Never Build In Your App: Authentication, Notifications, and Payments
Tags: buildvsbuy, javascript, programming
Published: 2021-05-18 00:30:07

Author: Tejas Kumthekar

Back in early 2018, I embarked on a side gig with a few partners - the idea was to make ridesharing socially engaging and fun. We made a ton of mistakes and never really got the product off the launchpad, however in retrospect, the biggest mistakes we made were wasting precious time in writing code for authentication and authorization as well as user notifications. We learned to focus on customer value the hard way. 

Every startup should consider the trade-offs of buying vs building non-differentiated features like authentication, notifications, payments etc. Companies like [Auth0](https://auth0.com/) (authentication and authorization platform), [Courier](https://www.courier.com/) (one API to design and deliver notifications across multiple channels), and [Stripe](https://stripe.com/) (payments infrastructure for the internet) have solved these problems so you, the entrepreneur or developer, can stay laser-focused on what your users truly want out of your product.

Whether you are working on a side gig like me or starting a company, shipping an MVP as quickly as possible helps you get feedback much earlier in the product development cycle. Outsourcing authentication, notifications, and payments tremendously helps in rolling your MVP out the door. The project I mentioned above took months to reach the MVP stage and outsourcing the non-core features could have had a great impact on shortening that time frame.

In this post, we shall focus on a pretty common use case that suits almost every product you will ever build. A user signs up for your product, you save the user details, and send them a welcome message. We will be using Auth0’s post-user registration hook and Courier’s automations feature to build this use case fairly quickly. Let’s dive right in!

With a single “send” API invocation, you can use Courier to reach your customers across one or more channels (email, text, push, or a direct message). We learned through talking to our customers that more often than not, they need something more powerful than just a single “send” action. They need workflows that do multiple actions with a single invocation, like making a single API call to send an email to their users and wait for a desired period of time (inducing a delay) and thereafter send a follow-up email. Another example of a workflow we've heard quite a lot is scheduled notifications, like sending product updates every week. [The Courier Automations API](https://help.courier.com/en/articles/5129279-how-to-build-and-trigger-multi-step-notification-automations-to-users) gives you the ability to build such workflows and quite literally “automate” your notifications. We will see it in action soon :)

##Configuring the Courier side of things

Let's start by creating a free account on Courier, which lets you send up to 10,000 free notifications every month. For the use case we are tackling in this blog post, we will be leveraging Courier’s Automations API. With a single API call, you’ll be able to create a new user profile inside Courier followed by a welcome message to send to this newly created user. The Automations Guide has a detailed walkthrough of all the cool features unlocked via Courier’s Automations API. Today, we will be invoking an automation that has two steps - update-profile and send.

###Step 1: update-profile

This step essentially takes any profile JSON object with a unique ID (called recipient ID) and saves it inside Courier. It stays as a permanent record inside Courier that you can further use to send notifications, add it to a list of users/profiles, customize preferences such as opt-in/opt-out, and so on. In our use case, this would originate from Auth0. Here’s how it would look:

```javascript
{
  "action": "update-profile",
  "recipient_id": "<RECIPIENT_ID>",
  "profile": {
     // User properties from Auth0
     // plus any more properties you want to attach to the user
  },
  "merge": "none"
}
```

*Save Profile Information as JSON Object in Courier*

<RECIPIENT_ID> =  A unique ID that you can use to identify your user.

###Step 2: send

This step sends a welcome email to the user. We will be sending users an email using Sendgrid as the downstream email provider. This [Setup Email using SendGrid](https://docs.courier.com/docs/getting-started-sendgrid) guide walks you through sending an email notification using Courier and SendGrid. Once you have created a notification template (with email as the channel using SendGrid), you can construct a send step that looks like this:

```javascript
{
  "action": "send",
  "profile": {
    "email": "foo@bar.com"
  },
  "recipient": "<RECIPIENT_ID>",
  "template": "<TEMPLATE_ID>"
}
```

*Construct a send Step*

<RECIPIENT_ID> = User ID you are sending the notification to (same as step 1)

<TEMPLATE_ID> = ID of the notification template you just created for welcoming the new user.

###Final Payload

Here’s how the payload to automations/invoke endpoint will look:

```javascript
{
    "automation":{
   	 "steps":[
   		 {
   			 "action":"update-profile",
   			 "recipient_id":"unique-foo",
   			 "profile":{
   				 "email":"foo@bar.com",
   				 "first_name":"Foo",
   				 "last_name":"Bar"
   			 },
   			 "merge":"none"
   		 },
   		 {
   			 "action":"send",
   			 "profile":{
   				 "email":"foo@bar.com"
   			 },
   			 "template":"AAZRHC4TDAMEHKGJHDAA9199WMVH",
   			 "recipient":"unique-foo"
   		 }
   	 ]
    }
}
```

*Payload to Automations*

##Configuring the Auth0 side of things

This post assumes you have a basic understanding of Auth0 and have set up an application in Auth0 before, but if not, follow the [Auth0 Getting Started](https://auth0.com/docs/get-started) guide. 

Let's take a step back and imagine how things would look if there was no integration between Auth0 and Courier. Every time you would have a new user signed up with Auth0, you would have to explicitly invoke Courier to create their profile and send them a welcome message. This would require writing more code in your application, deploying it reliably and maintaining it as your application scales. Having Auth0 and Courier integrated out of the box gives you great leverage in the form of speed and agility to move fast - without having to maintain a single line of code in your infrastructure.

We want Auth0 + Courier to work together automatically - like magic. Once a new user registers for the product, we want to trigger Courier’s Invoke Automation API - this is where [Auth0 Post-User Registration](https://auth0.com/docs/hooks/extensibility-points/post-user-registration) comes into picture. Here’s the code that lives behind Auth0 hook and uses the Courier Node.js SDK to invoke the automation dynamically based on the user obtained via Auth0 registration.

```javascript
module.exports = async function (user, context, cb) {
  const { CourierClient } = require("@trycourier/courier");
  const courier = CourierClient({ authorizationToken:               context.webtask.secrets.COURIER_AUTH_TOKEN });
  
  await courier.automations.invokeAdHocAutomation({
   automation: {
     steps: [
       {
         action: "update-profile",
         recipient_id: user.id,
         profile: {
           email: user.email,
           username: user.username,
           phoneNumber: user.phoneNumber
         },
         merge: "none"
       },
       {
         action: "send",
         profile:{
           email: user.email
         },
         template: "AAZRHC4TDAMEHKGJHDAA9199WMVH",
         recipient: user.id
       }
     ]
   }
 });
  cb();
};
```

*Invoke the Automation Dynamically*
 
This saves the user profile to Courier followed by sending the welcome email you configured in the template. 

The profiles can be viewed in Courier UI under Data Recipients tab:
![view-profiles-in-courier-UI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/puhb02ufezvn41f58y4k.png)

Next, the welcome email is sent to the user - it looks just like how you configured it to look in Courier. Just to give an example of how a notification could look:

![welcome-to-courier-email](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b1fcvbx825ijocskypj3.png)

##Feel the Magic Automation?
We just saw how Auth0 + Courier can help you automagically build user onboarding flow without having to maintain any code in your own system, and as we all know, the best code is no code that allows us to focus our energies where they are most needed.

Of course, Courier Integrates with more than just Auth0. Just like we sent an email using Sendgrid in this blog post, you can use any of these channels - email, SMS, push, direct messaging, etc. with providers like Mailgun, Twilio, and Slack with just a single API to power your notification workflows. Sign up for a free Courier account. You build the next big thing and let us handle the notifications for you. 🚀

###Additional References -

[Courier Docs](https://docs.courier.com/docs)
[Auth0 docs](https://auth0.com/docs/)
[Courier Node.js SDK](https://github.com/trycourier/courier-node)