【问题标题】:Using AWS Amplify Auth on the server with custom auth-storage在具有自定义 auth-storage 的服务器上使用 AWS Amplify Auth
【发布时间】:2019-10-10 08:36:04
【问题描述】:

我有一个服务器端渲染的反应应用程序,它调用 Amplify 的 Auth.CurrentAuthenticatedUser 方法在显示受保护的页面之前检查身份验证。
在客户端我使用cookieStorage。当通过 react-router 的 BrowserRouter 访问受保护的路由时调用 Auth.CurrentAuthenticatedUser 时成功检索凭据,它工作得非常好。
在服务器端(当通过直接 http 调用访问受保护的路由时),我对 Amplify 使用以下配置:

import cookie from 'cookie';

class ServerCookieStorage {
  constructor(requestCookie) {
    this.cookieObj = {};
    if (requestCookie) this.cookieObj = cookie.parse(requestCookie);
  }
  setItem(key, value) {
    this.cookieObj[key] = value;
  }
  getItem(key) {
    return this.cookieObj[key];
  }
  removeItem(key) {
    this.cookieObj[key] = '';
  }
}

Amplify.configure({
  Auth: {
    identityPoolId: 'placeholder',
      region: 'placeholder',
      userPoolId: 'placeholder',
      userPoolWebClientId: 'placeholder',
      storage: new ServerCookieStorage(event.headers.Cookie);
      //cookie header in aws lambda functions is located in event.header.Cookie
  }
});

我已经记录了 setItemgetItem 方法中的内容,并且在调用 Auth.CurrentAuthenticatedUser 方法时似乎可以正常检索所有信息,但是该方法失败并出现错误 Not Authenticated

我错过了什么吗?

【问题讨论】:

标签: javascript node.js reactjs amazon-cognito aws-amplify


【解决方案1】:

我意识到Auth.CurrentAuthenticatedUser 在后台使用fetch 方法,这就是它失败的原因。解决方案是使用node-fetch 为节点提供fetch 方法polyfill,我是这样做的:

const fetch = require('node-fetch');
global.fetch = fetch;

【讨论】:

    猜你喜欢
    • 2023-02-03
    • 1970-01-01
    • 2022-07-30
    • 2020-11-18
    • 2019-12-25
    • 2021-11-16
    • 1970-01-01
    • 2023-03-22
    • 2020-08-21
    相关资源
    最近更新 更多