0

I'm working with Next.js Server Side Rendering and AWS Amplify to get data. However, I've come to a roadblock, where I'm getting an error saying that there's no current user.

My question is why does the app need to have a user if the data is supposed to be read for the public?

What I'm trying to do is show data for the public, if they go to a user's profile page. They don't have to be signed into the app.

My current folder structure is:

/pages/[user]/index.js with getStaticProps and getStaticPaths:

export async function getStaticPaths() {
  const SSR = withSSRContext();
  const { data } = await SSR.API.graphql({ query: listUsers });
  const paths = data.listUsers.items.map((user) => ({
    params: { user: user.username },
  }));

  return {
    fallback: true,
    paths,
  };
}

export async function getStaticProps({ params }) {
  const SSR = withSSRContext();
  const { data } = await SSR.API.graphql({
    query: postsByUsername,
    variables: {
      username: params.username,
    },
  });

  return {
    props: {
      posts: data.postsByUsername.items,
    },
  };
}
hellomello
  • 8,219
  • 39
  • 151
  • 297

1 Answers1

0

Finally figured it out. A lot of tutorials uses authMode: 'AMAZON_COGNITO_USER_POOLS ' // or AWS_IAM parameter in their graphql query for example in https://docs.amplify.aws/lib/graphqlapi/authz/q/platform/js/

// Creating a post is restricted to IAM 
const createdTodo = await API.graphql({
  query: queries.createTodo,
  variables: {input: todoDetails},
  authMode: 'AWS_IAM'
});

But you rarely come across people who use authMode: API_KEY.

So I guess, if you want the public to read without authentication, you would just need to set authMode: 'API_KEY'...

Make sure you configure your amplify API to have public key as well.

hellomello
  • 8,219
  • 39
  • 151
  • 297