在 Apollo Server 上下文中获取 NextAuth.js 用户会话 [英] Getting NextAuth.js user session in Apollo Server context

查看:24
本文介绍了在 Apollo Server 上下文中获取 NextAuth.js 用户会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网络应用正在使用:

My web app is using:

  • NextJS
  • NextAuth.js
  • 阿波罗服务器

我在我的应用中设置了 NextAuth,我可以正常登录.

I have a NextAuth set up in my app, and I am able to log in just fine.

问题来自试图在 Apollo 上下文中访问用户的会话.我想将我的用户会话传递给每个解析器.这是我当前的代码:

The problem is coming from trying to get access to the user's session in the Apollo context. I want to pass my user's session into every resolver. Here's my current code:

import { ApolloServer, AuthenticationError } from "apollo-server-micro";
import schema from "./schema";
import mongoose from "mongoose";
import dataloaders from "./dataloaders";
import { getSession } from "next-auth/client";

let db;

const apolloServer = new ApolloServer({
  schema,
  context: async ({ req }) => {
    /*
    ...

    database connection setup
    
    ...
    */

    // get user's session
    const userSession = await getSession({ req });
    console.log("USER SESSION", userSession); // <-- userSession is ALWAYS null


    if (!userSession) {
      throw new AuthenticationError("User is not logged in.");
    }

    return { db, dataloaders, userSession };
  },
});

export const config = {
  api: {
    bodyParser: false,
  },
};

export default apolloServer.createHandler({ path: "/api/graphql" });

问题是,会话 (userSession) 始终为空,即使我已登录(并且可以从适当的 NextJS API 路由获得会话).我的猜测是因为 NextAuth 函数用于获取会话,getSession({ req }) 正在被传递 req——这是从 Apollo Server Micro 提供的,而不是来自 NextJS(NextAuth 期待).我已经做了很多搜索,但找不到任何遇到同样问题的人.非常感谢任何帮助!

The problem is, the session (userSession) is always null, even if I am logged in (and can get a session just fine from a proper NextJS API route). My guess is that because the NextAuth function used to get the session, getSession({ req }) is being passed req--which is provided from Apollo Server Micro, and not from NextJS (which NextAuth is expecting). I've done a lot of searching and can't find anyone who's had this same problem. Any help is much appreciated!

推荐答案

我确实遇到了这个问题,我发现这是因为 Apollo GraphQL playground.

I had exactly this issue and I found it was because of the Apollo GraphQL playground.

如果没有request.credentials":include",playground 不会发送凭据.

The playground does not send credentials without "request.credentials": "include".

我的 NextAuth/GraphQL API 如下所示:

My NextAuth / GraphQL API looks like this:

import { ApolloServer } from "apollo-server-micro";
import { getSession } from "next-auth/client";
import { typeDefs, resolvers } "./defined-elsewhere"

const apolloServer = new ApolloServer({
  typeDefs,
  resolvers,
  context: async ({ req }) => {
    const session = await getSession({ req });
    return { session };
  },
  playground: {
    settings: {
      "editor.theme": "light",
      "request.credentials": "include",
    },
  },
});

希望这对你有用!

这篇关于在 Apollo Server 上下文中获取 NextAuth.js 用户会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