在 static_dir 单页应用程序中对用户进行身份验证,以便它可以调用同级 GAE API

Authenticating a user in static_dir single-page-app so that it can call sibling GAE APIs

我有一个通过静态目录在 GAE 上提供的 React 应用程序。

app.yaml:

- url: /my_admin_app
  static_dir: admin_app
  login: required
  secure: always

通过浏览器访问时,GAE 在继续使用 React 应用程序之前按预期显示登录页面。

因为 React 应用程序完全独立于同样是 运行 的 GAE 应用程序,我需要网络应用程序来调用需要授权的 API,因为它们控制敏感数据。

React 应用正在调用此 URL 背后的敏感 API:

app.yaml:

- url: /admin/.*
  login: required // this causes a login page to be sent instead of data
  script: main.app
  secure: always

有没有更好的方法来提供我的静态文件,这样就不再需要登录了?或者有没有办法在 GAE 提供自己的身份验证信息时传递身份验证信息登录页面?

我了解到,当 Google 显示登录时,它会将身份验证 cookie 传递到以下网页。

然后,页面使用 fetch 发出的任何请求都应指定 cookie 应与该请求一起传递。

来自 Mozilla 文档:https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

By default, fetch won't send or receive any cookies from the server, resulting in unauthenticated requests if the site relies on maintaining a user session (to send cookies, the credentials init option must be set).

因此,为了确保身份验证得到传递:

// to only pass to to same origin endpoints
fetch('/endpoint', {credentials:'same-origin'}).then(/*..*/)

// to pass without restriction
fetch('/endpoint', {credentials:'include'}).then(/*..*/)