如何解决如何使用graphql将json网络令牌设置为cookies?
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 举报,一经查实,本站将立刻删除。