Reactjs 没有当前用户-getServerSideProps下一个JS和AWS放大
我正在尝试使用AWS Amplify和Next JS在Reactjs 没有当前用户-getServerSideProps下一个JS和AWS放大,reactjs,amazon-web-services,graphql,next.js,aws-amplify,Reactjs,Amazon Web Services,Graphql,Next.js,Aws Amplify,我正在尝试使用AWS Amplify和Next JS在getServerSideProps的服务器端发出一个经过用户验证的GraphQL请求 仅当my AWS数据库中的用户是文档的所有者时,他们才能访问数据 我从中得到的错误是“没有当前用户”。。。(正在服务器上登录)。 问题是,我需要getServerSideProps中可用的用户,这样我就可以发出经过身份验证的请求 这是我目前拥有的代码 index.tsx: import Amplify, { API, graphqlOperation, w
getServerSideProps
的服务器端发出一个经过用户验证的GraphQL请求
仅当my AWS数据库中的用户是文档的所有者时,他们才能访问数据
我从中得到的错误是“没有当前用户”。。。(正在服务器上登录)。
问题是,我需要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...
);
}
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: [],
},
};
};
index.tsx(getServerSideProps):
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...
);
}
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: [],
},
};
};
如果您能提供任何帮助或建议,我将不胜感激 看起来您可能只需要在
API.graphql
调用中传入authMode
在博客文章中,在标题为“在getServerSideProps中发出经过身份验证的API请求”的部分中,您将看到如下代码示例(注意下面添加的authMode
,我在上面的代码示例中没有看到):