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..4604583 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,31 +19,21 @@ 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)}`); + if (req.auth && req.auth.userId) { + const auth = req.auth; newContext = { ...newContext, - user: userProfile, - token: token, + user: await userById(auth.userId), + token: auth.getToken(), }; - } } catch (error) { logger.error(`[server.makeRequestContext] 💀 Error creating context ${JSON.stringify(error, undefined, 2)}`); @@ -51,7 +41,7 @@ export async function makeRequestContext({ req, res }: ExpressContextFunctionArg 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..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'; @@ -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); + }, + } + ), );