setup middleware on backend with oidc

I have three services:
  1. expo app (client)
  2. hono + better auth (oidc provider)
  3. hono app (server)
I have done auth in expo with expo-auth-session
The issue I am facing right now i how i add the middleware in the hono app?

my current hono middleware:

// Middleware to log requests and extract user info from bearer token
app.use("*", async (c, next) => {
  console.log(`${c.req.method} ${c.req.url}`);
  const authorization_header = c.req.raw.headers.get("Authorization");
  const bearer_token = authorization_header?.split(" ")[1] || "";
  console.log("Bearer token:", bearer_token);
  const response = await fetch(
    "https://uth.vercel.app/api/auth/oauth2/userinfo",
    {
      headers: {
        // add the authorization header from the request to the fetch request
        Authorization: `Bearer ${bearer_token}`,
      },
    }
  );
  const user = (await response.json()) as {
    sub: string;
    name: string;
    give_name: string;
  } | null;
  if (!user) {
    c.set("user", null);
    return next();
  }
  c.set("user", user);
  return next();
});


I dont want to do a fetch everytime.. plus i tried using the auth.api.getSession() but seems like getsession doesnt works with session created via the oidc provider
Was this page helpful?