NuxtN
Nuxt2y ago
4 replies
Flo

Composable for accessing event scoped data?

Given following server middleware:
export default eventHandler(async (event) => {
  // No protection for auth routes
  if (event.path.startsWith("/api/auth")) {
    return;
  }

  const session: Session | null = await getServerSession(event);

  if (session) {
    // To be used throughout the app
    event.context.currentUser = {
      name: session.user?.name,
      email: session.user?.email,
      role: useRoles().getHighestRole(session.roles!),
      uid: session.uid,
      key: session.key,
    } as currentUser;
  }
  ...
  return
}


and an api handler calling a composable useSomeCustomComposable(), is there a way to access the currentUser-object in that composable without passing it through function arguments?

Above implementation was an experiment. Happy to change that.

Background: That custom composable is used in many routes. For the sake of code dedup, I'd prefer to not extend all calls with something that never changes. I'm propably just blind and it's an easy thing to do, but neither my brain, nor chatgpt were really helpful with that so far.
Was this page helpful?