Node.js 尝试在Graphql服务器中使用带有Passportjs的Google Oauth2

Node.js 尝试在Graphql服务器中使用带有Passportjs的Google Oauth2,node.js,authentication,passport.js,graphql-js,passport-google-oauth,Node.js,Authentication,Passport.js,Graphql Js,Passport Google Oauth,我认为我正确地遵循了所有文档来实现这一点,但是,我得到了一个TokenError:Bad Request,其中包含错误消息invalid\u grant 我的服务器非常简单: require('dotenv').config(); import createServer from './createServer'; const passport = require('passport'); const GoogleStrategy = require('passport-google-oaut

我认为我正确地遵循了所有文档来实现这一点,但是,我得到了一个
TokenError:Bad Request
,其中包含错误消息
invalid\u grant

我的服务器非常简单:

require('dotenv').config();
import createServer from './createServer';

const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;

const server = createServer();

passport.use(
  new GoogleStrategy(
    {
      clientID: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
      callbackURL: 'http://localhost:4000/auth/callback',
    },
    (accessToken, refreshToken, profile, cb) => {
      console.log(accessToken);
      console.log(refreshToken);
      console.log(profile);

      cb(null, profile);
    },
  ),
);

server.express.use(
  '/auth',
  passport.authenticate('google', {
    scope: ['email', 'profile'],
    session: false,
  }),
);

server.express.use(
  '/auth/callback',
  passport.authenticate('google', {
    successRedirect: 'http://localhost:3000/authenticate',
    failureRedirect: 'http://localhost:3000/authenticate',
  }),
);

server.start(
  {
    cors: {
      credentials: true,
      origin: 'http://localhost:3000',
    },
  },
  () => console.log('Server is running on http://localhost:4000'),
);
我在云平台上设置Google的方式有问题吗?我不知道哪里出了错。我的回调设置正确。我不知道还能在哪里找到错误


另一件令人困惑的事情是,Google的策略是控制台记录用户配置文件和返回的访问令牌。我猜想,当回调路由尝试验证URL中的代码时,会出现错误。有谁能告诉我一个更好地解决这个问题的方向吗?提前感谢。

我找到了一个适合我的解决方案,但我仍然不清楚它是否是“最佳实践”。我希望有更多GraphQL经验的人加入进来

我按照文档中的说明在前端进行身份验证:

然后,我在后端编写了一个名为isAuthenticated的查询,可以用来验证令牌

async isAuthenticated(_: any, { token }) {
    if(!token) {
      return null;
    }

    const ticket = await client.verifyIdToken({
      idToken: token,
      audience: process.env.GOOGLE_CLIENT_ID,
    });

    return payload;
  },

在呈现任何受保护的路由之前,我使用React组件检查localStorage中的令牌。这对我有用。

如果用户在访问您的专用路由之前手动更改本地存储,该怎么办?