type VerifiedContext = Required<ApolloContext>;
function authenticateUser(context: ApolloContext): context is VerifiedContext {
if (!context.user) {
throw new AuthenticationError("Login required for this operation");
}
return true;
}
Here is an example of using the authentication middleware:
async fetchUser(_, { id }, context) {
authenticateUser(context);
const user = context.user;
// Expected behavior from Typescript to determine that user is defined and not null
},
Is there a way to assist Typescript in correctly inferring the type? For example, to indicate that if the code runs after the middleware, the user is considered "verified"?
The main objective is to avoid the need for conditional checks when implementing the authentication middleware.