Trying to add authentication to my appolo projects. I have the following structure
Class AuthenticatedDataSource extends RemoteGraphQLDataSource {
willSendRequest({request, context }) {
request.http.headers.set('required-auth', context.requiredAuth);
request.http.headers.set('authenticated', context.authenticated);
request.http.headers.set('user-role', context.userRole);
}
}
const gateway = new ApolloGateway({
supergraphSdl,
debug: false,
buildService({
return new AuthenticatedDataSource({ url });
},
});
const server = new ApolloServer({
gateway,
subscriptions: false,
plugins: [ApolloServerPluginDrainHttpServer({
httpServer
})],
context: async({ req }) => {
const token = req.headers['authorization'] || '';
const domain = req.headers['domain'] || '';
console.log('req url: ', req.headers.originalUrl)
const user = await getUserRole(token, domain);
return { ...user };
},
});
The flow is the following, the client sends a domain and auth token. The server fetches a configuration, gets all require information about authentication and adds it to context
so all services will get the information about user role, and authentication status. But the problem is that, the call is taking to long and the server does multiple calls with empty domain and token. Log looks like this
Info
2022-07-25T23:35:00.054339915Zreq url: undefined
Info
2022-07-25T23:35:00.054390450ZFetching appConfigs for domain:
Info
2022-07-25T23:35:00.066468331Zreq url: undefined
Info
2022-07-25T23:35:00.066975920ZFetching appConfigs for domain:
can someone help me to understand what I am doing wrong?