通过自定义身份验证存储在服务器上使用AWS Amplify Auth

时间:2019-05-23 18:10:58

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

我有一个服务器端呈现的react应用程序,该应用程序在显示受保护的页面之前调用Amplify的drwxrwxrwx 2 elasticsearch elasticsearch 0 May 22 22:44 analysis 方法来检查身份验证。
在客户端,我正在使用lrwxrwxrwx 1 elasticsearch elasticsearch 43 May 22 22:49 analysis -> /elasticdata/fileshare/analysis/ 。由于通过反应路由器的elasticsearch访问受保护的路由在调用Auth.CurrentAuthenticatedUser时成功检索到凭据,因此它可以很好地工作。
在服务器端(当通过直接的http调用访问受保护的路由时),我对Amplify使用以下配置:

cookieStorage

我已经记录了Auth.CurrentAuthenticatedUserBrowserRouter方法中的内容,并且在调用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 } }); 方法时似乎可以很好地检索所有信息,但是该方法失败了错误setItem

我在这里想念东西吗?

1 个答案:

答案 0 :(得分:0)

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

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