blitz icon indicating copy to clipboard operation
blitz copied to clipboard

BlitzAuth plugin onAuth hook

Open Zeko369 opened this issue 2 years ago • 3 comments

What do you want and why?

It would be amazing to add a onAuth function callback to BlitzAuth plugin for setting the user in i.e. sentry context on the server

Possible implementation(s)

AuthServerPlugin({
  // ...
  onAuth: (ctx: AuthenticatedMiddlewareCtx) => {
    Sentry.setUser({ id: ctx.session.userId });
  },
}),

and then in blitz-auth/src/server/auth-plugin.ts

const blitzSessionMiddleware: RequestMiddleware<
  IncomingMessage,
  ServerResponse & {blitzCtx: Ctx}
> = async (req, res, next) => {
  console.log("Starting sessionMiddleware...")
  if (!res.blitzCtx?.session) {
    const session = await getSession(req, res)
    options.onAuth?(session);
  }
  return next()
}

we currently have it implemented as a patch-package for the blitz (old blitz) that adds Sentry.setUser inside of the authorize resolver function, but this seams like a better option

Additional context

Maybe a better name than onAuth, i.e. onAuthMiddlware / afterAuth?

Zeko369 avatar Jun 26 '22 01:06 Zeko369

That's a great idea and that's something that we were thinking about already. The initial idea was to have "onSessionCreate", "onSessionDestroy" hooks. What do you think about this naming?

beerose avatar Jun 29 '22 11:06 beerose

That might be useful for tracking/setting up some resources, but besides that I'd also add a onSessionVerify? / onAuthMiddlware or something similar to be called on every call specifically for logging / sentry / ...

Zeko369 avatar Jul 04 '22 20:07 Zeko369

Hmm, thinking about this again brings me to reconsider, since for error tracking usecase you only want to setup sentry context when user get's authenticated.

So I'd maybe add a onAuth hook to $authenticate method / authenticate resolver pipe? (this is how it's currently in out codebase)

image

Zeko369 avatar Jul 07 '22 22:07 Zeko369