Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nestjs-shopify-auth

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nestjs-shopify-auth

Enable Shopify OAuth in NestJS. Wraps @shopify/shopify-api under the hood.

  • 2.1.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

nestjs-shopify-auth

Node.js CI

An OAuth setup for NestJS using Shopify's [shopify-node-api] package. Allows for online and offline auth using this module. Also adds a GraphQL proxy so you can use online tokens to proxy your GraphQL requests to Shopify, without exposing your Shopify Admin access token to the frontend.

Installation

Install package using NPM:

npm install @shopify/shopify-api nestjs-shopify-auth

or using Yarn:

yarn add @shopify/shopify-api nestjs-shopify-auth

Make sure you have your Shopify context initialized, as required in @shopify/shopify-api. You can use the following package to do this is a neat way in NestJS that I've developed as well:

npm install shopify-nestjs-api

See usage: https://github.com/tolgap/shopify-nestjs-api .

Usage

From any module, import the ShopifyAuthModule using registerOnlineAuthAsync and/or registerOfflineAuthAsync:

// app.module.ts
@Module({
  imports: [
    ShopifyAuthModule.registerOnlineAuthAsync({
      useFactory: () => ({
        basePath: 'user',
      }),
    }),
  ],
})
export class AppModule {}

You can provide an injectable that can handle the redirection or any other setup you want after an offline or online auth was successful:

// my-shopify-auth.handler.ts
@Injectable()
export class MyShopifyAuthHandler implements ShopifyAuthAfterHandler {
  async afterAuth(req: Request, res: Response, session: SessionInterface) {
    // implement your logic after a successful auth.
    // you can check `session.isOnline` to see if it was an online auth or offline auth.
  }
}

and provide and inject it to your ShopifyAuthModule:

// app.module.ts
import { MyShopifyAuthHandler } from './my-shopify-auth.handler';

@Module({
  imports: [
    ShopifyAuthModule.registerOnlineAuthAsync({
      useFactory: (afterAuthHandler: MyShopifyAuthHandler) => ({
        basePath: 'user',
        afterAuthHandler,
      }),
      provide: [MyShopifyAuthHandler]
      inject: [MyShopifyAuthHandler],
    }),
  ],
})
export class AppModule {}

You can also use useClass and useExisting to register the ShopifyAuthModule. You can even register both auth modes using the same Module:

// app.module.ts
import { MyShopifyAuthHandler } from './my-shopify-auth.handler';

@Module({
  imports: [
    ShopifyAuthModule.registerOnlineAuthAsync({
      useFactory: (afterAuthHandler: MyShopifyAuthHandler) => ({
        basePath: 'user',
        afterAuthHandler,
      }),
      provide: [MyShopifyAuthHandler]
      inject: [MyShopifyAuthHandler],
    }),
    ShopifyAuthModule.registerOfflineAuthAsync({
      useFactory: (afterAuthHandler: MyShopifyAuthHandler) => ({
        basePath: 'shop',
        afterAuthHandler,
      }),
      provide: [MyShopifyAuthHandler]
      inject: [MyShopifyAuthHandler],
    }),
  ],
})
export class AppModule {}

Now, if you want to install an App and store the offline access token in your DB, or Redis, or whatever storage you prefer, just visit /shop/auth?shop=<yourshopname>.myshopify.com. And if you want to create short-lived online access token, for instance, to only perform one-off requests to Shopify Admin GraphQL, you can visit /user/auth?shop=<yourshopname>.myshopify.com.

Authentication

When ShopifyAuthModule is setup, you can use @UseShopifyAuth() to require online or offline session in Controllers or specific routes. Example:

import { AccessMode } from '@shopify/shopify-api';
import { Controller, Get } from '@nestjs/common';

@UseShopifyAuth(AccessMode.Online)
@Controller()
export class AppController {
  @Get('online-route')
  hello() {
    return 'you are using online auth!';
  }

  @Get('offline-route')
  // Overriding the controller access mode:
  @UseShopifyAuth(AccessMode.Offline)
  offline() {
    return 'you are using offline auth!';
  }
}

GraphQL proxy

This module automatically attaches a GraphQL endpoint to /graphql if you register online auth. You will need valid online auth tokens to make use of it.

Keywords

FAQs

Package last updated on 28 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc