我用Express Session和Redis创建了一个简单的服务器。Redis服务器正在运行(当我键入“redis-cli ping”时,我收到了“PONG”),我声明了名称空间并导出了接口SessionData,以允许我在请求会话时存储userID。(使用一个基本的索引.d.ts)。但是,当我发出登录请求时,变量userID存储在req.session中,但由于cookie没有设置到浏览器,它会立即被忘记/擦除。似乎每个请求都会产生一个新的会话,cookie永远不会保存。
应用程序、Redis和会话Cookie设置:
// ...
const app = express();
const RedisStore = connectRedis(session);
const redis = new Redis();
app.use(
session({
name: 'testcookie',
store: new RedisStore({
client: redis,
disableTouch: true,
port: 6379,
host: 'localhost',
}),
cookie: {
maxAge: 36000000 * 24 * 365,
httpOnly: true,
sameSite: 'lax',
secure: false,
},
saveUninitialized: false,
secret: 'secret',
resave: false,
})
);
// ...
登录突变:
@Mutation(() => UserResponse)
async login(
@Arg("usernameOrEmail") usernameOrEmail: string,
@Arg("password") password: string,
@Ctx() { req }: MyContext
): Promise<UserResponse> {
// gets user via inputs (everything works here)
// ...
req.session.userID = user.id;
// userID is set to be a number, as is user.id
// logging req.session.userID works perfectly if done right here
return { user };
}
查询以检查是否已登录:
@Query(() => User, { nullable: true })
async me(@Ctx() { req }: MyContext): Promise<User | undefined> {
// logging req.session.userID shows undefined
return (req.session.userID)
? await User.findOne({ id: req.session.userID })
: undefined;
}
更新(解决方案):通过进入GraphQL的设置并将“request.credentials”属性更改为“include”,解决了此问题。
2条答案
按热度按时间8yoxcaq71#
我正在关注2022年6月发布的Fullstack React GraphQL TypeScript Tutorial。自2021年以来,Apollo的GraphQL Playground已不复存在-取而代之的是Apollo studio沙箱(
https://studio.apollographql.com/sandbox/explorer
)在新的阿波罗工作室中,我找不到将
request.credentials
设置为include
的方法。遵循这些线索后:
https://community.apollographql.com/t/allow-cookies-to-be-sent-alongside-request/920
和
https://github.com/apollographql/apollo-server/issues/5775
我找到了一个不太好的解决方案,但它符合我的需要。
基本上,在我的设置中,它看起来像会话。cookie参数'sameSite'和'secure'需要不同的值,这取决于你是否希望你的前端添加cookie和apollo studio添加cookie。这不是理想的-但我找不到任何其他参数组合,为这两个工作。到目前为止,我只找到互斥的设置。
在我的服务器的索引.ts上
当我想从前端localhost:3000设置cookie时,我使用此会话设置
如果我想从apollostudio设置cookie和会话userId,我实际上会更改会话设置
当我想从后端localhost:4000/graphql设置cookie时,我使用这个会话设置
请评论,如果你知道一种方法,使用相同的设置,以允许来自前端和后端的cookie。
对于那些正在学习教程并希望了解更多详细信息的人,这里是设置的其他重要部分。
这是我的整个index.ts文件从后端。注意-我是8.5小时的教程-所以不要担心,如果你不认识一些部分。
这是我的数据源(新的方式使typeORM连接!)
在createUrqlClient.tsx前端文件中,我添加了
这里是apollo studio中所需设置的快照。要打开这些设置,请点击SANDBOX输入中左上角的设置/齿轮图标。
请确保将“x-forwarded-proto”“https”添加到共享头中。
7vux5j2d2#
答案形式@NicoWheat是部分正确的(我猜,纠正我,如果我错了).它工作时,我发送请求与apollo工作室(与sameSite:“无”和安全:true),但无论选项是什么,当我通过localhost:3000进行变异时,cookie仍然没有为我设置。
编辑:我错了,在frontend目录中按照create urql client中的选项操作后,它对我起作用了,非常感谢和欣赏。