<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/stop-querying-your-auth-provider-on-every-edge-request-put-the-role-in-the-jwt--wfltrq7xh" -->

---
title: Stop querying your auth provider on every edge request....
description: Checking user roles by calling an auth provider's API on every edge request adds a network round-trip that defeats the purpose of running on the edge. Instead,...
canonical: https://daily.dev/posts/stop-querying-your-auth-provider-on-every-edge-request-put-the-role-in-the-jwt--wfltrq7xh
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Stop querying your auth provider on every edge request. Put the role in the JWT. | daily.dev
og:description: Checking user roles by calling an auth provider's API on every edge request adds a network round-trip that defeats the purpose of running on the edge. Instead,...
og:url: https://daily.dev/posts/stop-querying-your-auth-provider-on-every-edge-request-put-the-role-in-the-jwt--wfltrq7xh
og:image: https://api.daily.dev/og/posts/wFlTRQ7Xh.png
og:image:alt: Stop querying your auth provider on every edge request. Put the role in the JWT.
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Stop querying your auth provider on every edge request. Put the role in the JWT.

**[Anthony](https://daily.dev/sources/m24lqqchx4cycladbihj7)** · [@drakeze](https://daily.dev/drakeze) · 3 min read · 5 upvotes · 0 comments

## Summary

Checking user roles by calling an auth provider's API on every edge request adds a network round-trip that defeats the purpose of running on the edge. Instead, Clerk can embed a user's role from publicMetadata directly into JWT session claims at token issue time, configured via the Clerk CLI, letting a Cloudflare Workers app read the verified role straight off the token with zero database or API calls. Two caveats: claims are only as fresh as the token (role changes don't retroactively update issued tokens, so revoking access needs a real-time check), and misconfigured or wiped metadata can lock everyone out, including admins, so a recovery path independent of the permission system is needed. A structural tip is to wrap the auth SDK in an internal package with separate client/server export paths to keep server config out of browser bundles.

## Content

If you're running role-based access control on Cloudflare Workers or any edge runtime and checking roles by calling your auth provider's API, you're paying a network round-trip on every protected request and giving back most of the reason you deployed to the edge.

You usually don't have to. The role can travel inside the session token you're already receiving.

**The setup**

Five Next.js apps in a monorepo, deployed to Cloudflare Workers via OpenNext, using Clerk. Two roles: `admin` and `owner`.

The naive version looks up the user's role when they hit a protected route. Correct, and slow: an API call in the hot path of every request, at the edge, where the whole point was not doing that.

**The fix: custom session claims**

Clerk can copy a user's `publicMetadata` into the JWT claims at token issue time. Configure it once per instance with their CLI:

bash

```
npx clerk@latest config patch \
  --app app_xxxxxxxxxxxxx \
  --instance prod \
  --json '{"session":{"claims":{"metadata":{"role":"{{user.public_metadata.role}}"}}}}'
```

Substitute your own app ID, and run it separately for production and development. Configuring one and forgetting the other produces a confusing afternoon where auth works in exactly one environment.

Then read it off the verified claim:

typescript

```
export async function resolveAdminRole(): Promise<AdminRole | null> {
  const { sessionClaims } = await auth();

  const role = sessionClaims?.metadata?.role;
  if (role === 'owner' || role === 'admin') {
    return role;
  }

  return null;
}
```

Zero database queries. Zero API calls. The token was already verified as part of the request, and the answer was already in it.

**Two things to know before you do this**

**Claims are only as fresh as the token. **** Changing someone's role in your provider's dashboard doesn't retroactively change tokens already issued. There's a propagation window until the session refreshes. For promoting an admin, fine. For revoking access from someone you need locked out _now_, that path needs a real-time check, and it's worth being deliberate about which of your permission changes are urgent.

**You can lock yourself out.** Once roles live in provider metadata, wiping that metadata, misconfiguring it, or standing up a fresh dev instance without the claims configured resolves every role to null, including yours. The tool you'd use to fix the permissions is behind the permissions.

Build a recovery path that doesn't depend on the system it's recovering. Keep your specifics to yourself; a bootstrap is a bypass by definition, and publishing exactly where it keys is doing an attacker's scoping work.

**Structural note**

Wrap your provider's SDK in one internal package rather than importing it across apps. Use `package.json` export paths to separate client components from server-only helpers; server config then can't be pulled into a browser bundle, because the import path doesn't exist on the client side.

I'm publishing this whole build chronologically, including the parts where I was wrong:

[** My Blog **](https://blog.drakeze.com/)

## Similar posts on daily.dev

- [Give every teammate and agent the right level of access to your Workers](https://daily.dev/posts/give-every-teammate-and-agent-the-right-level-of-access-to-your-workers-l5txbftfg) · Cloudflare · 0 upvotes · 0 comments
- [Signed, Sealed, Delivered](https://daily.dev/posts/signed-sealed-delivered-1n0myntek) · Medium · 0 upvotes · 0 comments
- [Temporal JWT token authentication](https://daily.dev/posts/temporal-jwt-token-authentication-iguczsdep) · Temporal · 0 upvotes · 0 comments
- [Identity as an Architectural Concern: Enterprise IAM Beyond Authentication](https://daily.dev/posts/identity-as-an-architectural-concern-enterprise-iam-beyond-authentication-bqugsbqbl) · InfoQ · 0 upvotes · 0 comments

---

Tags: [#authentication](https://daily.dev/tags/authentication), [#nextjs](https://daily.dev/tags/nextjs), [#cloudflare](https://daily.dev/tags/cloudflare), [#jwt](https://daily.dev/tags/jwt)

[View this post on daily.dev](https://daily.dev/posts/stop-querying-your-auth-provider-on-every-edge-request-put-the-role-in-the-jwt--wfltrq7xh)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"DiscussionForumPosting","mainEntityOfPage":"https://daily.dev/posts/stop-querying-your-auth-provider-on-every-edge-request-put-the-role-in-the-jwt--wfltrq7xh","headline":"Stop querying your auth provider on every edge request. Put the role in the JWT.","text":"Checking user roles by calling an auth provider's API on every edge request adds a network round-trip that defeats the purpose of running on the edge. Instead, Clerk can embed a user's role from publicMetadata directly into JWT session claims at token issue time, configured via the Clerk CLI, letting a Cloudflare Workers app read the verified role straight off the token with zero database or API calls. Two caveats: claims are only as fresh as the token (role changes don't retroactively update issued tokens, so revoking access needs a real-time check), and misconfigured or wiped metadata can lock everyone out, including admins, so a recovery path independent of the permission system is needed. A structural tip is to wrap the auth SDK in an internal package with separate client/server export paths to keep server config out of browser bundles.","url":"https://daily.dev/posts/stop-querying-your-auth-provider-on-every-edge-request-put-the-role-in-the-jwt--wfltrq7xh","datePublished":"2026-09-14T20:04:22.804Z","dateModified":"2026-09-14T20:04:45.575Z","author":{"@type":"Person","name":"Anthony","url":"https://daily.dev/drakeze","image":"https://media.daily.dev/image/upload/s--11gwAjuC--/f_auto/v1765561095/avatars/avatar_m24lqqCHX4cycLADBIHJ7?_a=BAMAK+ZW0","description":"Full-stack dev | Founder of SorenLab | Building in public | Future polyglot engineer","interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"EndorseAction"},"userInteractionCount":4180}},"image":"https://media.daily.dev/image/upload/s--apU8wrCD--/f_auto/v1789416263/posts/wFlTRQ7Xh?_a=BAMAMicg0","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":5},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"isPartOf":{"@type":"WebPage","url":"https://daily.dev/sources/m24lqqchx4cycladbihj7","name":"Anthony"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Anthony","item":"https://daily.dev/sources/m24lqqchx4cycladbihj7"},{"@type":"ListItem","position":3,"name":"Stop querying your auth provider on every edge request. Put the role in the JWT."}]}
```

