{"library":"simple-koa-shopify-auth","title":"Koa Shopify Authentication Middleware","description":"simple-koa-shopify-auth is a Koa middleware library designed to simplify Shopify app authentication, serving as a successor to the now-deprecated `@shopify/koa-shopify-auth`. It specifically supports `@shopify/shopify-api` version 5.x.x, integrating features like token exchange for online sessions and removing cookie-based session management to reduce redirects. The package is currently at version 3.0.0, with patch updates for performance and bug fixes, but the project is officially considered deprecated by its maintainer due to ongoing improvements in Shopify's native authentication flows that will render such a library unnecessary. It differentiates itself by its explicit support for `@shopify/shopify-api` v5 and its streamlined session handling, but it is not affiliated with Shopify directly. There are no plans to support `@shopify/shopify-api` v6 or newer versions, making it suitable only for applications locked into the v5 API.","language":"javascript","status":"deprecated","last_verified":"Wed Apr 22","install":{"commands":["npm install simple-koa-shopify-auth"],"cli":null},"imports":["import { createShopifyAuth } from 'simple-koa-shopify-auth';","import { verifyRequest } from 'simple-koa-shopify-auth';","import type { AuthOptions } from 'simple-koa-shopify-auth';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import Koa from 'koa';\nimport Router from '@koa/router';\nimport dotenv from 'dotenv';\nimport { createShopifyAuth, verifyRequest } from 'simple-koa-shopify-auth';\nimport '@shopify/shopify-api/adapters/node'; // Must be imported before initializing Shopify API\nimport { shopifyApi, LATEST_API_VERSION } from '@shopify/shopify-api';\n\ndotenv.config();\n\nconst app = new Koa();\nconst router = new Router();\n\nconst { SHOPIFY_API_KEY, SHOPIFY_API_SECRET, SCOPES, HOST } = process.env;\n\nif (!SHOPIFY_API_KEY || !SHOPIFY_API_SECRET || !SCOPES || !HOST) {\n  throw new Error('Missing Shopify API environment variables. Please check your .env file.');\n}\n\nconst shopify = shopifyApi({\n  apiKey: SHOPIFY_API_KEY,\n  apiSecretKey: SHOPIFY_API_SECRET,\n  scopes: SCOPES.split(','),\n  hostName: HOST.replace(/https?:\\/\\//, ''),\n  apiVersion: LATEST_API_VERSION,\n  is</div>Online: true // crucial for online sessions with simple-koa-shopify-auth\n});\n\n// Register authentication routes\nrouter.get('/auth', createShopifyAuth({\n  async afterAuth(ctx) {\n    const { shop, accessToken } = ctx.state.shopify;\n    console.log(`Authenticated shop: ${shop} with access token: ${accessToken}`);\n    // Redirect to your app's main page or dashboard\n    ctx.redirect(`https://${shop}/admin/apps/${shopify.config.apiKey}`);\n  }\n}));\n\n// Middleware to verify requests for authenticated routes\nconst verifyPageRequest = verifyRequest();\nconst verifyApiRequest = verifyRequest({ returnHeader: true });\n\n// Example protected route for app pages\nrouter.get('/', verifyPageRequest, async (ctx) => {\n  ctx.body = 'Welcome to your Shopify App!';\n});\n\n// Example protected route for API endpoints\nrouter.get('/api/data', verifyApiRequest, async (ctx) => {\n  const { shop, accessToken } = ctx.state.shopify;\n  ctx.body = { message: `Data for ${shop}`, token: accessToken };\n});\n\napp.use(shopify.validateAuthenticatedSession()); // Necessary for session management with shopify-api v5\napp.use(router.routes()).use(router.allowedMethods());\n\nconst PORT = process.env.PORT || 3000;\napp.listen(PORT, () => {\n  console.log(`Server running on port ${PORT}`);\n  console.log(`Shopify API Key: ${SHOPIFY_API_KEY}`);\n});","lang":"typescript","description":"This quickstart demonstrates setting up a basic Koa server with `simple-koa-shopify-auth` for Shopify app authentication. It includes registering auth routes and using `verifyRequest` middleware for protecting app pages and API endpoints, showcasing both `createShopifyAuth` and `verifyRequest` with environment variable configuration for Shopify API credentials.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}