Express-Session不设置Cookie?

xqnpmsa8  于 2022-09-21  发布在  Redis
关注(0)|答案(3)|浏览(212)

我跟随Ben Awad's 13-hour Fullstack React GraphQL TypeScript Tutorial,在设置登录cookie时遇到了一堵墙(aprx在1:50:00)。

我认为我成功地连接到了Redis,设置了Express-Session并设置了请求类型,但在GraphQL沙箱中,我在Inspect->Application中看不到我的cookie(名为‘qid’)。

Index.ts

import { MikroORM } from "@mikro-orm/core";
import { __prod__ } from "./constants";
import microConfig from "./mikro-orm.config";
import express from "express";
import { ApolloServer } from "apollo-server-express";
import { buildSchema } from "type-graphql";
import { HelloResolver } from "./resolvers/hello";
import { PostResolver } from "./resolvers/post";
import { UserResolver } from "./resolvers/user";
import redis from "redis";
import session from "express-session";
import connectRedis from "connect-redis";

const main = async () => {
  const orm = await MikroORM.init(microConfig);
  await orm.getMigrator().up();

  const app = express();

  const RedisStore = connectRedis(session);
  const redisClient = redis.createClient();

  app.use(
    session({
      name: "qid",
      store: new RedisStore({
        client: redisClient,
        disableTouch: true,
      }),
      cookie: {
        maxAge: 1000 * 60 * 60 * 24 * 365 * 10,
        httpOnly: true,
        sameSite: "none",
        // secure: __prod__,
      },
      saveUninitialized: false,
      secret: "dfhfdjkgfkbjktzkzf",
      resave: false,
    })
  );

  app.use(function (req, res, next) {
    res.header(
      "Access-Control-Allow-Origin",
      "https://studio.apollographql.com"
    );
    res.header("Access-Control-Allow-Credentials", "true");
    next();
  });

  const apolloServer = new ApolloServer({
    schema: await buildSchema({
      resolvers: [HelloResolver, PostResolver, UserResolver],
      validate: false,
    }),
    context: ({ req, res }) => ({ em: orm.em, req, res }),
  });

  await apolloServer.start();
  apolloServer.applyMiddleware({
    app,
    cors: {
      credentials: true,
      origin: new RegExp("/*/"),
    },
  });

  app.listen(4000, () => {
    console.log("server started on port 4000");
  });
};

main();

Types.ts

import { EntityManager, IDatabaseDriver, Connection } from "@mikro-orm/core";
import { Request, Response } from "express";
import { Session, SessionData } from "express-session";

export type MyContext = {
  em: EntityManager<any> & EntityManager<IDatabaseDriver<Connection>>;
  req: Request & {
    session: Session & Partial<SessionData> & { userId: number };
  };
  res: Response;
};

和我的userResolver(user.ts)

import { User } from "../entities/User";
import { MyContext } from "../types";
import {
  Arg,
  Ctx,
  Field,
  InputType,
  Mutation,
  ObjectType,
  Query,
  Resolver,
} from "type-graphql";
import argon2 from "argon2";

@InputType()
class UsernamePasswordInput {
  @Field()
  username: string;

  @Field()
  password: string;
}

@ObjectType()
class FieldError {
  @Field()
  field: string;

  @Field()
  message: string;
}

@ObjectType()
class UserResponse {
  @Field(() => [FieldError], { nullable: true })
  errors?: FieldError[];

  @Field(() => User, { nullable: true })
  user?: User;
}

@Resolver()
export class UserResolver {

  @Mutation(() => UserResponse)
  async login(
    @Arg("options", () => UsernamePasswordInput) options: UsernamePasswordInput,
    @Ctx() { em, req }: MyContext
  ): Promise<UserResponse> {
    const user = await em.findOne(User, { username: options.username });
    if (!user) {
      return {
        errors: [
          {
            field: "username",
            message: "username does not exist",
          },
        ],
      };
    }
    const valid = await argon2.verify(user.password, options.password);
    if (!valid) {
      return {
        errors: [
          {
            field: "password",
            message: "incorrect password",
          },
        ],
      };
    }

    req.session.userId = user.id;

    return {
      user,
    };
  }
}

我试着按照GraphQL沙箱的要求设置res.Header,但仍然无济于事。将不胜感激,谢谢您的帮助!

f0brbegy

