获取在React JS中使用google-auth-library为Cloud Run获取短暂ID令牌。

huangapple go评论62阅读模式
英文:

Aquiring a short-lived ID token for Cloud Run in React JS using google-auth-library

问题

I have a React JS project, that lets users log in via my own Keycloak-based ID provider, returning a JWT token in case of successful authentication.

I'd like to use this token to acquire a short-lived ID token from Google Cloud to invoke a Cloud Run endpoint.

All GCP configuration has been set up correctly since I can get this token if I invoke the proper endpoints by hand, but I'd like to automate it from my React app. AFAIK the google-auth-library has the functionality implemented that lets me get this token, but when I npm i google-auth-library it in my project and start the app, I get a plethora of errors akin to this:

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "util": require.resolve("util/") }'
        - install 'util'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "util": false }

Is there a way for me to prevent these errors from happening? I already tried react-app-rewrite without much success.

React version: 18.2.0
google-auth-library version: 8.7.0

英文:

I have a React JS project, that lets users log in via my own Keycloak-based ID provider, returning a JWT token in case of successful authentication.

I'd like to use this token to acquire a short-lived ID token from Google Cloud to invoke a Cloud Run endpoint.

All GCP configuration has been set up correctly since I can get this token if I invoke the proper endpoints by hand, but I'd like to automate it from my React app. AFAIK the google-auth-library has the functionality implemented that lets me get this token, but when I npm i google-auth-library it in my project and start the app, I get a plethora of errors akin to this:

BREAKING CHANGE: webpack &lt; 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback &#39;resolve.fallback: { &quot;util&quot;: require.resolve(&quot;util/&quot;) }&#39;
        - install &#39;util&#39;
If you don&#39;t want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { &quot;util&quot;: false }

Is there a way for me to prevent these errors from happening? I already tried react-app-rewrite without much success.

React version: 18.2.0
google-auth-library version: 8.7.0

答案1

得分: 1

你遇到错误的原因是 google-auth-library 是一个 Node.js 库,而不是 React.js 库。

在 React.js 前端中不应使用 google-auth-library,因为它会带来安全风险。

你可以查找并使用与 google-auth-library 中要使用的相同功能相对应的 API 调用。

或者创建一个 Node.js 服务器,用于在前端执行这些操作。

英文:

You are facing the error cause google-auth-library is a node.js library and not a react js library.

You should not use google-auth-library in React.js frontend cause it'll pose a security threat.

You can find and use API calls for the same function you are going to use in the google-auth-library.

Or else create a node js server which does these things for your frontend.

huangapple
  • 本文由 发表于 2023年4月11日 03:56:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980269.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定