Authentication issues with API.

I’m trying to create a route via a Shopify app using Remix, but I’m facing an authentication issue. When I create a route with load, the authenticate.admin(request) doesn’t work because the request doesn’t include the access token. In a regular route, it brings the user’s session. Is there a way to authenticate with Shopify when creating an API route in Remix app? How can I achieve this?

Hi @wandersoncorrea

To address the authentication issue in a Remix app, follow these steps:

  1. Ensure Session Middleware: Verify your session middleware is set up to handle the user’s session and access token.

  2. Manually Include Access Token: Add the access token to your request manually if it’s not included automatically. Example:

import { authenticate } from 'shopify-auth';
import { getSession } from './session';

export const loader = async ({ request }) => {
  const session = await getSession(request.headers.get('Cookie'));
  const accessToken = session.get('accessToken');

  if (!accessToken) {
    throw new Response('Unauthorized', { status: 401 });
  }

  const authResponse = await authenticate.admin({ token: accessToken });

  if (!authResponse) {
    throw new Response('Authentication failed', { status: 403 });
  }

  return { authResponse };
};
  • API Route Authentication: For API routes, ensure the access token is included in your server-side logic:
import { authenticate } from 'shopify-auth';
import { getSession } from './session';

export async function apiHandler(request) {
  const session = await getSession(request.headers.get('Cookie'));
  const accessToken = session.get('accessToken');

  if (!accessToken) {
    return new Response('Unauthorized', { status: 401 });
  }

  const authResponse = await authenticate.admin({ token: accessToken });

  if (!authResponse) {
    return new Response('Authentication failed', { status: 403 });
  }

  // Proceed with your API logic here
}
​

These steps should help resolve your authentication issue.