【发布时间】: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
}
});
我已经记录了 setItem 和 getItem 方法中的内容,并且在调用 Auth.CurrentAuthenticatedUser 方法时似乎可以正常检索所有信息,但是该方法失败并出现错误 Not Authenticated。
我错过了什么吗?
【问题讨论】:
-
你也可以只使用
CookieStoragestackoverflow.com/a/62646354/5874913
标签: javascript node.js reactjs amazon-cognito aws-amplify