Skip to content

Commit 3b8dcf9

Browse files
feat: add support for pages router and add apiRoute handler
1 parent 7ce24e3 commit 3b8dcf9

14 files changed

Lines changed: 2565 additions & 264 deletions

src/server/auth-client.test.ts

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { Auth0NextRequest, Auth0NextResponse } from "./http/index.js";
2626
import { StatefulSessionStore } from "./session/stateful-session-store.js";
2727
import { StatelessSessionStore } from "./session/stateless-session-store.js";
2828
import { TransactionState, TransactionStore } from "./transaction-store.js";
29+
import { Auth0Client } from "./client.js";
2930

3031
function createSessionData(sessionData: Partial<SessionData>): SessionData {
3132
return {
@@ -602,7 +603,8 @@ ca/T0LLtgmbMmxSv/MmzIg==
602603
method: "GET"
603604
});
604605
authClient.handleAccessToken = vi.fn().mockResolvedValue({});
605-
const response = await authClient.handler(request);
606+
const fallback = vi.fn().mockResolvedValue(new Auth0NextResponse(NextResponse.next()));
607+
const response = await authClient.handler(request, fallback);
606608
expect(authClient.handleAccessToken).not.toHaveBeenCalled();
607609
// When a route doesn't match, the handler returns a NextResponse.next() with status 200
608610
expect(response.status).toBe(200);
@@ -677,19 +679,8 @@ ca/T0LLtgmbMmxSv/MmzIg==
677679
describe("rolling sessions - no matching auth route", async () => {
678680
it("should update the session expiry if a session exists", async () => {
679681
const secret = await generateSecret(32);
680-
const transactionStore = new TransactionStore({
681-
secret
682-
});
683-
const sessionStore = new StatelessSessionStore({
684-
secret,
685-
686-
rolling: true,
687-
absoluteDuration: 3600,
688-
inactivityDuration: 1800
689-
});
690-
const authClient = new AuthClient({
691-
transactionStore,
692-
sessionStore,
682+
683+
const authClient = new Auth0Client({
693684

694685
domain: DEFAULT.domain,
695686
clientId: DEFAULT.clientId,
@@ -700,7 +691,12 @@ ca/T0LLtgmbMmxSv/MmzIg==
700691

701692
routes: getDefaultRoutes(),
702693

703-
fetch: getMockAuthorizationServer()
694+
fetch: getMockAuthorizationServer(),
695+
session: {
696+
rolling: true,
697+
absoluteDuration: 3600,
698+
inactivityDuration: 1800
699+
}
704700
});
705701

706702
const session: SessionData = {
@@ -728,7 +724,7 @@ ca/T0LLtgmbMmxSv/MmzIg==
728724
}
729725
);
730726

731-
const response = await authClient.handler(request);
727+
const response = await authClient.middleware(request);
732728

733729
// assert session has been updated
734730
const updatedSessionCookie = response.cookies.get("__session");
@@ -760,19 +756,7 @@ ca/T0LLtgmbMmxSv/MmzIg==
760756

761757
it("should pass the request through if there is no session", async () => {
762758
const secret = await generateSecret(32);
763-
const transactionStore = new TransactionStore({
764-
secret
765-
});
766-
const sessionStore = new StatelessSessionStore({
767-
secret,
768-
769-
rolling: true,
770-
absoluteDuration: 3600,
771-
inactivityDuration: 1800
772-
});
773-
const authClient = new AuthClient({
774-
transactionStore,
775-
sessionStore,
759+
const auth0Client = new Auth0Client({
776760

777761
domain: DEFAULT.domain,
778762
clientId: DEFAULT.clientId,
@@ -783,9 +767,17 @@ ca/T0LLtgmbMmxSv/MmzIg==
783767

784768
routes: getDefaultRoutes(),
785769

786-
fetch: getMockAuthorizationServer()
770+
fetch: getMockAuthorizationServer(),
771+
session: {
772+
rolling: true,
773+
absoluteDuration: 3600,
774+
inactivityDuration: 1800
775+
}
787776
});
788777

778+
// Temporary workaround to make test pass without reworking.
779+
const authClient = (auth0Client as any).authClient as AuthClient;
780+
789781
const request = new NextRequest(
790782
"https://example.com/dashboard/projects",
791783
{
@@ -795,7 +787,7 @@ ca/T0LLtgmbMmxSv/MmzIg==
795787

796788
authClient.getTokenSet = vi.fn();
797789

798-
const response = await authClient.handler(request);
790+
const response = await auth0Client.middleware(request);
799791
expect(authClient.getTokenSet).not.toHaveBeenCalled();
800792

801793
// assert session has not been updated
@@ -4247,7 +4239,7 @@ ca/T0LLtgmbMmxSv/MmzIg==
42474239

42484240
expect(mockOnCallback).toHaveBeenCalledWith(
42494241
null,
4250-
expectedContext,
4242+
expect.objectContaining(expectedContext),
42514243
expectedSession
42524244
);
42534245

@@ -4318,7 +4310,7 @@ ca/T0LLtgmbMmxSv/MmzIg==
43184310

43194311
expect(mockOnCallback).toHaveBeenCalledWith(
43204312
expect.any(Error),
4321-
{},
4313+
expect.objectContaining({}),
43224314
null
43234315
);
43244316
expect(mockOnCallback.mock.calls[0][0].code).toEqual("missing_state");
@@ -4403,7 +4395,7 @@ ca/T0LLtgmbMmxSv/MmzIg==
44034395

44044396
expect(mockOnCallback).toHaveBeenCalledWith(
44054397
expect.any(Error),
4406-
{},
4398+
expect.objectContaining({}),
44074399
null
44084400
);
44094401
expect(mockOnCallback.mock.calls[0][0].code).toEqual("invalid_state");
@@ -4488,10 +4480,10 @@ ca/T0LLtgmbMmxSv/MmzIg==
44884480

44894481
expect(mockOnCallback).toHaveBeenCalledWith(
44904482
expect.any(Error),
4491-
{
4483+
expect.objectContaining({
44924484
responseType: RESPONSE_TYPES.CODE,
44934485
returnTo: transactionState.returnTo
4494-
},
4486+
}),
44954487
null
44964488
);
44974489
expect(mockOnCallback.mock.calls[0][0].code).toEqual(
@@ -4580,10 +4572,10 @@ ca/T0LLtgmbMmxSv/MmzIg==
45804572

45814573
expect(mockOnCallback).toHaveBeenCalledWith(
45824574
expect.any(Error),
4583-
{
4575+
expect.objectContaining({
45844576
responseType: RESPONSE_TYPES.CODE,
45854577
returnTo: transactionState.returnTo
4586-
},
4578+
}),
45874579
null
45884580
);
45894581
expect(mockOnCallback.mock.calls[0][0].code).toEqual(
@@ -4671,10 +4663,10 @@ ca/T0LLtgmbMmxSv/MmzIg==
46714663

46724664
expect(mockOnCallback).toHaveBeenCalledWith(
46734665
expect.any(Error),
4674-
{
4666+
expect.objectContaining({
46754667
responseType: RESPONSE_TYPES.CODE,
46764668
returnTo: transactionState.returnTo
4677-
},
4669+
}),
46784670
null
46794671
);
46804672
expect(mockOnCallback.mock.calls[0][0].code).toEqual(
@@ -5253,10 +5245,10 @@ ca/T0LLtgmbMmxSv/MmzIg==
52535245

52545246
expect(mockOnCallback).toHaveBeenCalledWith(
52555247
expect.any(Error),
5256-
{
5248+
expect.objectContaining({
52575249
responseType: RESPONSE_TYPES.CONNECT_CODE,
52585250
returnTo: transactionState.returnTo
5259-
},
5251+
}),
52605252
null
52615253
);
52625254
expect(mockOnCallback.mock.calls[0][0].code).toEqual(
@@ -5374,10 +5366,10 @@ ca/T0LLtgmbMmxSv/MmzIg==
53745366

53755367
expect(mockOnCallback).toHaveBeenCalledWith(
53765368
expect.any(Error),
5377-
{
5369+
expect.objectContaining({
53785370
responseType: RESPONSE_TYPES.CONNECT_CODE,
53795371
returnTo: transactionState.returnTo
5380-
},
5372+
}),
53815373
null
53825374
);
53835375
expect(mockOnCallback.mock.calls[0][0].code).toEqual(
@@ -5498,10 +5490,10 @@ ca/T0LLtgmbMmxSv/MmzIg==
54985490

54995491
expect(mockOnCallback).toHaveBeenCalledWith(
55005492
expect.any(Error),
5501-
{
5493+
expect.objectContaining({
55025494
responseType: RESPONSE_TYPES.CONNECT_CODE,
55035495
returnTo: transactionState.returnTo
5504-
},
5496+
}),
55055497
null
55065498
);
55075499
expect(mockOnCallback.mock.calls[0][0].code).toEqual(

0 commit comments

Comments
 (0)