amplify-js icon indicating copy to clipboard operation
amplify-js copied to clipboard

getStaticPaths No current user Server Error

Open levelingup opened this issue 3 years ago • 10 comments

Before opening, please confirm:

JavaScript Framework

React, Next.js

Amplify APIs

Authentication, GraphQL API

Amplify Categories

auth

Environment information

  System:
    OS: macOS 11.5.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 97.55 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 15.14.0 - ~/.nvm/versions/node/v15.14.0/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.10.0 - ~/.nvm/versions/node/v15.14.0/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Browsers:
    Chrome: 92.0.4515.131
    Safari: 14.1.2
    Safari Technology Preview: 15.0
  npmPackages:
    @ampproject/toolbox-optimizer:  undefined ()
    @aws-amplify/ui-react: ^1.2.8 => 1.2.8 
    @babel/core:  undefined ()
    @headlessui/react: ^1.4.0 => 1.4.0 
    @heroicons/react: ^1.0.3 => 1.0.3 
    @tailwindcss/forms: ^0.3.3 => 0.3.3 
    @tailwindcss/typography: ^0.4.1 => 0.4.1 
    amphtml-validator:  undefined ()
    arg:  undefined ()
    async-retry:  undefined ()
    async-sema:  undefined ()
    autoprefixer: ^10.3.1 => 10.3.1 
    aws-amplify: ^4.2.2 => 4.2.2 
    bfj:  undefined ()
    cacache:  undefined ()
    ci-info:  undefined ()
    comment-json:  undefined ()
    compression:  undefined ()
    conf:  undefined ()
    content-type:  undefined ()
    cookie:  undefined ()
    css-loader:  undefined ()
    debug:  undefined ()
    devalue:  undefined ()
    escape-string-regexp:  undefined ()
    eslint: 7.32.0 => 7.32.0 
    eslint-config-next: 11.0.1 => 11.0.1 
    file-loader:  undefined ()
    find-cache-dir:  undefined ()
    find-up:  undefined ()
    fresh:  undefined ()
    gzip-size:  undefined ()
    http-proxy:  undefined ()
    ignore-loader:  undefined ()
    is-animated:  undefined ()
    is-docker:  undefined ()
    is-wsl:  undefined ()
    json5:  undefined ()
    jsonwebtoken:  undefined ()
    loader-utils:  undefined ()
    lodash.curry:  undefined ()
    lru-cache:  undefined ()
    mini-css-extract-plugin:  undefined ()
    nanoid:  undefined ()
    neo-async:  undefined ()
    next: 11.0.1 => 11.0.1 
    ora:  undefined ()
    postcss: ^8.3.6 => 8.3.6 (8.2.13)
    postcss-flexbugs-fixes:  undefined ()
    postcss-loader:  undefined ()
    postcss-preset-env:  undefined ()
    postcss-scss:  undefined ()
    react: 17.0.2 => 17.0.2 (17.0.1)
    react-dom: 17.0.2 => 17.0.2 
    recast:  undefined ()
    resolve-url-loader:  undefined ()
    sass: ^1.37.5 => 1.37.5 
    sass-loader:  undefined ()
    schema-utils:  undefined ()
    semver:  undefined ()
    send:  undefined ()
    source-map:  undefined ()
    string-hash:  undefined ()
    strip-ansi:  undefined ()
    tailwindcss: ^2.2.7 => 2.2.7 
    terser:  undefined ()
    text-table:  undefined ()
    unistore:  undefined ()
    uuid: ^8.3.2 => 8.3.2 (3.4.0, 3.3.2)
    web-vitals:  undefined ()
    webpack:  undefined ()
    webpack-sources:  undefined ()
  npmGlobalPackages:
    @aws-amplify/cli: 5.0.2
    agentkeepalive: 4.1.4
    expo-cli: 4.5.2
    npm: 7.10.0
    react-native-cli: 2.0.1

Describe the bug

Using Next.JS

Constantly getting this error:

Screen Shot 2021-08-14 at 11 25 57 AM

Using getStaticPaths() and getStaticProps() for dynamic routing

export async function getStaticPaths() {
  const activationData = await API.graphql({
    query: list*****,
  });
  // ...
}

export async function getStaticProps() {
  // ...
}

In addition, it just keeps refreshing the page

Expected behavior

I was able to get a list of my queries from client side, so I should be able to return something, but instead, getting the No current user error on server side.

Reproduction steps

  1. install amplify and other required packages.
  2. set Cognito user pool and a public API key
  3. fetch the list using amplify api

Code Snippet

No response

Log output

// Put your logs below this line


aws-exports.js

No response

Manual configuration

