我有一个服务器端呈现的反应应用程序,它Auth.CurrentAuthenticatedUser
在显示受保护的页面之前调用 Amplify 的方法来检查身份验证。
在客户端,我使用cookieStorage
. 它工作得很好,因为在Auth.CurrentAuthenticatedUser
通过 react-router 访问受保护路由时成功检索到凭据BrowserRouter
。
在服务器端(当受保护的路由通过直接 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
}
});
我已经记录了进入setItem
和getItem
方法的内容,并且在Auth.CurrentAuthenticatedUser
调用该方法时似乎可以正常检索所有信息,但是该方法因错误而失败Not Authenticated
。
我在这里错过了什么吗?
我意识到它在Auth.CurrentAuthenticatedUser
幕后使用了该fetch
方法,这就是它失败的原因。解决方案是fetch
为 node using提供一种polyfill 方法node-fetch
,我是这样做的:
const fetch = require('node-fetch');
global.fetch = fetch;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句