vue-clerk icon indicating copy to clipboard operation
vue-clerk copied to clipboard

use of useAuth from #clerk in middleware

Open 5knnbdwm opened this issue 4 months ago • 5 comments

Hey, I basically want to protect most of my api with clerk, also dont really need access to the userObject so i just wanted to do the auth check in middleware.

However when doing so getAuth() returns undefined. Any idea as how to solve this?

// server/middleware/auth.ts
import { getAuth } from "#clerk";
// import { clerkClient, getAuth } from "#clerk";

export default defineEventHandler(async (event) => {
  if (event.node.req.url?.startsWith("/api")) {
    const publicRoutes = ["/api/public", "/api/another-public-route"];

    if (publicRoutes.includes(event.node.req.url || "")) {
      return;
    }

    console.log("getAuth", getAuth(event)); // returns `undefined`
    // const { userId } = getAuth(event);

    // if (!userId) {
    //   throw createError({
    //     statusCode: 401,
    //   });
    // }

    // event.context.userId = userId;
  }
});

5knnbdwm avatar Oct 11 '24 19:10 5knnbdwm