Skip to content

Custom userPoolEndpoint does not take effect in Next.js adapter and custom identityPoolEndpoint is unsupported #13650

@tonestrike

Description

@tonestrike

Is this related to a new or existing framework?

Next.js

Is this related to a new or existing API?

Authentication

Is this related to another service?

No response

Describe the feature you'd like to request

This is my configuration:

{
	Auth: {
		Cognito: {
			userPoolEndpoint: process.env.NEXT_PUBLIC_COGNITO_POOL_ENDPOINT,
			userPoolId: process.env.NEXT_PUBLIC_COGNITO_USER_POOL_ID!,
			identityPoolId: process.env.NEXT_PUBLIC_COGNITO_IDENTITY_POOL_ID!,
			userPoolClientId: process.env.NEXT_PUBLIC_COGNITO_CLIENT_ID!,
			signUpVerificationMethod: 'code',
			allowGuestAccess: true,
			loginWith: {
				email: true,
			},
		},
	},
}

My userPoolEndpoint points to LocalStack. Everything works correctly on the client side, but on the server side, Amplify is not able to parse the cookies and return the current user.

If I use an AWS hosted Cognito User Pool, everything works as expected. My assumption is that userPoolEndpoint is not supported on the server side.

This is the error:

[ResourceNotFoundException: IdentityPool 'us-east-1:7b676de5-ed4a-4750-95f5-223e8d95102d' not found.] {
  name: 'ResourceNotFoundException',
  $metadata: {
  attempts: 1,
  httpStatusCode: 400,
  requestId: '58fa10e9-e0be-4a81-b785-7611deed288c',
  extendedRequestId: undefined,
  cfId: undefined
}

This is my middleware:


export async function middleware(request: NextRequest) {
	const response = NextResponse.next()

	const authenticated = await runWithAmplifyServerContext({
		nextServerContext: { request, response },
		operation: async (contextSpec) => {
			try {
				const session = await fetchAuthSession(contextSpec)

				return session.tokens?.accessToken !== undefined && session.tokens?.idToken !== undefined
			} catch (error) {
				console.log(error)
				return false
			}
		},
	})

	if (authRoutes.includes(request.nextUrl.pathname)) {
		if (authenticated) {
			return NextResponse.redirect(new URL('/', request.url))
		}

		return response
	}

	if (authenticated) {
		return response
	}

	return NextResponse.redirect(new URL('/login', request.url))
}

Describe the solution you'd like

Support userPoolEndpoint on the server side using @aws-amplify/adapter-nextjs.

Describe alternatives you've considered

Looked into work arounds and could not find one.

Additional context

No response

Is this something that you'd be interested in working on?

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

Metadata

Metadata

Assignees

No one assigned

    Labels

    Next.jsbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions