如何使用graphql将json网络令牌设置为cookies?

问题描述

我有一个这个 graphql 解析器函数

async login(root,{ email,password },{ req,res }) {
            const user = await models.User.findOne({ where: { email } });

            if (!user) {
                throw new Error('No user with that email');
            }

            const valid = await bcrypt.compare(password,user.password);
            console.log(password,user.password,"check password",valid);

            if (!valid) {
                throw new Error('Incorrect password');
            }

            const accesstoken = sign({ userId: user.id },'secret',{
                expiresIn: "15min"
            });

            res.cookie("access-token",accesstoken,{ httpOnly: true });

            return user;
        },

在我看来,如果有匹配项,它应该从数据库返回一个用户,然后我想将会话令牌设置到 cookie 中。 第一步有效,我有关于用户的信息,但第二步没有...... 当我进入 google devtools 的网络选项卡时,我在请求信息中看到一个包含访问令牌的 cookie 部分。 Network panel 但我想知道为什么没有在应用程序面板中设置 cookie 我以这种方式设置我的服务器:

const express = require('express');
const { ApolloServer} = require('apollo-server-express');
const cors = require('cors');
const cookieParser = require('cookie-parser');

const typeDefs = require('./schema');
const resolvers = require('./resolvers');
const models = require('./models');
const { NONE } = require('sequelize');

const startServer = async () => {
    
    const server = new ApolloServer({
        typeDefs,resolvers,context: ({ req,res }) => ({ req,res })
    });

const app = express();


app.use(cors({origin:'http://localhost:8080',credentials: true}));


app.use(cookieParser());




server.applyMiddleware({ app });
models.sequelize.authenticate();

models.sequelize.sync();


app.listen({ port: 4000 },() =>
    console.log(`? Server ready at http://localhost:4000${server.graphqlPath}`)
);}

startServer();

最后,在我的 React 应用程序中,我的 index.js 文件中有这个

const link = createHttpLink({
  uri: 'http://localhost:4000/graphql',credentials: 'same-origin'
});


const client = new ApolloClient({
  cache: new InMemoryCache(),link,});

作为 graphql 的新手,我真的不知道我该怎么做,如果有人能给我一个线索,那就太好了。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)