---
title: "Authentication"
description: "Authenticate admin requests, handle OAuth, and manage sessions in @yan-ad/nuxt-shopify."
canonical_url: "https://shopify.yan.ad/guides/authentication"
---
# Authentication

> Authenticate admin requests, handle OAuth, and manage sessions in @yan-ad/nuxt-shopify.

## Admin Authentication

Use `useShopifyAdmin()` in your server API routes to authenticate requests from the Shopify admin. The returned `admin` object provides a **typed GraphQL** client powered by `@shopify/admin-api-client`:

```ts [server/api/products.ts]
export default defineEventHandler(async (event) => {
  const { admin, session } = await useShopifyAdmin(event)

  const { data } = await admin.graphql(`{
    products(first: 5) {
      edges {
        node {
          id
          title
        }
      }
    }
  }`)

  return data
})
```

### GraphQL with variables

```ts
const { data } = await admin.graphql(
  `#graphql
  mutation populateProduct($input: ProductInput!) {
    productCreate(input: $input) {
      product {
        id
        title
      }
    }
  }`,
  {
    variables: {
      input: { title: 'New Product' }
    }
  }
)
```

### Full `AdminContext` return type

<table>
<thead>
  <tr>
    <th>
      Property
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        session
      </code>
    </td>
    
    <td>
      <code>
        Session
      </code>
    </td>
    
    <td>
      The authenticated Shopify session
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        admin
      </code>
    </td>
    
    <td>
      <code>
        AdminApiContext
      </code>
    </td>
    
    <td>
      GraphQL API client
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        sessionToken
      </code>
    </td>
    
    <td>
      <code>
        JwtPayload | undefined
      </code>
    </td>
    
    <td>
      Decoded session token (embedded apps only)
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        billing
      </code>
    </td>
    
    <td>
      <code>
        BillingContext
      </code>
    </td>
    
    <td>
      Helpers for <code>
        require()
      </code>
      
      , <code>
        check()
      </code>
      
      , <code>
        request()
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        cors
      </code>
    </td>
    
    <td>
      <code>
        (response) => Response
      </code>
    </td>
    
    <td>
      Add CORS headers to a response
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        redirect
      </code>
    </td>
    
    <td>
      <code>
        (url, init?) => Response
      </code>
    </td>
    
    <td>
      Redirect helper that works inside embedded iframes
    </td>
  </tr>
</tbody>
</table>

## OAuth Routes

The module automatically registers these routes to handle the full OAuth flow:

<table>
<thead>
  <tr>
    <th>
      Route
    </th>
    
    <th>
      Purpose
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        GET /_shopify/auth
      </code>
    </td>
    
    <td>
      Start the OAuth flow
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        GET /_shopify/auth/callback
      </code>
    </td>
    
    <td>
      Handle the OAuth callback from Shopify
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        GET /_shopify/auth/exit-iframe
      </code>
    </td>
    
    <td>
      App Bridge iframe escape page
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        GET /_shopify/auth/session-token
      </code>
    </td>
    
    <td>
      Session token bounce page
    </td>
  </tr>
</tbody>
</table>

The prefix `/_shopify/auth` is configurable via the `authPathPrefix` option in `nuxt.config.ts`.

<tip>

You don't need to create these routes — the module registers them automatically. Just make sure your app's redirect URL is set to `{APP_URL}/_shopify/auth/callback` in the Partners Dashboard.

</tip>

## Other Authentication Types

### Shopify Flow

```ts [server/api/flow.ts]
export default defineEventHandler(async (event) => {
  const { session, admin, payload } = await useShopifyFlow(event)
  // Handle Flow trigger/action
})
```

### Public requests (checkout extensions, etc.)

```ts [server/api/public/widget.ts]
export default defineEventHandler(async (event) => {
  const { sessionToken, cors } = await useShopifyPublic(event)
  // sessionToken contains the decoded JWT payload
  // Use cors() to wrap your response with CORS headers
})
```

### POS extensions

```ts [server/api/pos/action.ts]
export default defineEventHandler(async (event) => {
  const context = await useShopifyPos(event)
  // Handle POS extension request
})
```

### Fulfillment service

```ts [server/api/fulfillment.ts]
export default defineEventHandler(async (event) => {
  const context = await useShopifyFulfillmentService(event)
  // Handle fulfillment service callback
})
```

### Unauthenticated access (background jobs)

For accessing the Shopify API without an incoming request (cron jobs, background tasks):

```ts [server/api/cron/sync.ts]
export default defineEventHandler(async () => {
  const { admin } = await useShopifyUnauthenticatedAdmin(
    'my-shop.myshopify.com'
  )

  const { data } = await admin.graphql(`{
    products(first: 10) { edges { node { id title } } }
  }`)

  return data
})
```

### Merchant login (non-embedded apps)

```ts [server/api/login.ts]
export default defineEventHandler(async (event) => {
  const { errors } = await useShopifyLogin(event)
  // Handle login for non-embedded app flows
})
```


## Sitemap

See the full [sitemap](https://shopify.yan.ad/sitemap.md) for all pages.
