【发布时间】:2022-10-04 15:54:28
【问题描述】:
我正在尝试使用 express-graphql 中的上下文创建身份验证策略,但是,当我在isAuthenticated 中访问上下文时,它返回[Function: context]。我不明白什么?
app.use(
"/graphql",
graphqlHTTP(async (req: any) => ({
schema: schema,
graphiql: true,
context: (req: any) => {
const user = users.find((user) => user.username === "test user");
if (!user) {
return {
message: "Incorrect username or password.",
};
}
return {
user: "test user",
active: "Yes",
};
},
}))
);
const isAuthenticated =
() =>
(next: any) =>
async (root: any, args: any, context: any, info: any) => {
console.log("context", context);
if (!context.currentUser) {
throw new Error("You are not authorized");
}
return next(root, args, context, info);
};
【问题讨论】: