0
votes

I have a React front end created by following: https://shopify.dev/tutorials/build-a-shopify-app-with-node-and-react

It is a NextJs React app with a custom Koa server to serve the NextJs app. That Koa server uses the koa-ashopify-auth package to perform OAuth with Shopify and get an access token.

This is the auth middleware code:

    server.use(
      createShopifyAuth({
        apiKey: SHOPIFY_API_KEY as string,
        secret: SHOPIFY_API_SECRET_KEY as string,
        scopes: ['read_products'],
        afterAuth(ctx) {
          const { shop, accessToken } = ctx.session as IShopifyKoaSession

          >>> I would like to create a user on my own backend now and login the user.
          
           .... more stuff
        }
      })
    )

I have a Django backend that I will be setting up a REST endpoint to do auth with my backend (I would like to use JWT).

So I have performed OAuth with Shopify on client-side and have an access token

How do I use this client side access token and securely create a new user on my backend in a secure way?

I am thinking something like ->

    ...
    const { shop, accessToken } = ctx.session as IShopifyKoaSession
    await myApiService.createOrUpdateUser(shop, accessToken) // Calls my external backend

How should I go about doing this? This is basically an implicit oauth flow.

The above would not be secure unless there was a way to validate shop and access token on the backend.

1

1 Answers

0
votes

You could for instance use the next-auth module, which is OSS and has a nice api

https://next-auth.js.org/

Check out this example here. Btw all the other examples are solid, highly recommended going there every time you're not sure about an approach.

https://github.com/vercel/next.js/tree/canary/examples/with-next-auth

This particular tutorial seems relevant too (using next-auth) https://next-auth.js.org/tutorials/securing-pages-and-api-routes

In general you could take advange of server-side-rendered features and support for /pages/api routes to create backend routes that get the JWT tokens from the Django backend.