f0brbegy1#

好吧,我不知道发生了什么,但我似乎解决了这个问题。

我的想法是:GraphQLPlayground停用了,而Localhost:port/GraphQL现在将Apollo GraphQL Sandbox重定向到不同的URL,我猜Cookie不会传输到这个位置,但Cookie设置在Localhost。

因此,有一种方法可以迫使阿波罗仍然使用Playground,方法是添加:

import { ApolloServerPluginLandingPageGraphQLPlayground } from "apollo-server-core";

  const apolloServer = new ApolloServer({
    ...,
    plugins: [
      ApolloServerPluginLandingPageGraphQLPlayground({
        // options
      }),
    ],
  });

这样,Playground就会出现,你可以设置

"request.credentials": "include",

在设置中,cookie显示在localhost:port中。

我希望这对任何人解决这个问题都有帮助--然而,我仍然不确定这是不是一个正确的解决方案。

ghhaqwfi

ghhaqwfi2#

将旧Playground添加为插件可能会奏效,但既然他们说它已被弃用,如果你想让它与新的阿波罗工作室一起工作,以下是我成功做到这一点的方法:

我在应用程序初始化后添加了这三行:

app.set("trust proxy", !process.env.NODE_ENV === "production");
  app.set("Access-Control-Allow-Origin", "https://studio.apollographql.com");
  app.set("Access-Control-Allow-Credentials", true);

下面是我的会话的配置:

const RedisStore = connectRedis(session);
const redisClient = redis.createClient();

app.use(
  session({
    saveUninitialized: false,
    store: new RedisStore({ client: redisClient }),
    cookie: {
      maxAge: 1000 * 60 * 60 * 24 * 365 * 1, // 1 year
      httpOnly: true,
      sameSite: "none",
      secure: true, // if true, studio works, postman doesn't; if false its the other way around
    },
    name: "qid",
    secret: "keyboard cat",
    resave: false,
  }),
);

然后,转到Apollo Studio,进入连接设置->编辑->包括Cookie(这个Cookie真的很难找到):

确保将此标头与每个登录请求一起发送:x-forwarded-proto: https

6qfn3psc

6qfn3psc3#

在这件事上花了一些时间。尝试此组合解决方案:

import { MikroORM } from "@mikro-orm/core";
import { __prod__ } from "./constants";
import microConfig from "./mikro-orm.config";
import express from "express";
import { ApolloServer } from "apollo-server-express";
import { buildSchema } from "type-graphql";
import { PostResolver } from "./resolvers/Post";
import { UserResolver } from "./resolvers/User";
import session from "express-session";
import connectRedis from "connect-redis";
import { createClient } from "redis";

const main = async () => {
  try {
    const orm = await MikroORM.init(microConfig);
    orm.getMigrator().up();
    const app = express();
    app.set("trust proxy", process.env.NODE_ENV !== "production"); //a little fix here from another users codes--- actually think this made it works
    app.set("Access-Control-Allow-Origin", "https://studio.apollographql.com");
    app.set("Access-Control-Allow-Credentials", true);
    let redisClient = createClient({ legacyMode: true });
    redisClient.connect().catch(console.error);
    let RedisStore = connectRedis(session);
    const cors = {
      credentials: true,
      origin: "https://studio.apollographql.com",
    };
    app.use(
      session({
        name: "qid",
        store: new RedisStore({ client: redisClient as any, disableTTL: true }),
        cookie: {
          maxAge: 1000 * 60 * 60 * 24 * 365 * 10,
          httpOnly: true,
          secure: true,
          sameSite: "none",
        },
        saveUninitialized: false,
        secret: "keyboardcaxt",
        resave: false,
      })
    );
    const apolloServer = new ApolloServer({
      schema: await buildSchema({
        resolvers: [PostResolver, UserResolver],
        validate: false,
      }),
      context: ({ req, res }) => ({ em: orm.em, req, res }),
    });

    await apolloServer.start();
    apolloServer.applyMiddleware({ app, cors });
    app.listen(4000, () => {
      console.log("EXPRESS SERVER IS RUNNINGG");
    });
  } catch (error) {
    console.log(error, "ERRR");
  }
};

main();

也不要忘记这个setup和硬重置您的https://studio.apollographql.com/sandbox/。还有这个:add ENV to your root

那你应该已经准备好出发了。

相关问题