This is part two in a series of articles documenting the process of building our first Shopify app. Click here for part one.
If you’ve built a Shopify App before, you know all too well the time and effort needed to handle all of the authentication and installation procedures required for each app you want to create. It is a vital, but cumbersome process we all must endure before we get to the fun parts of actually building our app.
The good news? We can get this part of the app out of the way before we even know what app we’re building, and once we’re done, we’ll have a template we can reuse in the future when building our next app!
However, making changes to any part of this process will be difficult as it will need to happen in two places, and then three, then four, and the workload would keep scaling as our app portfolio grows.
This is where we decided to branch off a little at Shopside, and consider if there was an even better way to do this, while still keeping aligned with our core values.
Note to self: Yes, there is always a better way.
We dared to dream, and thought about our ideal method for handling Shopify’s installation and authentication requirements, which is not a small list by any means. It includes handling OAuth installation, session tokens, HMAC signatures, managing subscriptions/charges and more.
Our ideal solution, was a single service which could be used by an unlimited number of apps, and would handle all installation steps, as well as any verification/authentication requests that needed to happen and interact with our backend database.
Basically any process that would be repeated in every app, should be handled by this service if possible, so it only exists in one place, and the functionality can be abstracted to an external service, reducing the size and complexity of our apps moving forward.
We did a little research around the requirements and realised that we could indeed cover everything we need with an external service. We decided on our technology stack, and created the first version of our Shopify Authentication Service!
It is important that our Authentication Service can handle thousands of requests without breaking down, as every time a store loads our app, an authentication request will need to be sent to verify they have the app installed with an active subscription. This may also need to happen on every request on the front-end store (depending on the app specifics), so we need to be prepared for massive amounts of traffic.
For this reason, and because we were familiar with the technology, we decided to build the service as a Serverless NodeJS application, which allows us to setup and manage a stack of Lambda functions within AWS, and attach them to public URLs via API Gateway.
Each Lambda function runs as a separate serverless function, and will automatically scale the number of functions running simultaneously as demand increases or decreases. Perfect!
Using Serverless means that all of our AWS resources are managed via a .yml config file, saving us from the complicated UI that is AWS. Creating and deploying our service is a breeze:
We decided to use Supabase to host our PostgreSQL database, and to separate each app into their own database project. Although they would all share an identical shop table (to track installs and store access tokens etc.), we decided it would be better to keep each app as separate as possible.
Before we take a look at the Authentication Service, let’s first understand how the OAuth process works and what steps we need our service to perform.
Here is a helpful diagram outlining the flow of events:
And this is the explanation Shopify give in their documentation:
Their documentation has a lot more in-depth information on each step if you’re interested in learning more. For now, let’s assume we understand the process perfectly.
Now that we have all the pieces in place, let’s go through the structure and functionality so we can understand how everything connects. Time to get technical 😎
This is the current configuration for the service, which implements three lambda functions, auth , verify and confirm:
Let’s dive a little deeper and see what each handler is doing:
This handler function is quite simple, and serves as the entry point for our app. It uses a few basic classes we wrote for basic database and Shopify API interaction, as well as some config handling and lambda response normalising.
It will ensure:
After redirecting to the OAuth installation URL, the user will have to review the permission scopes being requested and authorize the installation of the app.
Once this is done, the user will be sent back to our Auth Service to handle the actual installation. This is where our second handler function comes in, and does most of the work!
There’s a decent amount of code here so let’s look at each step! We:
Phew! A lot of steps, but nothing we can’t handler! Now the user will be faced with a confirmation screen where they can approve the subscription and enter their payment details.
Once they have confirmed the payment, there is one final redirect back to our third handler.
And that’s all that this service needs to worry about! High five! 🙏
Now that all the annoying parts are out of the way, we have a fully reusable and scalable Authentication Service for our Shopify Apps!
It’s time to start planning and building the app itself!
…
…Any ideas?
Part three of this series is now available, covering the ideation of our app and how we defined its scope.