Screen Shot 2021-08-14 at 1 43 26 PM

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

No response

levelingup avatar Aug 14 '21 18:08 levelingup

Hi @levelingup :wave: Thank you for raising this issue. Did you call Amplify.configure like so in your _app.js file?

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

chrisbonifacio avatar Aug 20 '21 19:08 chrisbonifacio

@chrisbonifacio yup, added the ssr to it

levelingup avatar Aug 26 '21 03:08 levelingup

Hello, I wanted to see if there's any updates on this? I'm getting no current user when I'm doing getServerSideProps as well with a different query. But when I'm in the AWS AppSync website to query the same thing, its allowing me to actually query. I've Logged in using the same login I'm using in my app.

levelingup avatar Sep 28 '21 17:09 levelingup

@levelingup Hi, have you found the solution?

moneebalalfi avatar Nov 11 '21 20:11 moneebalalfi

Same problem here, I'm doing exactly what suggested in the docs

giulioambrogi avatar Jan 16 '22 17:01 giulioambrogi

same for me problem.

VTFLAB avatar May 01 '22 14:05 VTFLAB

same problem here, is there any solution on this?

rollinx1 avatar May 21 '22 03:05 rollinx1

When we are using SSR to fetch data, we need to pass authentication info via context and consume it with withSSRContext as below.

const { API } = withSSRContext(context);

And then, use API that is returned from withSSRContext.

The above way is working in pure SSR page(GetServerSideProps) but it doesn't work in GetStaticProps.

For GetStaticProps, I'm getting No current user error. Current workaround is setting authMode to API_KEY in the graphql query parameter.

authMode: 'API_KEY'

naingaungphyo avatar May 28 '22 19:05 naingaungphyo

@chrisbonifacio Hi, can you please help me with an example to call appsync in GetStaticProps. There is no doc for GetStaticProps but for GetServerSideProps.

First of all, I already have amplify configure as ssr to true Amplify.configure({ ...awsconfig, ssr: true }); in _app.tsx.

in GetStaticProps, I'm calling API as below. Is it correct?

const SSR = withSSRContext(); const posts = await SSR.API.graphql({ query: MyQuery, variables: { MyVariable }, authMode: "AMAZON_COGNITO_USER_POOLS" }) The above way is not working and getting No current user error.

My default appsync authentication type is 'cognito user pools'. i.e "aws_appsync_authenticationType": "AMAZON_COGNITO_USER_POOLS", in aws-exports.js,

so if I don't set authMode parameter, it will be the same result as the above query imo. It is also not working with same error. const SSR = withSSRContext(); const posts = await SSR.API.graphql({ query: MyQuery, variables: { MyVariable } })

With authMode: "API_KEY", it is working though. const SSR = withSSRContext(); const posts = await SSR.API.graphql({ query: MyQuery, variables: { MyVariable }, authMode: "API_KEY" })

naingaungphyo avatar May 28 '22 20:05 naingaungphyo

Same error in 08/2022. It's not great when you follow the official documentation and stuck in the middle. (QA documentation?)

managea avatar Aug 08 '22 13:08 managea

@naingaungphyo @asangamanage can you share the schema for the models you are trying to access in your server-side functions and the components and/or scenarios you're running into this error?

I should point out that withSSRContext can still be used in getStaticProps and getStaticPaths but if you're trying to perform a GraphQL query, the model has to be publicly accessible (via API_KEY). They don't take req objects so there's no way to pass user credentials to make authenticated requests for models (via Cognito User Pools, OIDC, etc).

chrisbonifacio avatar Aug 17 '22 20:08 chrisbonifacio

Based on the original issue, I can see that withSSRContext was not being used. Also, when the API was added via the Amplify CLI, the default authorization type was set to Amazon Cognito User Pool so this is the auth mode the AppSync query would have been made with by default.

export async function getStaticPaths() {
  const activationData = await API.graphql({
    query: list*****,
  });
  // ...
}

Instead, we would use withSSRContext to initialize Amplify on the server, and the request should have manually specified the authMode as API_KEY and the model should allow public read access for the request to be authorized

export async function getStaticPaths() {
  const { API } = withSSRContext();
  
  const activationData = await API.graphql({
    query: list*****,
    authMode: "API_KEY"
  });
  // ...
}

chrisbonifacio avatar Aug 18 '22 17:08 chrisbonifacio

Hi 👋 Closing this as we have not heard back from you. If you are still experiencing this issue and in need of assistance, please feel free to comment and provide us with any information previously requested by our team members so we can re-open this issue and be better able to assist you.

Thank you!

chrisbonifacio avatar Oct 27 '22 16:10 chrisbonifacio