middleware icon indicating copy to clipboard operation
middleware copied to clipboard

Clerk getAuth inside the app.post

Open lumberman opened this issue 1 year ago • 1 comments

Should something like this work?

app.post(
	"/strategies",
	async (c) => {
		const auth = getAuth(c);

I'm getting empty auth object

auth {
  sessionClaims: null,
  sessionId: null,
  session: null,
  userId: null,
  user: null,
  actor: null,
  orgId: null,
  orgRole: null,
  orgSlug: null,
  organization: null,
  getToken: [Function: getToken],
  debug: [Function (anonymous)]
}

Same user session returns properly filled auth object when running the code bellow with GET.

app.get(
	"/",
	async (c) => {
		const auth = getAuth(c);

Should getAuth work in POST block or I'm doing something wrong?

lumberman avatar Jan 05 '24 01:01 lumberman

I would check two things:

  • that the middleware is running for /
  • that the clerk SDK is loaded on the frontend
 const clerk = new Clerk(clerkPubKey);
    await clerk.load();

The clerk token expires after 60 seconds so you need the JS client present to refresh the token which then set the client side cookie required for auth on the backend

collinstommy avatar Apr 26 '24 01:04 collinstommy