【发布时间】:2021-05-04 15:40:09
【问题描述】:
我正在尝试使用 AWS Amplify 和 Next JS 在 getServerSideProps 的服务器端发出经过用户身份验证的 GraphQL 请求。
我的 AWS 数据库中的用户只有作为文档的所有者才能访问数据。
我从中得到的错误是 "No current user"...(正在登录服务器)。 问题是,我需要 getServerSideProps 中可用的用户,这样我才能使经过身份验证的请求发生。
这是我目前拥有的代码
index.tsx:
import Amplify, { API, graphqlOperation, withSSRContext } from "aws-amplify";
import config from "../aws-exports";
Amplify.configure({ ...config, ssr: true });
function Index({ bankAccounts }: { bankAccounts: BankAccount[] }) {
return (
...stuff...
);
}
index.tsx (getServerSideProps):
export const getServerSideProps: GetServerSideProps = async (context) => {
const { API } = withSSRContext(context);
const result = (await API.graphql({
query: listBankAccounts,
})) as {
data: ListBankAccountsQuery;
errors: any[];
};
if (!result.errors) {
return {
props: {
bankAccounts: result.data.listBankAccounts.items,
},
};
}
return {
props: {
bankAccounts: [],
},
};
};
如果您能提供任何帮助或建议,我将不胜感激!
【问题讨论】:
-
我也遇到了这个问题。你能解决问题吗?提供
authMode的解决方案对我不起作用
标签: reactjs amazon-web-services graphql next.js aws-amplify