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

now-shopify-auth

v1.0.6

Published

Authenticate your Next.js app and ZEIT Now serverless functions with Shopify.

Downloads

11

Readme

now-shopify-auth

License: MIT

Authenticate your Next.js app and ZEIT Now serverless functions with Shopify.

Installation

$ npm install -S now-shopify-auth

Usage

verifyRequest

in pages/_app.tsx

import App, { AppContext, AppProps } from "next/app";
import { parseCookies } from "nookies";
import { AppProvider } from "@shopify/polaris";
import { Loading, Provider } from "@shopify/app-bridge-react";
import { verifyRequest } from "now-shopify-auth";

const apiKey = "shopify api key";

class MyShopifyApp extends App<AppProps & { shopOrigin: string }> {
	static async getInitialProps({ Component, ctx }: AppContext) {
		const authRoute = "/api/shopify/auth";
        const fallbackRoute = "/login";
        const verifyTokenUrl = `https://myshopifyapp.com/api/shopify/verify-token`;
		const cookies = parseCookies(ctx);
		const shopOrigin = ctx.query.shop ?? cookies.shopOrigin;

		if (ctx.pathname !== fallbackRoute) {
            await verifyRequest({
                query: ctx.query,
                cookies,
                res: ctx.res,
                options: { authRoute, fallbackRoute, verifyTokenUrl },
            });
		}

		return {
			pageProps: {
				// Call page-level getInitialProps
				...(Component.getInitialProps ? await Component.getInitialProps(ctx) : {}),
			},
			shopOrigin,
		};
	}

	render() {
		const { Component, pageProps, shopOrigin } = this.props;

		if (!shopOrigin) {
			return (
                <AppProvider>
                    <Component {...pageProps} />
                </AppProvider>
			);
		}

		const forceRedirect = true;

		if (
			forceRedirect &&
			typeof window !== "undefined" &&
			window === window.parent
		) {
			window.location.href = `https://${shopOrigin}/admin/apps/${apiKey}${location.pathname}${location.search || ""}`;

			return <>Redirecting to Shopify</>;
		}

		const shopifyBridgeConfig: import("@shopify/app-bridge").AppConfig = {
			apiKey,
			shopOrigin,
			forceRedirect,
		};

		return (
            <AppProvider
                {...shopifyBridgeConfig}
            >
                <Provider config={shopifyBridgeConfig}>
                    <Loading />
                    <Component {...pageProps} />
                </Provider>
            </AppProvider>
		);
	}
}

in pages/api/hello-word.ts

import { NowRequest, NowResponse } from "@now/node";
import { verifyRequest } from "now-shopify-auth";

export default async function sendTestNotification(req: NowRequest, res: NowResponse): Promise<void> {
    const authRoute = "/api/shopify/auth";
    const fallbackRoute = "/login";
    const verifyTokenUrl = `https://myshopifyapp.com/api/shopify/verify-token`;

	await verifyRequest({ query: req.query, cookies: req.cookies, res, options: { authRoute, fallbackRoute, verifyTokenUrl } });
}

createShopifyAuth

Returns a set of helpers that you will need to use in 4 different routes explained below.

import createShopifyAuth, { OAuthStartOptions } from "now-shopify-auth";

const shopifyAuthOptions: OAuthStartOptions = {
    // your shopify app api key
	apiKey: "SHOPIFY_API_KEY",
    // your shopify app secret
	secret: "SHOPIFY_API_SECRET_KEY",
    // your app url
	appUrl: "https://myshopifyapp.com",
    // if specified, mounts the routes off of the given path
    // eg. /api/shopify/auth, /api/shopify/auth/callback
    // defaults to ""
	prefix: "/api/shopify",
    // scopes to request on the merchants store
	scopes: ["read_products", "read_orders"],
    // set access mode, default is "online"
    accessMode: "offline",
    // callback for when auth is completed
	async afterAuth({ shopOrigin, shopifyToken, res }) {
		console.log(`We're authenticated on shop ${shopOrigin}: ${shopifyToken}`);

        res.writeHead(302, { Location: `https://${shopOrigin}/admin/apps/${apiKey}` });
		res.end();
	},
};

Required routes

In a Next.js app, you will need to create these 5 routes:

  • /api/shopify/auth/callback
  • /api/shopify/auth/enable-cookies
  • /api/shopify/auth/index
  • /api/shopify/auth/inline
  • /api/shopify/verify-token

Theses files will need the following content for the authentication and verification processes to work as expected

/api/shopify/auth/callback

import { NowRequest, NowResponse } from "@now/node";
import createShopifyAuth from "now-shopify-auth";

import { shopifyAuthOptions } from "../../../config";

export default async function shopifyAuthCallback(req: NowRequest, res: NowResponse): Promise<void> {
	const { oAuthCallback } = createShopifyAuth(shopifyAuthOptions);

	await oAuthCallback(req, res);
}

/api/shopify/auth/enable-cookies

import { NowRequest, NowResponse } from "@now/node";
import createShopifyAuth from "now-shopify-auth";

import { shopifyAuthOptions } from "../../../config";

export default async function shopifyAuthEnableCookies(req: NowRequest, res: NowResponse): Promise<void> {
	const { enableCookies } = createShopifyAuth(shopifyAuthOptions);

	await enableCookies(req, res);
}

/api/shopify/auth/index

import { NowRequest, NowResponse } from "@now/node";
import createShopifyAuth, { hasCookieAccess, shouldPerformInlineOAuth } from "now-shopify-auth";

import { shopifyAuthOptions } from "../../../config";

export default async function shopifyAuthIndex(req: NowRequest, res: NowResponse): Promise<void> {
	const {
		enableCookiesRedirect,
		oAuthStart,
		topLevelOAuthRedirect,
	} = createShopifyAuth(shopifyAuthOptions);

	if (!hasCookieAccess(req.cookies)) {
		await enableCookiesRedirect(req, res);
		return;
	}

	if (shouldPerformInlineOAuth(req.cookies)) {
		await oAuthStart(req, res);
		return;
	}

	await topLevelOAuthRedirect(req, res);
}

/api/shopify/auth/inline

import { NowRequest, NowResponse } from "@now/node";
import createShopifyAuth from "now-shopify-auth";

import { shopifyAuthOptions } from "../../../config";

export default async function shopifyAuthInline(req: NowRequest, res: NowResponse): Promise<void> {
	const { oAuthStart } = createShopifyAuth(shopifyAuthOptions);

	await oAuthStart(req, res);
}

/api/shopify/verify-token

This is a simple proxy route to avoid CORS issues we would face by hitting Shopify API from a different domain

import { NowRequest, NowResponse } from "@now/node";
import fetch from "isomorphic-fetch";

export default async function verifyToken(req: NowRequest, res: NowResponse): Promise<void> {
	const { shopOrigin, shopifyToken } = req.cookies;

    const response = await fetch(
	    `https://${shopOrigin}/admin/metafields.json`,
        {
            method: "GET",
            headers: {
                "Content-Type": "application/json",
                "X-Shopify-Access-Token": shopifyToken,
            },
        },
    );

    res.status(response.status).end();
}

Gotchas

Make sure to install a fetch polyfill, since internally we use it to make HTTP requests.

In your terminal $ npm install -S isomorphic-fetch

In your app import "isomorphic-fetch"

OR

require("isomorphic-fetch")