Skip to content

Commit

Permalink
chore(server): shared IoC 4 - middleware cleanup (#3119)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabis94 authored Sep 26, 2024
1 parent b3f07fa commit 1c07c76
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
5 changes: 3 additions & 2 deletions packages/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import {
authContextMiddleware,
buildContext,
determineClientIpAddressMiddleware,
mixpanelTrackerHelperMiddleware
mixpanelTrackerHelperMiddlewareFactory
} from '@/modules/shared/middleware'
import { GraphQLError } from 'graphql'
import { redactSensitiveVariables } from '@/logging/loggingHelper'
Expand All @@ -64,6 +64,7 @@ import { statusCodePlugin } from '@/modules/core/graph/plugins/statusCode'
import { BaseError, ForbiddenError } from '@/modules/shared/errors'
import { loggingPlugin } from '@/modules/core/graph/plugins/logging'
import { shouldLogAsInfoLevel } from '@/logging/graphqlError'
import { getUser } from '@/modules/core/repositories/users'

const GRAPHQL_PATH = '/graphql'

Expand Down Expand Up @@ -384,7 +385,7 @@ export async function init() {
next()
}
)
if (enableMixpanel()) app.use(mixpanelTrackerHelperMiddleware)
if (enableMixpanel()) app.use(mixpanelTrackerHelperMiddlewareFactory({ getUser }))

// Initialize default modules, including rest api handlers
await ModulesSetup.init(app)
Expand Down
24 changes: 11 additions & 13 deletions packages/server/modules/shared/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
AuthParams,
authHasFailed
} from '@/modules/shared/authz'
import { Request, Response, NextFunction } from 'express'
import { Request, Response, NextFunction, Handler } from 'express'
import { ForbiddenError, UnauthorizedError } from '@/modules/shared/errors'
import { ensureError } from '@/modules/shared/helpers/errorHelper'
import { validateToken } from '@/modules/core/services/tokens'
Expand Down Expand Up @@ -188,18 +188,16 @@ export async function buildContext({
/**
* Adds a .mixpanel helper onto the req object that is already pre-identified with the active user's identity
*/
export async function mixpanelTrackerHelperMiddleware(
req: Request,
_res: Response,
next: NextFunction
) {
const ctx = req.context
const user = ctx.userId ? await getUser(ctx.userId) : null
const mp = mixpanel({ userEmail: user?.email, req })

req.mixpanel = mp
next()
}
export const mixpanelTrackerHelperMiddlewareFactory =
(deps: { getUser: typeof getUser }): Handler =>
async (req: Request, _res: Response, next: NextFunction) => {
const ctx = req.context
const user = ctx.userId ? await deps.getUser(ctx.userId) : null
const mp = mixpanel({ userEmail: user?.email, req })

req.mixpanel = mp
next()
}

const X_SPECKLE_CLIENT_IP_HEADER = 'x-speckle-client-ip'
/**
Expand Down

0 comments on commit 1c07c76

Please sign in to comment.