All Projects → bluebeel → nextjs-shopify-auth

bluebeel / nextjs-shopify-auth

Licence: other
Authenticate your Next.js app with Shopify.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to nextjs-shopify-auth

Shopify-Serverless-Starter-App
Shopify Serverless Starter Application built on Serverless Framework and Polaris UI (React.js)
Stars: ✭ 56 (+3.7%)
Mutual labels:  shopify, shopify-app
koa-shopify-starter
A slightly modified version of https://github.com/Shopify/unite-react-node-app-workshop.
Stars: ✭ 14 (-74.07%)
Mutual labels:  shopify, shopify-app
Next Storefront
🛍️ A dazzlingly fast E-Commerce solution built with Typescript and NextJS.
Stars: ✭ 72 (+33.33%)
Mutual labels:  nextjs, shopify
shopify-node-express-app
Simple Shopify app with Express and Node.js that connects to a Shopify store via OAuth.
Stars: ✭ 20 (-62.96%)
Mutual labels:  shopify, shopify-app
mechanic-tasks
Public task repository for Mechanic (https://mechanic.dev)
Stars: ✭ 42 (-22.22%)
Mutual labels:  shopify, shopify-app
shopify-node-app-starter
🚀🚀 A Shopify embedded app starter template, written in TypeScript with session storage, app context and examples for basic functionalities.
Stars: ✭ 99 (+83.33%)
Mutual labels:  shopify, shopify-app
shopify-node-react-app
Shopify paid app in Node.js & React.js that connects to a store and lets merchants select products to automatically discount them in the Shopify admin interface.
Stars: ✭ 29 (-46.3%)
Mutual labels:  shopify, shopify-app
nextjs-shopify
This repository contains the app without webhook for the Build a Shopify app with Node and React tutorial.
Stars: ✭ 56 (+3.7%)
Mutual labels:  shopify, shopify-app
server-authentication-next.js
No description or website provided.
Stars: ✭ 103 (+90.74%)
Mutual labels:  nextjs
next-mdx
next-mdx provides a set of helper functions for fetching and rendering local MDX files. It handles relational data, supports custom components, is TypeScript ready and really fast.
Stars: ✭ 176 (+225.93%)
Mutual labels:  nextjs
react-keycloak-examples
Examples for react-keycloak packages
Stars: ✭ 110 (+103.7%)
Mutual labels:  nextjs
react-ant-mobile
react+ant-mobile+Next.js 脚手架
Stars: ✭ 22 (-59.26%)
Mutual labels:  nextjs
Adding-Storyblok-to-NextJS-like-a-Pro
Adding Headless CMS to NextJS like a Pro, this repository contains code examples and guide on how to integrate Storyblok, a headless CMS to NextJS.
Stars: ✭ 23 (-57.41%)
Mutual labels:  nextjs
portfolio
My personal portfolio website, proudly built with Next.js, TypeScript and Tailwind
Stars: ✭ 165 (+205.56%)
Mutual labels:  nextjs
Batteries-Included-Next.js
A starting boilerplate for a TS Next.js project with batteries included. Tailwind CSS for styling, Jest and React Testing Library working with path aliases and node-mock-http for API route testing.
Stars: ✭ 35 (-35.19%)
Mutual labels:  nextjs
shopify-development-resources
A List of resources for Shopify development
Stars: ✭ 56 (+3.7%)
Mutual labels:  shopify
shopify-product-image-slider
Implementation of the Slick image carousel into a Shopify store
Stars: ✭ 21 (-61.11%)
Mutual labels:  shopify
next-mdx-digital-garden-starter
An opinionated starting point for Digital Garden content authoring.
Stars: ✭ 50 (-7.41%)
Mutual labels:  nextjs
monorepo-typescript-next-the-sane-way
A monorepo example using TypeScript and Next.js
Stars: ✭ 104 (+92.59%)
Mutual labels:  nextjs
now-course
Proyecto para el curso de Now.sh en Platzi
Stars: ✭ 19 (-64.81%)
Mutual labels:  nextjs

@bluebeela/nextjs-shopify-auth

License: MIT

Authenticate your Next.js app with Shopify.

Sister module to @shopify/shopify-express and @shopify/koa-shopify-auth, but for nextjs.

Missing features

  • Registering webhook
  • Listening to webhook

A example app without the webhook for the Build a Shopify app with Node and React tutorial is available here

Installation

$ npm install @bluebeela/nextjs-shopify-auth

Usage

This package exposes createShopifyAuth by default, authenticateShopifyPage, authenticateShopifyAPI, and NextShopifyApiRequest, as a named export.

import createShopifyAuth, {authenticateShopifyPage, authenticateShopifyAPI, NextShopifyApiRequest} from '@bluebeela/nextjs-shopify-auth';

createShopifyAuth

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

const shopifyAuthOptions = {
  // your shopify app api key
  apiKey: process.env.SHOPIFY_API_KEY,
  // your shopify app secret
  secret: process.env.SHOPIFY_API_SECRET,
  // your app url
  appUrl: process.env.HOST,
  // 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", "write_products"],
  // set access mode, default is "online"
  accessMode: "online",
  // callback for when auth is completed
  async afterAuth({
    shopOrigin,
    shopifyToken,
    shopifyAssociatedUser,
    req,
    res,
  }) {
    console.log(
      `We're authenticated on shop ${shopOrigin}: ${shopifyToken} with user ${JSON.stringify(
        shopifyAssociatedUser
      )}`
    );
    res.writeHead(302, { Location: `/` });
    res.end();
  },
};

const shopify = createShopifyAuth(shopifyAuthOptions);

in pages/api/example-api-route-you-want-to-protect.js

Simply add the authenticateShopifyAPI middleware to your route to verify that the request is secure.

import { authenticateShopifyAPI } from "@bluebeela/nextjs-shopify-auth";

export default authenticateShopifyAPI(async function helloWorld(req, res) {
  return await res.json({ hello: "world" });
});

in pages/protected-page.js

Simply add the authenticateShopifyPage middleware to your route to verify that the request is secure.

// random page
import { authenticateShopifyPage } from "@bluebeela/nextjs-shopify-auth";

export const getServerSideProps = authenticateShopifyPage(async (ctx) => {
  return { props: { hello: "world"}}
});

Required API 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 shopify from "../../../../lib/shopify"; // lib is on the root dir

export default async function shopifyAuthCallback(req, res) {
	const { oAuthCallback } = shopify;

	await oAuthCallback(req, res);
}

/api/shopify/auth/enable_cookies

import shopify from "../../../../lib/shopify"; // lib is on the root dir

export default async function shopifyAuthEnableCookies(req, res) {
	const { enableCookies } = shopify;

	await enableCookies(req, res);
}

/api/shopify/auth/index

import {
  hasCookieAccess,
  shouldPerformInlineOAuth,
  grantedStorageAccess,
} from "@bluebeela/nextjs-shopify-auth";
import shopify from "../../../../lib/shopify"; // lib is on the root dir

export default async function shopifyAuthIndex(req, res) {
  const {
    enableCookiesRedirect,
    oAuthStart,
    topLevelOAuthRedirect,
    requestStorageAccess,
  } = shopify;

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

  if (!grantedStorageAccess(req.cookies)) {
    await requestStorageAccess(req, res);
    return;
  }

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

  await topLevelOAuthRedirect(req, res);
}

/api/shopify/auth/inline

import shopify from "../../../../lib/shopify"; // lib is on the root dir

export default async function shopifyAuthInline(req, res) {
  const { oAuthStart } = shopify;

  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

export default async function verifyToken(req, res) {
  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();
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].