From 3fe3deaceffdca3efe667bfad783f263a0ff2153 Mon Sep 17 00:00:00 2001 From: helipilot50 Date: Sun, 15 Oct 2023 21:25:47 +0200 Subject: [PATCH 1/2] weir context and auth --- backend/dsp-service/src/clerk.ts | 2 +- backend/dsp-service/src/context.ts | 38 ++++++++----------- .../src/resolvers/accountsResolvers.ts | 11 ++---- backend/dsp-service/src/server.ts | 17 ++++++--- 4 files changed, 32 insertions(+), 36 deletions(-) diff --git a/backend/dsp-service/src/clerk.ts b/backend/dsp-service/src/clerk.ts index d3d19ff..66c03ce 100644 --- a/backend/dsp-service/src/clerk.ts +++ b/backend/dsp-service/src/clerk.ts @@ -27,7 +27,7 @@ export async function userByToken(token: string): Promise { } export async function userById(id: string): Promise { - const user = await clerkClient.users.getUser(id as string); + const user = await clerkClient.users.getUser(id); return user; } diff --git a/backend/dsp-service/src/context.ts b/backend/dsp-service/src/context.ts index 84f0537..4cd4fe1 100644 --- a/backend/dsp-service/src/context.ts +++ b/backend/dsp-service/src/context.ts @@ -3,7 +3,7 @@ import { User } from "@clerk/clerk-sdk-node"; import { PrismaClient } from "@prisma/client"; import { PubSub } from "graphql-subscriptions"; import { logger, Logger } from "./logger"; -import { userByToken } from "./clerk"; +import { userById } from "./clerk"; import { ExpressContextFunctionArgument } from "@apollo/server/dist/esm/express4"; export const prisma = new PrismaClient(); @@ -19,39 +19,33 @@ export interface DspContext { }; -export async function makeRequestContext({ req, res }: ExpressContextFunctionArgument): Promise { - +export async function makeRequestContext({ req, res }: any): Promise { let newContext: DspContext = { prisma, pubsub, logger: logger, }; - try { - - const headers = req.headers; - - if (headers && headers.authorization) { - - logger.info('[server.makeRequestContext] has authoriztion header'); - logger.debug(`[server.makeRequestContext] req.headers.authorization ${JSON.stringify(headers.authorization, undefined, 2)}`); - const token = headers.authorization.split(' ')[1]; - const userProfile: User = await userByToken(token); - logger.debug(`[server.makeRequestContext] userProfile ${JSON.stringify(userProfile, undefined, 2)}`); - newContext = { - ...newContext, - user: userProfile, - token: token, - }; - - } + console.log(`[server.makeRequestContext] req ${JSON.stringify(req, undefined, 2)}`); + + // if (req.auth && req.auth.userId) { + // const auth = req.auth; + // logger.debug(`[server.makeRequestContext] req.auth ${JSON.stringify(auth, undefined, 2)}`); + // newContext = { + // ...newContext, + // user: await userById(auth.userId), + // token: auth.getToken(), + // }; + + // } } catch (error) { logger.error(`[server.makeRequestContext] 💀 Error creating context ${JSON.stringify(error, undefined, 2)}`); } + console.log(`[server.makeRequestContext] newContext ${JSON.stringify(newContext, undefined, 2)}`); return newContext; } -export async function makeWebSocketContext(args: ExpressContextFunctionArgument): Promise { +export function makeWebSocketContext(args: any): DspContext { let newContext: DspContext = { prisma, diff --git a/backend/dsp-service/src/resolvers/accountsResolvers.ts b/backend/dsp-service/src/resolvers/accountsResolvers.ts index bc09ec3..0515e0c 100644 --- a/backend/dsp-service/src/resolvers/accountsResolvers.ts +++ b/backend/dsp-service/src/resolvers/accountsResolvers.ts @@ -1,10 +1,7 @@ import { GraphQLResolveInfo } from "graphql"; import { QueryAccountsArgs, QueryAccountArgs, MutationNewAccountArgs, MutationMapAccountRetailersArgs } from "../resolver-types"; import { withFilter } from "graphql-subscriptions"; -import { DspContext, pubsub } from "../context"; - -// TODO: add logger to context for sunscription -import { logger } from "../logger"; +import { DspContext } from "../context"; const ACCOUNT_CREATED = 'ACCOUNT_CREATED'; @@ -132,12 +129,12 @@ export const accountResolvers/*: Resolvers*/ = { accountCreated: { subscribe: withFilter( (_: any, varibles: any, context: DspContext, info: any) => { - context.logger.info(`[accountsResolvers.accountCreated] subscribe ${varibles}`); + context.logger.debug(`[accountsResolvers.accountCreated] subscribe ${JSON.stringify(varibles, null, 2)}`); return context.pubsub.asyncIterator(ACCOUNT_CREATED); }, (payload, variables, context: DspContext, info: any) => { - context.logger.debug(`accountCreated variables ${variables}`); - context.logger.info(`accountCreated payload ${payload}`); + context.logger.debug(`accountCreated variables ${JSON.stringify(variables, null, 2)}`); + context.logger.debug(`accountCreated payload ${JSON.stringify(payload, null, 2)}`); return payload; } ) diff --git a/backend/dsp-service/src/server.ts b/backend/dsp-service/src/server.ts index 945ad5d..ece15cd 100644 --- a/backend/dsp-service/src/server.ts +++ b/backend/dsp-service/src/server.ts @@ -26,6 +26,7 @@ import { resolvers } from './resolvers'; import { DspContext, makeRequestContext, pubsub, prisma, makeWebSocketContext } from './context'; import { corsOptions } from './cors'; +import { ClerkExpressWithAuth, LooseAuthProp, WithAuthProp } from '@clerk/clerk-sdk-node'; const PORT = (process.env.PORT) ? Number.parseInt(process.env.PORT) : 4000; @@ -86,7 +87,7 @@ async function listen() { // WebSocketServer start listening. const serverCleanup = useServer({ schema: graphSchema, - context: async (args: ExpressContextFunctionArgument) => { + context: async (args: any) => { return makeWebSocketContext(args); }, onConnect: async (ctx: any) => { @@ -166,11 +167,15 @@ async function listen() { '/graphql', cors(corsOptions), json(), - expressMiddleware(server, { - context: async (args: ExpressContextFunctionArgument) => { - return makeRequestContext(args); - }, - }), + // ClerkExpressWithAuth(), + expressMiddleware(server, + { + context: async (args: any) => { + console.log(`[server] expressMiddleware context ${JSON.stringify(args, undefined, 2)}`); + return await makeRequestContext(args); + }, + } + ), ); From b6c9c7b392d98de1d014f7f6f2f7f3ddf95e58ef Mon Sep 17 00:00:00 2001 From: "p.milne" Date: Wed, 18 Oct 2023 07:55:33 -0500 Subject: [PATCH 2/2] Fixed weirt but with logging the context --- backend/dsp-service/src/context.ts | 22 +++++++++------------- backend/dsp-service/src/server.ts | 6 +++--- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/backend/dsp-service/src/context.ts b/backend/dsp-service/src/context.ts index 4cd4fe1..4604583 100644 --- a/backend/dsp-service/src/context.ts +++ b/backend/dsp-service/src/context.ts @@ -26,22 +26,18 @@ export async function makeRequestContext({ req, res }: any): Promise logger: logger, }; try { - console.log(`[server.makeRequestContext] req ${JSON.stringify(req, undefined, 2)}`); - - // if (req.auth && req.auth.userId) { - // const auth = req.auth; - // logger.debug(`[server.makeRequestContext] req.auth ${JSON.stringify(auth, undefined, 2)}`); - // newContext = { - // ...newContext, - // user: await userById(auth.userId), - // token: auth.getToken(), - // }; - - // } + + if (req.auth && req.auth.userId) { + const auth = req.auth; + newContext = { + ...newContext, + user: await userById(auth.userId), + token: auth.getToken(), + }; + } } catch (error) { logger.error(`[server.makeRequestContext] 💀 Error creating context ${JSON.stringify(error, undefined, 2)}`); } - console.log(`[server.makeRequestContext] newContext ${JSON.stringify(newContext, undefined, 2)}`); return newContext; } diff --git a/backend/dsp-service/src/server.ts b/backend/dsp-service/src/server.ts index ece15cd..e20a988 100644 --- a/backend/dsp-service/src/server.ts +++ b/backend/dsp-service/src/server.ts @@ -1,6 +1,6 @@ import { ApolloServer } from '@apollo/server'; -import { ExpressContextFunctionArgument, expressMiddleware } from '@apollo/server/express4'; +import { expressMiddleware } from '@apollo/server/express4'; import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer'; import { ApolloServerPluginLandingPageLocalDefault } from '@apollo/server/plugin/landingPage/default'; import { createServer } from 'http'; @@ -167,11 +167,11 @@ async function listen() { '/graphql', cors(corsOptions), json(), - // ClerkExpressWithAuth(), + ClerkExpressWithAuth(), expressMiddleware(server, { context: async (args: any) => { - console.log(`[server] expressMiddleware context ${JSON.stringify(args, undefined, 2)}`); + // console.log(`[server] expressMiddleware context ${JSON.stringify(args, undefined, 2)}`); return await makeRequestContext(args); }, }