-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"No current user" error when calling graphql api endpoints in Next.js v14 project #12996
Comments
Have you tried doing Amplify.configure(awsConfig) on both the client components and server components or are you already doing that? |
Hi @ndaba1 👋 I was able to reproduce the "No current user" error when using the cookie based client on the server side. However, I realized that I had old credentials in Local Storage. I cleared them and then started getting Unauthorized errors. I simply logged in again and was able to retrieve data with the So, it seems this is reproducible if you have expired tokens (including refresh), preventing Amplify from refreshing the cognito access token and resulting in the "No current user" error. Can you confirm that your credentials are not expired and that you are able to reproduce this issue with a recently logged in user? This is the schema I had while reproducing (using Amplify Gen 2 to build the backend) const schema = a.schema({
CommunityPost: a
.model({
title: a.string().required(),
poll: a.hasOne("CommunityPoll"),
})
.authorization([a.allow.private()]),
CommunityPoll: a
.model({
question: a.string().required(),
answers: a.hasMany("CommunityPollAnswer").arrayRequired().valueRequired(),
})
.authorization([a.allow.private()]),
CommunityPollAnswer: a
.model({
answer: a.string().required(),
votes: a.hasMany("CommunityPollVote").arrayRequired().valueRequired(),
})
.authorization([a.allow.private()]),
CommunityPollVote: a
.model({ name: a.string() })
.authorization([a.allow.private()]),
}); |
@chrisbonifacio thank you for taking the time to look into this. Yes, the issue occurs even for recently logged in users. To your point, I do think its related to the tokens not being refreshed correctly for whatever reason. Some users actually get randomly redirected to the sign in page when trying to access an SSR page(with RSC) but upon inputting credentials, the error "There is already a signed in user" is returned. And if you refresh the page, then you seem to be logged in again. |
Since I am able to reproduce the issue, I marked this as a bug for the team to investigate further. Seems more auth related than GraphQL related. To be more confident that we can reproduce it under the same conditions as your application, can you share more details about your auth resource such as the expiration for your tokens? If you are building a Gen 1 Amplify app, using the Amplify CLI, you can also run the following command and provide us the project identifier in the output. This will let us recreate your exact auth resource. amplify diagnose --send-report |
Hey @chrisbonifacio |
Hello, is there any feedback ? The issue is still persistent |
Hi @ndaba1 By reading "It appears ever so randomly and always when trying to invoke the amplify GraphQL API from a server component" I suspect that the issue was caused by a failure of If you are using In the meantime, we are actively exploring a better solution to make fetchAuthSession more efficient. In addition, we have improved error throwing from the GraphQL APIs, the |
Hey @HuiSF , |
@HuiSF we tried your suggested solution and unfortunately, it doesn't seem to work import type { GraphQLOptionsV6 } from "@aws-amplify/api-graphql";
import { cookies } from "next/headers";
import { createServerRunner } from "@aws-amplify/adapter-nextjs";
import { generateClient } from "aws-amplify/api/server";
import { parseAmplifyConfig } from "aws-amplify/utils";
import awsExports from "@repo/aws-exports";
const config = parseAmplifyConfig(awsExports);
const client = generateClient({ config });
export const { runWithAmplifyServerContext } = createServerRunner({
config,
});
export const ssrClient = {
graphql: async <TQuery = unknown>(options: GraphQLOptionsV6<TQuery>) => {
const res = await runWithAmplifyServerContext({
nextServerContext: { cookies },
operation: async (contextSpec) => {
return client.graphql<TQuery>(contextSpec, {
query: options.query,
variables: options.variables,
authMode: options.authMode,
});
},
});
return res;
},
}; This is how we refactored the |
Hi @ndaba1 thanks for following up. Looking at your implementation, it's actually no difference from calling The above linked comment, suggested that to make multiple |
Hey @HuiSF, thank you for getting back to me so quickly. I think I see what you mean: And if that's the case, wouldn't still we risk running into the issue if there are a high number of concurrent requests for a server rendered page (from different users) ? Also, would you kindly offer some more insights on which Cognito Service Quotas we'd try adjusting to help with this issue ? The callback in the docs doesn't specify which limits one would need to adjust. You mentioned
but I'm not sure which service calls this would be |
That's correct @ndaba1 the optimization was only for multiple GraphQL calls within one incoming request.
For your use case, I'd recommend first to monito the |
@ndaba1, wanted to check in and see if you had further questions on this or are still blocked. Let us know! |
Closing this issue as we have not heard back from you. If you are still experiencing this, please feel free to reply back and provide any information previously requested and we'd be happy to re-open the issue. Thank you! |
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
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 thatAmplify.configure
orAuth.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
aws-amplify/adapter-nextjs
packageCode Snippet
Amplify is configured in a file which exports a
Providers
component that wraps everything else in the app in the rootlayout.tsx
We then have some utilities exported for amplify ssr interactions:
And then we use the ssrClient
Log output
aws-exports.js
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
The text was updated successfully, but these errors were encountered: