首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError: jwt.split不是OAuth2Client.verifySignedJwtWithCertsAsync节点包中的函数: google-auth-library

TypeError: jwt.split不是OAuth2Client.verifySignedJwtWithCertsAsync节点包中的函数: google-auth-library
EN

Stack Overflow用户
提问于 2021-03-27 21:55:08
回答 1查看 295关注 0票数 0

来自客户端react应用程序的react-google-login使用post请求将响应发送回Nodejs服务器-

客户端代码-

代码语言:javascript
复制
import axios from 'axios';
import React, { Component } from 'react';
import GoogleLogin from 'react-google-login';
import refreshTokenSetup from '../../utils/refreshToken';

const clientId =
 'xxxxxx-xfdgsdjg3gfxxxxxxxxxxx.apps.googleusercontent.com';


function Login() {
 const onSuccess = (res) => {
   console.log('Login Success: currentUser:', res.profileObj);
   alert(
     `Logged in successfully welcome ${res.profileObj.name} . \n See console for full profile object.`
   );
   axios
     .post('http://localhost:5000/auth/checkToken', { body: res.tokenId })
     .then()
     .catch((err) => {
       console.log(err);
     });
 };

 const onFailure = (res) => {
   console.log('Login failed: res:', res);
   alert(
     `Failed to login.  Please ping this to repo owner twitter.com/sivanesh_fiz`
   );
 };

 return (
   <div>
     <GoogleLogin
       clientId={clientId}
       buttonText='Login'
       onSuccess={onSuccess}
       onFailure={onFailure}
       cookiePolicy={'single_host_origin'}
       style={{ marginTop: '100px' }}
       isSignedIn={true}
     />
   </div>
 );
}

export default Login;

后端路由-

代码语言:javascript
复制
const { OAuth2Client } = require('google-auth-library');
const key = require('../config/key');
module.exports = {
  checkToken: (req, res, next) => {
    console.log('checking begins...', req.body);

    const client = new OAuth2Client(key.GOOGLE_CLIENT_ID);
    async function verify() {
      const ticket = await client.verifyIdToken({
        idToken: req.body,
        audience: key.GOOGLE_CLIENT_ID, // Specify the CLIENT_ID of the app that accesses the backend
        // Or, if multiple clients access the backend:
        //[CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3]
      });
      const payload = ticket.getPayload();
      const userid = payload['sub'];
      // If request specified a G Suite domain:
      // const domain = payload['hd'];
    }
    verify().catch(console.error);
  },
};

上面的代码参考了谷歌官方文档,网址为- https://developers.google.com/identity/sign-in/web/backend-auth

现在一切正常,用户在客户端登录,tokenId被发送回服务器,并可以通过控制台记录它来验证,即使在https://jwt.io/上也是如此,但显示了以下错误-

代码语言:javascript
复制
TypeError: jwt.split is not a function
    at OAuth2Client.verifySignedJwtWithCertsAsync (E:\Projects\EAbackend\node_modules\google-auth-library\build\src\auth\oauth2client.js:528:30)
    at OAuth2Client.verifyIdTokenAsync (E:\Projects\EAbackend\node_modules\google-auth-library\build\src\auth\oauth2client.js:394:34)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async verify (E:\Projects\EAbackend\middleware\auth.js:9:22)
EN

回答 1

Stack Overflow用户

发布于 2021-03-27 21:55:08

问题出在idToken: req.body,

req.body有一个body对象,其中包含令牌,只需将其更改为req.body.body即可解决错误。

这个问题可能是非常糟糕的,但却占用了我很多时间,而且没有在线资源可以为我指明方向。

检查POST请求,您会发现错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66832082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档