Skip to content
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

feat(adapter-nextjs): support next.js 15 #13947

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/push-preid-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
push:
branches:
# Change this to your branch name where "example-preid" corresponds to the preid you want your changes released on
- feat/example-preid-branch/main
- feat/next-15/main

jobs:
e2e:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe('createServerRunner', () => {
});

describe('when nextServerContext is not null', () => {
it('should create auth providers with cookie storage adapter', () => {
it('should create auth providers with cookie storage adapter', async () => {
const operation = jest.fn();
const mockCookieStorageAdapter = {
get: jest.fn(),
Expand All @@ -147,7 +147,7 @@ describe('createServerRunner', () => {
const { runWithAmplifyServerContext } = createServerRunner({
config: mockAmplifyConfig,
});
runWithAmplifyServerContext({
await runWithAmplifyServerContext({
operation,
nextServerContext:
mockNextServerContext as unknown as NextServer.Context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Socket } from 'net';
import { enableFetchMocks } from 'jest-fetch-mock';
import { NextRequest, NextResponse } from 'next/server.js';
import { cookies } from 'next/headers.js';
import { CookieStorage } from 'aws-amplify/adapter-core';

import {
DATE_IN_THE_PAST,
Expand Down Expand Up @@ -46,29 +47,32 @@ describe('createCookieStorageAdapterFromNextServerContext', () => {
describe('cookieStorageAdapter created from NextRequest and NextResponse', () => {
const request = new NextRequest(new URL('https://example.com'));
const response = NextResponse.next();

jest.spyOn(request, 'cookies', 'get').mockImplementation(
() =>
({
get: mockGetFunc,
getAll: mockGetAllFunc,
}) as any,
);

jest.spyOn(response, 'cookies', 'get').mockImplementation(() => ({
set: mockSetFunc,
delete: mockDeleteFunc,
get: jest.fn(),
getAll: jest.fn(),
has: jest.fn(),
}));

const mockContext = {
request,
response,
} as any;
let result: CookieStorage.Adapter;

beforeAll(async () => {
jest.spyOn(request, 'cookies', 'get').mockImplementation(
() =>
({
get: mockGetFunc,
getAll: mockGetAllFunc,
}) as any,
);

const result = createCookieStorageAdapterFromNextServerContext(mockContext);
jest.spyOn(response, 'cookies', 'get').mockImplementation(() => ({
set: mockSetFunc,
delete: mockDeleteFunc,
get: jest.fn(),
getAll: jest.fn(),
has: jest.fn(),
}));

result =
await createCookieStorageAdapterFromNextServerContext(mockContext);
});

it('gets cookie by calling `get` method of the underlying cookie store', () => {
result.get(mockKey);
Expand Down Expand Up @@ -121,26 +125,32 @@ describe('createCookieStorageAdapterFromNextServerContext', () => {
describe('cookieStorageAdapter created from NextRequest and Response', () => {
const request = new NextRequest(new URL('https://example.com'));
const response = new Response();

jest.spyOn(request, 'cookies', 'get').mockImplementation(
() =>
({
get: mockGetFunc,
getAll: mockGetAllFunc,
}) as any,
);
jest.spyOn(response, 'headers', 'get').mockImplementation(
() =>
({
append: mockAppend,
}) as any,
);

const mockContext = {
request,
response,
} as any;

let result: CookieStorage.Adapter;

beforeAll(async () => {
jest.spyOn(request, 'cookies', 'get').mockImplementation(
() =>
({
get: mockGetFunc,
getAll: mockGetAllFunc,
}) as any,
);
jest.spyOn(response, 'headers', 'get').mockImplementation(
() =>
({
append: mockAppend,
}) as any,
);

result =
await createCookieStorageAdapterFromNextServerContext(mockContext);
});

const mockSerializeOptions = {
domain: 'example.com',
expires: new Date('2023-08-22'),
Expand All @@ -150,8 +160,6 @@ describe('createCookieStorageAdapterFromNextServerContext', () => {
path: '/a-path',
};

const result = createCookieStorageAdapterFromNextServerContext(mockContext);

it('gets cookie by calling `get` method of the underlying cookie store', () => {
result.get(mockKey);
expect(mockGetFunc).toHaveBeenCalledWith(mockKey);
Expand Down Expand Up @@ -233,9 +241,15 @@ describe('createCookieStorageAdapterFromNextServerContext', () => {
});

describe('cookieStorageAdapter created from Next cookies function', () => {
mockNextCookiesFunc.mockReturnValueOnce(mockNextCookiesFuncReturn);
let result: CookieStorage.Adapter;

beforeAll(async () => {
mockNextCookiesFunc.mockReturnValueOnce(mockNextCookiesFuncReturn);

const result = createCookieStorageAdapterFromNextServerContext({ cookies });
result = await createCookieStorageAdapterFromNextServerContext({
cookies,
});
});

it('gets cookie by calling `get` method of the underlying cookie store', () => {
result.get(mockKey);
Expand Down Expand Up @@ -286,7 +300,7 @@ describe('createCookieStorageAdapterFromNextServerContext', () => {
});

describe('cookieStorageAdapter created from IncomingMessage and ServerResponse as the Pages Router context', () => {
it('operates with the underlying cookie store', () => {
it('operates with the underlying cookie store', async () => {
const mockCookies = {
key1: 'value1',
key2: 'value2',
Expand All @@ -302,7 +316,7 @@ describe('createCookieStorageAdapterFromNextServerContext', () => {
},
});

const result = createCookieStorageAdapterFromNextServerContext({
const result = await createCookieStorageAdapterFromNextServerContext({
request: request as any,
response,
});
Expand Down Expand Up @@ -341,7 +355,7 @@ describe('createCookieStorageAdapterFromNextServerContext', () => {
]);
});

it('operates with the underlying cookie store with encoded cookie names', () => {
it('operates with the underlying cookie store with encoded cookie names', async () => {
// these the auth keys generated by Amplify
const encodedCookieName1 = encodeURIComponent('[email protected]');
const encodedCookieName2 = encodeURIComponent(
Expand All @@ -364,7 +378,7 @@ describe('createCookieStorageAdapterFromNextServerContext', () => {
},
});

const result = createCookieStorageAdapterFromNextServerContext({
const result = await createCookieStorageAdapterFromNextServerContext({
request: request as any,
response,
});
Expand Down Expand Up @@ -413,7 +427,7 @@ describe('createCookieStorageAdapterFromNextServerContext', () => {
]);
});

it('does not add duplicate cookies when the cookies are defined in the response Set-Cookie headers', () => {
it('does not add duplicate cookies when the cookies are defined in the response Set-Cookie headers', async () => {
const mockExistingSetCookieValues = [
'CognitoIdentityServiceProvider.1234.accessToken=1234;Path=/',
'CognitoIdentityServiceProvider.1234.refreshToken=1234;Path=/',
Expand All @@ -433,7 +447,7 @@ describe('createCookieStorageAdapterFromNextServerContext', () => {

getHeaderSpy.mockReturnValue(mockExistingSetCookieValues);

const result = createCookieStorageAdapterFromNextServerContext({
const result = await createCookieStorageAdapterFromNextServerContext({
request: request as any,
response,
});
Expand All @@ -455,6 +469,6 @@ describe('createCookieStorageAdapterFromNextServerContext', () => {
request: undefined,
response: new ServerResponse({} as any),
} as any),
).toThrow();
).rejects.toThrow();
});
});
2 changes: 1 addition & 1 deletion packages/adapter-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "The adapter for the supporting of using Amplify APIs in Next.js.",
"peerDependencies": {
"aws-amplify": "^6.0.7",
"next": ">=13.5.0 <15.0.0"
"next": ">=13.5.0 <16.0.0"
},
"dependencies": {
"aws-jwt-verify": "^4.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { NextServer } from '../types';

export const DATE_IN_THE_PAST = new Date(0);

export const createCookieStorageAdapterFromNextServerContext = (
export const createCookieStorageAdapterFromNextServerContext = async (
context: NextServer.Context,
): CookieStorage.Adapter => {
): Promise<CookieStorage.Adapter> => {
const { request: req, response: res } =
context as Partial<NextServer.GetServerSidePropsContext>;

Expand Down Expand Up @@ -110,10 +110,10 @@ const createCookieStorageAdapterFromNextRequestAndHttpResponse = (
};
};

const createCookieStorageAdapterFromNextCookies = (
const createCookieStorageAdapterFromNextCookies = async (
cookies: NextServer.ServerComponentContext['cookies'],
): CookieStorage.Adapter => {
const cookieStore = cookies();
): Promise<CookieStorage.Adapter> => {
const cookieStore = await cookies();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😔


// When Next cookies() is called in a server component, it returns a readonly
// cookie store. Hence calling set and delete throws an error. However,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const createRunWithAmplifyServerContext = ({
nextServerContext === null
? sharedInMemoryStorage
: createKeyValueStorageFromCookieStorageAdapter(
createCookieStorageAdapterFromNextServerContext(
await createCookieStorageAdapterFromNextServerContext(
nextServerContext,
),
createTokenValidator({
Expand Down
Loading