npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

shopify-express-auth

v1.0.1-alpha.12

Published

Middleware to authenticate express app with Shopify

Downloads

5

Readme

shopify-express-auth

License: MIT npm version

Middleware to authenticate an Express application with Shopify. Works in sync with @shopify/shopify-api version 2.0.0

Why do I need this? What does this middleware do?

  • Auth Generation - This middleware creates the required oauth and session required to run your Shopify app, in case the current oauth/session is expired it creates a new one.
  • Request Validation - This middleware validates all the API calls you make from your client-side to your express backend and secures your backend from malicious activity

Prerequisite

Make sure you have already installed @shopify/shopify-api version 2.0.0 . In case you have not installed it yet. Use the following command to install it

npm

npm install @shopify/[email protected]

yarn

yarn add @shopify/[email protected]

Initialize @shopify/shopify-api

import Shopify, {ApiVersion} from '@shopify/shopify-api';

Shopify.Context.initialize({
  API_KEY: 'YOUR_APP_API_KEY',
  API_SECRET_KEY: 'YOUR_APP_API_SECRET_KEY',
  SCOPES: ['APP_SCOPES'],
  HOST_NAME: ('YOUR_HOST').replace(/https:\/\//, ""),
  API_VERSION: ApiVersion.July21,
  IS_EMBEDDED_APP: true,
  SESSION_STORAGE: new Shopify.Session.MemorySessionStorage(),
});

Note: Make sure you replace

SESSION_STORAGE: new Shopify.Session.MemorySessionStorage()

by

SESSION_STORAGE: new Shopify.Session.CustomSessionStorage(
    'YOUR_CUSTOM_STORAGE'.storeCallback,
    'YOUR_CUSTOM_STORAGE'.loadCallback,
    'YOUR_CUSTOM_STORAGE'.deleteCallback)

in production environment once you have set up your Custom Session storage. MemorySessionStorage() should work fine in development environment.

Installation

Once you have satisfied all the prerequisite conditions, install this package via:

npm

npm install shopify-express-auth

yarn

yarn add shopify-express-auth

Usage

Authenticate your app

Add authentication function of this middleware to your express app as a middleware. This function handles creating/re-creating oauth and session.

Sample

const express = require("express");
const app = express();
...
...
...
app.use(
  shopifyAuth({
    // Default path prefix is '' i.e  the paths become /auth and /auth/callback
    // Incase your want to modify the base path due to some reason you can modify it by specifying a prefix value - such as /shopify
    // When /shopify prefix is added the paths will become /shopify/auth and /shopify/auth/callback
    prefix: '',
    // set access mode. Allowed values are 'online' and 'offline'. Note: online tokens expire every 24 hours and you will need to re-validate
    // if want the developed hassle free - keep the accessMode as 'offline'
    accessMode: 'offline',
    // Callback for when the auth procedure is complete. 
    afterAuth(req, res) {
      const { shop, accessToken } = req.shopify;

      console.log('The access token is: ', accessToken);

      res.redirect('/');
    },
  }),
);

Access the session information on res.shopify inside the afterAuth callback. Store the access token for future use - such as: for creating webhooks, retrieving order, customer information etc.

shopifyAuth params

| Param | Options | Description | | :---: | :-: | :-: | | prefix | empty string/some custom route prefix string | If you want to add a customp prefix in the auth routes. | | accessMode | offline/online | Corresponds to which access mode you want to use | | afterAuth() | - | A required callback function passed to shopifyAuth. After auth is successful, this callback function will be invoked and will contain the session information |

Note: Online tokens expire every 24 hours and you will need to re-validate.

Validate your API calls

Use this middleware to authenticate all API calls you make from your app's frontend to your app's express backend. This middleware will check if a valid and active session token has been generated by your express app and your frontend has sent a valid & active session token along with the request or not.

Make sure you are using @shopify/app-bridge v2.0.5 on the client side.

Sample

const express = require("express");
const router = express.Router();
const UserController = require("../controllers/user.controller");
const shopifyExpressAuth = require("shopify-express-auth");

router.post(
  "/create",
  shopifyExpressAuth.verifyRequest({ accessMode: "offline", returnHeader: false }),
  UserController.create
);

If the session/session token is not valid then the user will be redirected to the shopify oauth route (/auth) in order to generate a valid session.

verifyRequest params

| Param | Options | Description | | :---: | :-: | :-: | | accessMode | offline/online | Checks for offline or online session | | returnHeader | false/true | Returns re-auth headers on auth failure |