Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next.js and AWS Amplify getting Error: No Current User - getServerSideProps

getServerSideProps is displaying "No Current User", but I'm logged in my app.

type Post
  @model
  @key(
    name: "postsByUsername"
    fields: ["username"]
    queryField: "postsByUsername"
  )
  @auth(
    rules: [
      { allow: owner, ownerField: "username" }
      { allow: private, operations: [read] }
      { allow: public, operations: [read] }
    ]
  ) {
  id: ID!
  username: String
  image: S3Object
}

I'm able to use AWS AppSync's website to actually query the posts by username, it returned values. I logged in using the same login I'm in for my app.

export async function getServerSideProps() {
  const SSR = withSSRContext();
  const { data } = await SSR.API.graphql({
    query: postsByUsername,
    variables: {
      username: "username" // checked in dynamodb, same username as signed in. 
    },
    authMode: "AMAZON_COGNITO_USER_POOLS",
  });
  return {
    props: {
      posts: data.postsByUsername.items,
    },
  };
}

I've also added ssr in my _app.js:

import { Amplify } from "aws-amplify";
import awsExports from "../../aws-exports";

Amplify.configure({ ...awsExports, ssr: true });

in aws-exports.js:

const awsmobile = {
    "aws_project_region": "xxxxx",
    "aws_appsync_graphqlEndpoint": "https://...",
    "aws_appsync_region": "xxxxx",
    "aws_appsync_authenticationType": "AMAZON_COGNITO_USER_POOLS",
    "aws_appsync_apiKey": "xxx-xxxxxxxx...",
    "aws_cognito_identity_pool_id": "xxxxxx",
    "aws_cognito_region": "xxxxx",
    "aws_user_pools_id": "xxxxx",
    "aws_user_pools_web_client_id": "xxxxxx",
    "oauth": {},
    "aws_user_files_s3_bucket": "xxxxxx",
    "aws_user_files_s3_bucket_region": "xxxxxx"
};

everything in awsmobile were autogenerated.

like image 286
hellomello Avatar asked Oct 27 '25 04:10

hellomello


1 Answers

You need to pass through the request context in your withSSRContext function, so your getServerSideProps function should look as follows

export async function getServerSideProps({ req }) {
  const SSR = withSSRContext({ req });
  ...
}

Reference: https://docs.amplify.aws/lib/ssr/q/platform/js/#withssrcontext

like image 128
bourkison Avatar answered Oct 29 '25 18:10

bourkison