Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@dooman87/koa-shopify-auth

Shopify7MIT1.0.0TypeScript support: included

readme

@shopify/koa-shopify-auth

Build Status License: MIT npm version

Middleware to authenticate a Koa application with Shopify.

Sister module to @shopify/shopify-express, but simplified.

Features you might know from the express module like the webhook middleware and proxy will be presented as their own packages instead.

Warning: 3.1.61-3.1.62 vulnerable to reflected XSS

Versions 3.1.61 and 3.1.62 are vulnerable to a reflected XSS attack. Please update to the latest version to protect your app.

Installation

$ yarn add @shopify/koa-shopify-auth

Usage

This package exposes shopifyAuth by default, and verifyRequest as a named export.

import shopifyAuth, {verifyRequest} from '@shopify/koa-shopify-auth';

shopifyAuth

Returns an authentication middleware taking up (by default) the routes /auth and /auth/callback.

app.use(
  shopifyAuth({
    // if specified, mounts the routes off of the given path
    // eg. /shopify/auth, /shopify/auth/callback
    // defaults to ''
    prefix: '/shopify',
    // your shopify app api key
    apiKey: SHOPIFY_API_KEY,
    // your shopify app secret
    secret: SHOPIFY_SECRET,
    // scopes to request on the merchants store
    scopes: ['write_orders, write_products'],
    // set access mode, default is 'online'
    accessMode: 'offline',
    // callback for when auth is completed
    afterAuth(ctx) {
      const {shop, accessToken} = ctx.session;

      console.log('We did it!', accessToken);

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

/auth

This route starts the oauth process. It expects a ?shop parameter and will error out if one is not present. To install it in a store just go to /auth?shop=myStoreSubdomain.

/auth/callback

You should never have to manually go here. This route is purely for shopify to send data back during the oauth process.

verifyRequest

Returns a middleware to verify requests before letting them further in the chain.

app.use(
  verifyRequest({
    // path to redirect to if verification fails
    // defaults to '/auth'
    authRoute: '/foo/auth',
    // path to redirect to if verification fails and there is no shop on the query
    // defaults to '/auth'
    fallbackRoute: '/install',
  }),
);

Example app

import 'isomorphic-fetch';

import Koa from 'koa';
import session from 'koa-session';
import shopifyAuth, {verifyRequest} from '@shopify/koa-shopify-auth';

const {SHOPIFY_API_KEY, SHOPIFY_SECRET} = process.env;

const app = new Koa();
app.keys = [SHOPIFY_SECRET];

app
  // sets up secure session data on each request
  .use(session({secure: true, sameSite: 'none'}, app))

  // sets up shopify auth
  .use(
    shopifyAuth({
      apiKey: SHOPIFY_API_KEY,
      secret: SHOPIFY_SECRET,
      scopes: ['write_orders, write_products'],
      afterAuth(ctx) {
        const {shop, accessToken} = ctx.session;

        console.log('We did it!', accessToken);

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

  // everything after this point will require authentication
  .use(verifyRequest())

  // application code
  .use(ctx => {
    ctx.body = '🎉';
  });

Gotchas

Fetch

This app uses fetch to make requests against shopify, and expects you to have it polyfilled. The example app code above includes a call to import it.

Session

Though you can use shopifyAuth without a session middleware configured, verifyRequest expects you to have one. If you don't want to use one and have some other solution to persist your credentials, you'll need to build your own verifiction function.

Testing locally

By default this app requires that you use a myshopify.com host in the shop parameter. You can modify this to test against a local/staging environment via the myShopifyDomain option to shopifyAuth (e.g. myshopify.io).

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and adheres to Semantic Versioning.

  • Include prefix when redirect to the root endpoint

[3.1.63] - 2020-05-25

Fixed

  • Protect against reflected XSS vulnerability 1455

[3.1.62] - 2015-05-20

Fixed

  • Include prefix when we redirect to the /auth path.

[3.1.61] - 2020-05-01

  • Fixes ITP 2.3 and Safari 13.1 enable cookies loop 1413

[3.1.56] - 2020-02-03

  • Package now forces cookies.secure to be true 1255
  • Package sets cookies to samesite:none and secure 1251

[3.1.54] - 2020-01-24

  • Updated redirect script to use App Bridge 1242

[3.1.37] - 2019-09-23

Fixed

  • No longer errors out on fresh installs with no session 1022

[3.1.36] - 2019-08-30

Fixed

  • Package no longer allows sessions from one shop to bleed over into another 940

[3.1.32] - 2019-08-15

Fixed

  • Package now lists missing '@shopify/network' dependency 862

[3.1.31] - 2019-08-13

Fixed

  • Installation no longer fails if accessToken is invalid #844

[3.1.14] - 2019-02-05

Fixed

  • OAuth route no longer rejects uppercase shop domains #493

[3.1.11] - 2019-01-10

Fixed

  • HMAC validation no longer breaks when params are unsorted #451

[3.1.10] - 2019-01-09

  • Start of Changelog