Open siphosenkosindhlovu opened 1 week ago
I'm experiencing the same, regardless if using cookies from next/headers or request+response in middleware
Hi @siphosenkosindhlovu @austinjlaverty can you both confirm the following:
Amplify.configure(config, { ssr: true })
? Hi @siphosenkosindhlovu @austinjlaverty can you both confirm the following:
- Have you configured Amplify on your client-side with
Amplify.configure(config, { ssr: true })
?- After signing in an end user, have the auth tokens been written into browser cookie store?
Everything is configured on the front end. All cookies are present, I can long them in the Server Component. It even runs properly when deployed to AWS Amplify Hosting. But errors out when running locally witch fetchAuthSession(contextSpec)
returning an undefined
session.tokens
field. Client side fetches work correctly through
Hi @siphosenkosindhlovu @austinjlaverty can you both confirm the following:
- Have you configured Amplify on your client-side with
Amplify.configure(config, { ssr: true })
?- After signing in an end user, have the auth tokens been written into browser cookie store?
Yep, setting ssr: true, and cookies are in browser storage. Client amplify APIs work when retrieving a user from these stored values.
This is the final piece of my migration from v5 to v6. Everything else is functioning great
@austinjlaverty and @siphosenkosindhlovu, can you share what shape of your client side config looks like? Feel free to redact/remove any sensitive ID's or informaiton.
@austinjlaverty and @siphosenkosindhlovu, can you share what shape of your client side config looks like? Feel free to redact/remove any sensitive ID's or informaiton.
Here's what my amplifyconfiguration.json
looks like:
{
"aws_project_region": "us-east-1",
"aws_cloud_logic_custom": [
{
"name": "stripe",
"endpoint": "https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/canary",
"region": "us-east-1"
}
],
"aws_appsync_graphqlEndpoint": "https://xxxxxxxxxxxxxxxxx.appsync-api.us-east-1.amazonaws.com/graphql",
"aws_appsync_region": "us-east-1",
"aws_appsync_authenticationType": "AMAZON_COGNITO_USER_POOLS",
"aws_appsync_apiKey": "xxxxxxxxxxxxxxxxxxxxx",
"aws_cognito_identity_pool_id": "us-east-1:xxxxxxxxxxxxxxxxxxxxxxx",
"aws_cognito_region": "us-east-1",
"aws_user_pools_id": "us-east-xxxxxxxxxxxxxx",
"aws_user_pools_web_client_id": "xxxxxxxxxxxxxxxxxx",
"oauth": {},
"aws_cognito_username_attributes": [
"EMAIL"
],
"aws_cognito_social_providers": [],
"aws_cognito_signup_attributes": [
"EMAIL"
],
"aws_cognito_mfa_configuration": "OFF",
"aws_cognito_mfa_types": [],
"aws_cognito_password_protection_settings": {
"passwordPolicyMinLength": 8,
"passwordPolicyCharacters": [
"REQUIRES_LOWERCASE",
"REQUIRES_UPPERCASE",
"REQUIRES_NUMBERS",
"REQUIRES_SYMBOLS"
]
},
"aws_cognito_verification_mechanisms": [
"EMAIL"
],
"aws_user_files_s3_bucket": "xxxxxxxxxxxxxxxxxxxxxxxx",
"aws_user_files_s3_bucket_region": "us-east-1"
}
@austinjlaverty and @siphosenkosindhlovu, can you share what shape of your client side config looks like? Feel free to redact/remove any sensitive ID's or informaiton.
Here is mine:
import { type ResourcesConfig } from "aws-amplify";
import { APP_URL } from "@/utils/env";
export const AMPLIFY_CONFIG: ResourcesConfig = {
Auth: {
Cognito: {
userPoolId: process.env.NEXT_PUBLIC_AMPLIFY_USER_POOL_ID!,
userPoolClientId:
process.env.NEXT_PUBLIC_AMPLIFY_USER_POOL_WEB_CLIENT_ID!,
loginWith: {
oauth: {
domain: process.env.NEXT_PUBLIC_AMPLIFY_OAUTH_DOMAIN!,
scopes: ["phone", "email", "profile", "openid"],
redirectSignIn: [`${APP_URL}/login/verify`],
redirectSignOut: [`${APP_URL}/`],
responseType: "code",
},
},
},
},
};
After some testing, it briefly worked when I'm behind a VPN (Cloudflare WARP in this instance) but stopped again.
@siphosenkosindhlovu want to circle back on this question from earlier. After signing in an end user, have the auth tokens been written into browser cookie store? Can you share the shape/values of the cookies in your cookie store?
@austinjlaverty, can you also share the shape/value of the cookies in your cookies store as well as clarify which API you're calling when you see this exception happening?
We're still trying to reproduce this on our side, but haven't been able to up to this point.
@siphosenkosindhlovu want to circle back on this question from earlier. After signing in an end user, have the auth tokens been written into browser cookie store? Can you share the shape/values of the cookies in your cookie store?
@austinjlaverty, can you also share the shape/value of the cookies in your cookies store as well as clarify which API you're calling when you see this exception happening?
We're still trying to reproduce this on our side, but haven't been able to up to this point.
My app is only using Auth. It interfaces with a separate services API layer built on AWS.
I'm attempting to getCurrentUser()
from aws-amplify/auth/server
within the operation callback:
import { cookies } from "next/headers";
import { Amplify } from "aws-amplify";
import {
fetchAuthSession,
getCurrentUser as getCurrentAmplifyUser,
} from "aws-amplify/auth/server";
import { createServerRunner } from "@aws-amplify/adapter-nextjs";
import { AMPLIFY_CONFIG } from "./config";
Amplify.configure(AMPLIFY_CONFIG, {
ssr: true,
});
export const { runWithAmplifyServerContext } = createServerRunner({
config: AMPLIFY_CONFIG,
});
export async function getCurrentUser() {
const user = await runWithAmplifyServerContext({
nextServerContext: { cookies },
operation: async (context) => {
const session = await fetchAuthSession(context);
console.log({ session, cookieStore: cookies() });
//if (!session.tokens) return;
const user = await getCurrentAmplifyUser(context);
console.log({ user });
return user;
},
});
if (!user) {
throw new Error("unauthenticated");
}
return user;
}
Upon successfully signing in from the client, cookies are present within the browser storage:
The same values are also present when logging the value of cookies()
from the server:
{
session: {
tokens: undefined,
credentials: undefined,
identityId: undefined,
userSub: undefined
},
cookieStore: RequestCookies {
_parsed: Map(10) {
'ajs_user_id' => [Object],
'ajs_anonymous_id' => [Object],
'CognitoIdentityServiceProvider.XXXXXXXXXXXXXXXXXXXXXXXXXX.LastAuthUser' => [Object],
'CognitoIdentityServiceProvider.XXXXXXXXXXXXXXXXXXXXXXXXXX.google_XXXXXXXXXXXXXXXXXXXXX.accessToken' => [Object],
'CognitoIdentityServiceProvider.XXXXXXXXXXXXXXXXXXXXXXXXXX.google_XXXXXXXXXXXXXXXXXXXXX.idToken' => [Object],
'CognitoIdentityServiceProvider.XXXXXXXXXXXXXXXXXXXXXXXXXX.google_XXXXXXXXXXXXXXXXXXXXX.refreshToken' => [Object],
'CognitoIdentityServiceProvider.XXXXXXXXXXXXXXXXXXXXXXXXXX.google_XXXXXXXXXXXXXXXXXXXXX.clockDrift' => [Object],
},
}
}
As a result, everything on the server (components, route handlers, middleware using request+response) all fail to retrieve the authenticated state. However, once the client initializes and invokes getCurrentUser()
from aws-amplify/auth
it successfully retrieves the logged in user, and the UI reflects this.
My setup is currently working locally and in production using v5.
Locally I'm based in SEA, but the AWS project is us-east-1. I have no idea if that helps or might influence any sort of edge case.
@siphosenkosindhlovu want to circle back on this question from earlier. After signing in an end user, have the auth tokens been written into browser cookie store? Can you share the shape/values of the cookies in your cookie store?
@austinjlaverty, can you also share the shape/value of the cookies in your cookies store as well as clarify which API you're calling when you see this exception happening?
We're still trying to reproduce this on our side, but haven't been able to up to this point.
Yep, they're being written to the browser store. When I use Amplify auth client side, it correctly shows as authenticated, but doesn't for SSR like for @austinjlaverty.
Here's what's logged from the server:
{
session: {
tokens: undefined,
credentials: {
accessKeyId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
secretAccessKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
sessionToken: 'IQoJb3JpZ2luX2VjECgaCXVzLWVhc3QtMSJHMEUCIQDyo9FUB9zOMoZWkdGxOImS1CXC5y70hF/v1YD3/8AXMgIgJNXExtQwZVgtpk17v0JCpjojWmrlwFcecykZ1Gv2jFsq0gUIof//////////ARAEGgw0MDUwOTQ1OTIwNzgiDOZOrKKSwmGFZNJYviqmBawH0IO5k9afRoU7g6cFY1Flbzl3dbB3fNIddHGj3aCHMwG/ptBG+4kmEa0x6dvab1iWaePeJgjcHCl3XKDdHgoT9MboVvvJIYBtRKMydLvixKbeVB0uea3MT38bhKQFYnsUXf1a0CcNexJnQwYi9di6CkScW74p1+2CHZxOKqToWD0GmHf8VvoFcOortgZFfcabMsPOd3PMVvd1IRV5MCJQvx/HmvES9yejOi8KC7fUlbiCpdA9lBzEy3MhN7KKg45H6lggl4ljn8boJZUx12iPuEk3DEp3jhyuQC/5Vnfmr8ogRgNRtqct/OcZhb6PuFD9IuuIvf/x13goMpkKBM/U508dIH+SbubUdcDJR37p0dYwno8IatGDKtDTZHoIUs6UllmFmE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
},
cookies: f {
_parsed: Map(7) {
'activeProfile' => [Object],
'CognitoIdentityServiceProvider.xxxxxxxxxxxxxxxxxxxxxxxxxx.LastAuthUser' => [Object],
'CognitoIdentityServiceProvider.xxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.accessToken' => [Object],
'CognitoIdentityServiceProvider.xxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.idToken' => [Object],
'CognitoIdentityServiceProvider.xxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.refreshToken' => [Object],
'CognitoIdentityServiceProvider.xxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.signInDetails' => [Object],
'CognitoIdentityServiceProvider.xxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.clockDrift' => [Object]
},
_headers: Headers {}
}
}
And from the browser console:
It's only working when deployed to AWS Amplify.
@austinjlaverty and @siphosenkosindhlovu, greatly appreciate the detailed responses and follow up here. While we work on reproducing the issue on our side, are either of you able to provide a public, minimal sample repo or possibly invite to a private repo where this is happening?
@austinjlaverty, is your app also only experiencing this locally (but not when deployed)?
@austinjlaverty and @siphosenkosindhlovu, greatly appreciate the detailed responses and follow up here. While we work on reproducing the issue on our side, are either of you able to provide a public, minimal sample repo or possibly invite to a private repo where this is happening?
Hi, added you to a private. It's a barebones setup with a CSR and SSR component, CSR works but SSR errors out. Not sure if its possible to get more detailed logs to see if it's a network issue.
For what it's worth, my auth on Next.js 15 began failing after upgrading to amplify 6.8.0
. I began to get the error Auth UserPool not configured
. Had to revert back to 6.6.7
.
For what it's worth, my auth on Next.js 15 began failing after upgrading to amplify
6.8.0
. I began to get the errorAuth UserPool not configured
. Had to revert back to6.6.7
.
Next.js 15 is not supported by amplify at the moment if im not mistaken
Before opening, please confirm:
JavaScript Framework
Next.js
Amplify APIs
Authentication
Amplify Version
v6
Amplify Categories
auth
Backend
Amplify CLI
Environment information
Describe the bug
Cannot use Amplify server api categories from NextJS server components on local computer, but works on deployment.
This code:
Throws this errror:
The session object undefined
tokens
anduserSub
fields event though the token is correctly set in the client and viewable on the server.Important to note that everything works normally in client components.
Expected behavior
getCurrentUser()
to return the credentials of the currently authenticated user.Reproduction steps
getCurrentUser()
api category.Code Snippet
Log output
aws-exports.js
No response
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
No response