【问题标题】:How does Passport (oAuth2) work with GraphQL (TypeGraphQL)?Passport (oAuth2) 如何与 GraphQL (TypeGraphQL) 一起使用?
【发布时间】:2020-01-09 09:25:07
【问题描述】:

我正在尝试使用 Google 策略。

我的目标是让用户通过仅通过 Google 登录进行身份验证来访问 React Next.js 应用程序的受限区域。这是基于他们的电子邮件扩展名为 @SpecificDomain.com。

我在前端有一个组件,它打开一个 Google 登录窗口并返回带有访问令牌的对象。据我了解,然后我将此对象转发到我自己的后端(Apollo 服务器,TypeGraphQL 配置)。

我的 ApolloServer 实例如下所示:

onst apolloServer = new ApolloServer({ schema,
context: async ({ req}) => {
    let token = null;
    let currentUser = null;

    try{
      token = req.headers.authorization;

      if(token){
        currentUser = await authenticate(token);
      }
    } catch (error) {
      console.warn(`Unable to authenticate using auth token: ${token}`);
    }

    return {
      currentUser,
      token
    }



} }) as any;

在上下文中,我尝试通过将令牌发送到另一个组件来验证用户访问令牌,在该组件中我尝试使用 Passport,该组件使用 Google oauth2 策略来验证用户。

但是,我在网上看到的所有示例都使用 Express 中间件。我想知道是否有任何方法可以仅使用 GraphQL 来做到这一点?

我不明白我是怎么称呼这个的:

    passport.use(new GoogleStrategy({
  clientID: googleCredentials.CLIENT_ID,
  clientSecret: googleCredentials.CLIENT_SECRET,
  callbackURL: googleCredentials.redirect_uris[0]
},
function(accessToken : string, cb : any, refreshToken? : string, profile? : string) {
  const userOject = {accessToken, refreshToken, profile};
  return cb(null, userOject);
}
));

如何将令牌传递给它?

我也试过了,

   export default function authenticateUser(token : string) {
  passport.authenticate('oauth2');

}

但是 passport.authenticate 没有任何令牌参数。

有人可以给我一些指点或指出正确的方法吗?

感谢您的宝贵时间。

【问题讨论】:

    标签: node.js passport.js next.js graphql-js


    【解决方案1】:

    决定使用 Express 来处理身份验证。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-23
      • 2013-10-01
      • 2016-06-09
      • 2021-08-14
      • 2016-08-27
      • 2016-09-24
      • 2016-09-19
      • 1970-01-01
      相关资源
      最近更新 更多