amplify-js: "No current user" error when calling graphql api endpoints in Next.js v14 project

Before opening, please confirm:

JavaScript Framework

Next.js

Amplify APIs

Authentication, GraphQL API

Amplify Version

v6

Amplify Categories

auth, api

Backend

Amplify CLI

Environment information

  System:
    OS: Windows 11 10.0.22631
    CPU: (12) x64 12th Gen Intel(R) Core(TM) i5-12400F
    Memory: 9.12 GB / 31.84 GB
  Binaries:
    Node: 20.10.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.21 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 10.2.3 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.13.1 - ~\AppData\Roaming\npm\pnpm.CMD
  Browsers:
    Edge: Chromium (121.0.2277.112)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    @changesets/changelog-github: ^0.5.0 => 0.5.0 
    @changesets/cli: ^2.27.1 => 2.27.1 
    @commitlint/cli: ^18.4.3 => 18.4.3 
    @commitlint/config-conventional: ^18.4.3 => 18.4.3
    @knocklabs/node: ^0.5.0 => 0.5.0
    @repo/aws-types: workspace:^ => 0.1.0
    @repo/prettier-config: workspace:^0.1.0 => 0.1.0
    @repo/types: workspace:^ => 0.1.0
    @segment/analytics-next: ^1.62.0 => 1.62.0
    @segment/analytics-node: ^1.1.4 => 1.1.4
    @turbo/gen: ^1.11.2 => 1.11.2
    @types/node: ^20.10.5 => 20.10.5
    chalk: ^5.3.0 => 5.3.0
    husky: ^8.0.3 => 8.0.3
    lint-staged: ^15.2.0 => 15.2.0
    prettier: ^3.1.1 => 3.1.1
    prompts: ^2.4.2 => 2.4.2
    turbo: ^1.11.2 => 1.11.2
    typescript: ^5.3.3 => 5.3.3
  npmGlobalPackages:
    @aws-amplify/cli: 12.0.3
    @thejumba/auditable-transformer: 1.3.3
    eas-cli: 6.1.0
    pnpm: 8.13.1
    vercel: 33.0.2
    yarn: 1.22.21

Describe the bug

Recently, I’ve been getting this consistent error No current user on our Next.js v14 application. It appears ever so randomly and always when trying to invoke the amplify GraphQL API from a server component. I’ve tried multiple solutions such as checking that Amplify.configure or Auth.configure is only called in one place and also checking for duplicate amplify version as described here.

Since the app is running on Amplify v6, there’s a utility constant ssrClient that is used to make all graphql api calls on server components as shown in the snippets below.

Expected behavior

The GraphQL API call should be made successfully without any errors being thrown

Reproduction steps

  1. Create a Next.js v14 application
  2. Setup amplify version 6
  3. Configure an amplify graphql api endpoint
  4. Setup SSR functionality for amplify
  5. Call said graphql api endpoint from a server component/server action using utilities provided from aws-amplify/adapter-nextjs package

Code Snippet

Amplify is configured in a file which exports a Providers component that wraps everything else in the app in the root layout.tsx

// app/providers.tsx
"use client"; 

import { Amplify } from "aws-amplify";
import { parseAmplifyConfig } from "aws-amplify/utils";
import amplifyConfig from "@repo/aws-exports";

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

// ... rest of code

We then have some utilities exported for amplify ssr interactions:

// @/lib/amplify-ssr.tsx
import { cookies } from "next/headers";
import { createServerRunner } from "@aws-amplify/adapter-nextjs";
import { generateServerClientUsingCookies } from "@aws-amplify/adapter-nextjs/api";
import { parseAmplifyConfig } from "aws-amplify/utils";

import config from "@repo/aws-exports";

export const ssrClient = generateServerClientUsingCookies({
  cookies,
  config: parseAmplifyConfig(config),
  authMode: "userPool",
});

export const { runWithAmplifyServerContext } = createServerRunner({
  config: parseAmplifyConfig(config),
});

And then we use the ssrClient

const res = await ssrClient.graphql({
    query: "someQuery",
    variables: {
       // query variables
    },
  })

Log output

// Put your logs below this line


aws-exports.js

const awsmobile = {
  "aws_appsync_authenticationType": "AMAZON_COGNITO_USER_POOLS",
  "aws_appsync_graphqlEndpoint": "https://XXXXXXXXXXXXXX.appsync-api.us-east-1.amazonaws.com/graphql",
  "aws_appsync_region": "us-east-1",
  "aws_cognito_identity_pool_id": "us-east-1:XXXXXXXXXXXXXX",
  "aws_cognito_mfa_configuration": "ON",
  "aws_cognito_mfa_types": [
    "SMS"
  ],
  "aws_cognito_password_protection_settings": {
    "passwordPolicyCharacters": [],
    "passwordPolicyMinLength": 8
  },
  "aws_cognito_region": "us-east-1",
  "aws_cognito_signup_attributes": [
    "EMAIL",
    "NAME",
    "PHONE_NUMBER"
  ],
  "aws_cognito_social_providers": [],
  "aws_cognito_username_attributes": [
    "PHONE_NUMBER"
  ],
  "aws_cognito_verification_mechanisms": [
    "PHONE_NUMBER"
  ],
  "aws_project_region": "us-east-1",
  "aws_user_files_s3_bucket": "XXXXXXXXXXXXXX",
  "aws_user_files_s3_bucket_region": "us-east-1",
  "aws_user_pools_id": "XXXXXXXXXXXXXX",
  "aws_user_pools_web_client_id": "XXXXXXXXXXXXXX",
  "oauth": {}
}

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

This is what keeps getting captured by sentry

Screenshot 2024-02-11 023709 Screenshot 2024-02-11 024458

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

Hey @HuiSF , Thank you for your well detailed response. Let me try it then get back to you

Hello, is there any feedback ? The issue is still persistent