【问题标题】:Session Cookie Not Saving On Browser会话 Cookie 未保存在浏览器上
【发布时间】:2018-11-19 21:39:43
【问题描述】:

我正在使用此 repo 中的代码: prisma-cookies

一切似乎都正常,但是当我使用 graphql 游乐场测试服务器时,会话 cookie 没有保存在我的浏览器上。换句话说,我向服务器发送了一个登录突变,登录显然有效,但是浏览器上没有保存任何 cookie。

我在 NodeJS 上的 ExpressJS 上运行 GraphQL Yoga 服务器。

如果您需要我添加任何其他信息,请告诉我。
非常感谢您的帮助!

index.ts

import { GraphQLServer } from "graphql-yoga";
import * as session from "express-session";
import { Prisma } from "./generated/prisma";
import resolvers from "./resolvers";

const server = new GraphQLServer({
  typeDefs: "./src/schema.graphql",
  resolvers,
  context: req => ({
    ...req,
    db: new Prisma({
      endpoint: process.env.PRISMA_ENDPOINT, // the endpoint of the Prisma DB service (value is set in .env)
      secret: process.env.PRISMA_SECRET, // taken from database/prisma.yml (value is set in .env)
      debug: true // log all GraphQL queries & mutations
    })
  })
});

const SESSION_SECRET = "lsdfjlkjlkewaqra";

server.express.use(
  session({
    name: "qid",
    secret: SESSION_SECRET,
    resave: false,
    saveUninitialized: false,
    cookie: {
      httpOnly: true,
      secure: process.env.NODE_ENV === "production",
      maxAge: 1000 * 60 * 60 * 24 * 7 // 7 days
    }
  })
);

const cors = {
  credentials: true,
  origin: "http://localhost:3000"
};

server.start({ cors }, () =>
  console.log(`Server is running on http://localhost:4000`)
);

auth.ts

import * as bcrypt from "bcryptjs";
import * as jwt from "jsonwebtoken";
import { Context } from "../../utils";

export const auth = {
  async signup(parent, args, ctx: Context, info) {
    const password = await bcrypt.hash(args.password, 10);
    const user = await ctx.db.mutation.createUser({
      data: { ...args, password }
    });

    ctx.request.session.userId = user.id;

    return {
      user
    };
  },

  async login(parent, { email, password }, ctx: Context, info) {
    const user = await ctx.db.query.user({ where: { email } });
    if (!user) {
      throw new Error(`No such user found for email: ${email}`);
    }

    const valid = await bcrypt.compare(password, user.password);
    if (!valid) {
      throw new Error("Invalid password");
    }

    ctx.request.session.userId = user.id;

    return {
      user
    };
  }
};

util.ts

import * as jwt from "jsonwebtoken";
import { Prisma } from "./generated/prisma";

export interface Context {
  db: Prisma;
  request: any;
}

export function getUserId(ctx: Context) {
  if (ctx.request.session.userId) {
    return ctx.request.session.userId;
  }

  throw new AuthError();
}

export class AuthError extends Error {
  constructor() {
    super("Not authorized");
  }
}

【问题讨论】:

  • 你解决了吗?

标签: express graphql express-session


【解决方案1】:

因此,目前 graphql 游乐场实际上存在一个错误。在过去的两天里,我花了大约 12 个小时试图找出问题所在。问题是 Graphql Playground 没有随您的请求发送凭据。从理论上讲,您应该可以单击右上角的设置图标并将“凭据”更改为“包含”,但它们仍会以“省略”的形式发送。

https://github.com/prisma/graphql-playground/issues/826

【讨论】:

    猜你喜欢
    • 2022-08-21
    • 2014-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 2021-03-09
    • 2021-07-05
    相关资源
    最近更新 更多