amplify-js
amplify-js copied to clipboard
getStaticPaths No current user Server Error
Before opening, please confirm:
- [X] I have searched for duplicate or closed issues and discussions.
- [X] I have read the guide for submitting bug reports.
- [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
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:
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
- install amplify and other required packages.
- set Cognito user pool and a public API key
- 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
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
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 yup, added the ssr to it
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 Hi, have you found the solution?
Same problem here, I'm doing exactly what suggested in the docs
same for me problem.
same problem here, is there any solution on this?
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'
@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" })
Same error in 08/2022. It's not great when you follow the official documentation and stuck in the middle. (QA documentation?)
@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).
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"
});
// ...
}
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!