Skip to content

Commit 37df4ce

Browse files
Merge branch 'main' into chore/filter-claims-in-before-session-saved
2 parents ced6b94 + 0bc4774 commit 37df4ce

File tree

3 files changed

+128
-126
lines changed

3 files changed

+128
-126
lines changed

EXAMPLES.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,12 +533,12 @@ When calling `getAccessToken` without request and response objects, you can pass
533533

534534
```typescript
535535
// app/api/my-api/route.ts
536-
import { getAccessToken } from '@auth0/nextjs-auth0';
536+
import { auth0 } from "@/lib/auth0"
537537

538538
export async function GET() {
539539
try {
540540
// Force a refresh of the access token
541-
const { token, expiresAt } = await getAccessToken({ refresh: true });
541+
const { token, expiresAt } = await auth0.getAccessToken({ refresh: true });
542542

543543
// Use the refreshed token
544544
// ...

e2e/app-router.spec.ts

Lines changed: 69 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,135 @@
1-
import { expect, test } from "@playwright/test"
1+
import { expect, test } from "@playwright/test";
22

33
test("getSession()", async ({ page }) => {
4-
await page.goto("/auth/login?returnTo=/app-router/server")
4+
await page.goto("/auth/login?returnTo=/app-router/server");
55

66
// fill out Auth0 form
7-
await page.fill('input[id="username"]', "test@example.com")
8-
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!)
9-
await page.getByText("Continue", { exact: true }).click()
7+
await page.fill('input[id="username"]', "test@example.com");
8+
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!);
9+
await page.getByText("Continue", { exact: true }).click();
1010

1111
// check that the page says "Welcome, test@example.com!"
1212
expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe(
1313
"Welcome, test@example.com!"
14-
)
14+
);
1515

1616
// ensure we're redirected back to the home page on logout
17-
await page.goto("/auth/logout")
17+
await page.goto("/auth/logout");
1818
expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe(
1919
"Home"
20-
)
20+
);
2121

2222
// check that `getSession()` returns null after logging out
23-
await page.goto("/app-router/server")
24-
expect(page.getByRole("link", { name: "Log in" })).toBeVisible()
25-
})
23+
await page.goto("/app-router/server");
24+
expect(page.getByRole("link", { name: "Log in" })).toBeVisible();
25+
});
2626

2727
test("useUser()", async ({ page }) => {
28-
await page.goto("/auth/login?returnTo=/app-router/client")
28+
await page.goto("/auth/login?returnTo=/app-router/client");
2929

3030
// fill out Auth0 form
31-
await page.fill('input[id="username"]', "test@example.com")
32-
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!)
33-
await page.getByText("Continue", { exact: true }).click()
31+
await page.fill('input[id="username"]', "test@example.com");
32+
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!);
33+
await page.getByText("Continue", { exact: true }).click();
3434

3535
// check that the page says "Welcome, test@example.com!"
3636
expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe(
3737
"Welcome, test@example.com!"
38-
)
38+
);
3939

4040
// ensure we're redirected back to the home page on logout
41-
await page.goto("/auth/logout")
41+
await page.goto("/auth/logout");
4242
expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe(
4343
"Home"
44-
)
44+
);
4545

4646
// check that `getSession()` returns null after logging out
47-
await page.goto("/app-router/client")
48-
expect(page.getByRole("link", { name: "Log in" })).toBeVisible()
49-
})
47+
await page.goto("/app-router/client");
48+
expect(page.getByRole("link", { name: "Log in" })).toBeVisible();
49+
});
5050

5151
test("getAccessToken()", async ({ page }) => {
52-
await page.goto("/auth/login?returnTo=/app-router/client")
52+
await page.goto("/auth/login?returnTo=/app-router/client");
5353

5454
// fill out Auth0 form
55-
await page.fill('input[id="username"]', "test@example.com")
56-
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!)
57-
await page.getByText("Continue", { exact: true }).click()
55+
await page.fill('input[id="username"]', "test@example.com");
56+
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!);
57+
await page.getByText("Continue", { exact: true }).click();
5858

5959
// fetch a token
60-
const requestPromise = page.waitForRequest("/auth/access-token")
61-
await page.getByText("Get token").click()
62-
const request = await requestPromise
63-
const tokenRequest = await (await request.response())?.json()
64-
expect(tokenRequest).toHaveProperty("token")
65-
expect(tokenRequest).toHaveProperty("expires_at")
66-
})
60+
const requestPromise = page.waitForRequest("/auth/access-token");
61+
await page.getByText("Get token").click();
62+
const request = await requestPromise;
63+
const tokenRequest = await (await request.response())?.json();
64+
expect(tokenRequest).toHaveProperty("token");
65+
expect(tokenRequest).toHaveProperty("expires_at");
66+
});
6767

6868
test("protected server route", async ({ page, context }) => {
6969
// before establishing a session, we should receive a 401
70-
const unauthedRes = await context.request.fetch("/app-router/api/test")
71-
expect(unauthedRes.status()).toBe(401)
70+
const unauthedRes = await context.request.fetch("/app-router/api/test");
71+
expect(unauthedRes.status()).toBe(401);
7272

73-
await page.goto("/auth/login?returnTo=/app-router/server")
73+
await page.goto("/auth/login?returnTo=/app-router/server");
7474

7575
// fill out Auth0 form
76-
await page.fill('input[id="username"]', "test@example.com")
77-
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!)
78-
await page.getByText("Continue", { exact: true }).click()
76+
await page.fill('input[id="username"]', "test@example.com");
77+
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!);
78+
await page.getByText("Continue", { exact: true }).click();
7979

8080
// after establishing a session, we should receive a 200
81-
const authedRes = await context.request.fetch("/app-router/api/test")
82-
expect(authedRes.status()).toBe(200)
83-
expect(await authedRes.json()).toEqual({ email: "test@example.com" })
84-
})
81+
const authedRes = await context.request.fetch("/app-router/api/test");
82+
expect(authedRes.status()).toBe(200);
83+
expect(await authedRes.json()).toEqual({ email: "test@example.com" });
84+
});
8585

8686
test("protected server action", async ({ page }) => {
87-
await page.goto("/app-router/action")
87+
await page.goto("/app-router/action");
8888

8989
// call protected server action
90-
await page.getByText("Call server action").click()
91-
await expect(page.locator("#status")).toHaveValue("unauthenticated")
90+
await page.getByText("Call server action").click();
91+
await expect(page.locator("#status")).toHaveValue("unauthenticated");
9292

93-
await page.goto("/auth/login?returnTo=/app-router/action")
93+
await page.goto("/auth/login?returnTo=/app-router/action");
9494

9595
// fill out Auth0 form
96-
await page.fill('input[id="username"]', "test@example.com")
97-
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!)
98-
await page.getByText("Continue", { exact: true }).click()
96+
await page.fill('input[id="username"]', "test@example.com");
97+
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!);
98+
await page.getByText("Continue", { exact: true }).click();
9999

100100
// call protected server action, now authenticated
101-
await page.getByText("Call server action").click()
102-
await expect(page.locator("#status")).toHaveValue("authenticated")
103-
})
101+
await page.getByText("Call server action").click();
102+
await expect(page.locator("#status")).toHaveValue("authenticated");
103+
});
104104

105105
test("updateSession()", async ({ page, context }) => {
106-
const now = Date.now()
106+
const now = Date.now();
107107

108-
await page.goto("/auth/login?returnTo=/app-router/server")
108+
await page.goto("/auth/login?returnTo=/app-router/server");
109109

110110
// fill out Auth0 form
111-
await page.fill('input[id="username"]', "test@example.com")
112-
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!)
113-
await page.getByText("Continue", { exact: true }).click()
111+
await page.fill('input[id="username"]', "test@example.com");
112+
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!);
113+
await page.getByText("Continue", { exact: true }).click();
114114

115115
// check that the page says "Welcome, test@example.com!"
116116
expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe(
117117
"Welcome, test@example.com!"
118-
)
118+
);
119119

120120
// the session should not have an `updatedAt` field initially
121-
let getSessionRes = await context.request.fetch("/app-router/api/get-session")
122-
let getSessionJson = await getSessionRes.json()
123-
expect(getSessionJson.updatedAt).toBeUndefined()
121+
let getSessionRes = await context.request.fetch(
122+
"/app-router/api/get-session"
123+
);
124+
let getSessionJson = await getSessionRes.json();
125+
expect(getSessionJson.updatedAt).toBeUndefined();
124126

125127
// now update the session and check that the `updatedAt` field is updated
126128
const updateSessionRes = await context.request.fetch(
127129
"/app-router/api/update-session"
128-
)
129-
expect(updateSessionRes.status()).toBe(200)
130-
getSessionRes = await context.request.fetch("/app-router/api/get-session")
131-
getSessionJson = await getSessionRes.json()
132-
expect(getSessionJson.updatedAt).toBeGreaterThan(now)
133-
})
130+
);
131+
expect(updateSessionRes.status()).toBe(200);
132+
getSessionRes = await context.request.fetch("/app-router/api/get-session");
133+
getSessionJson = await getSessionRes.json();
134+
expect(getSessionJson.updatedAt).toBeGreaterThan(now);
135+
});

e2e/pages-router.spec.ts

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,116 @@
1-
import { expect, test } from "@playwright/test"
1+
import { expect, test } from "@playwright/test";
22

33
test("getSession()", async ({ page }) => {
4-
await page.goto("/auth/login?returnTo=/pages-router/server")
4+
await page.goto("/auth/login?returnTo=/pages-router/server");
55

66
// fill out Auth0 form
7-
await page.fill('input[id="username"]', "test@example.com")
8-
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!)
9-
await page.getByText("Continue", { exact: true }).click()
7+
await page.fill('input[id="username"]', "test@example.com");
8+
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!);
9+
await page.getByText("Continue", { exact: true }).click();
1010

1111
// check that the page says "Welcome, test@example.com!"
1212
expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe(
1313
"Welcome, test@example.com!"
14-
)
14+
);
1515

1616
// ensure we're redirected back to the home page on logout
17-
await page.goto("/auth/logout")
17+
await page.goto("/auth/logout");
1818
expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe(
1919
"Home"
20-
)
20+
);
2121

2222
// check that `getSession()` returns null after logging out
23-
await page.goto("/pages-router/server")
24-
expect(page.getByRole("link", { name: "Log in" })).toBeVisible()
25-
})
23+
await page.goto("/pages-router/server");
24+
expect(page.getByRole("link", { name: "Log in" })).toBeVisible();
25+
});
2626

2727
test("useUser()", async ({ page }) => {
28-
await page.goto("/auth/login?returnTo=/pages-router/client")
28+
await page.goto("/auth/login?returnTo=/pages-router/client");
2929

3030
// fill out Auth0 form
31-
await page.fill('input[id="username"]', "test@example.com")
32-
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!)
33-
await page.getByText("Continue", { exact: true }).click()
31+
await page.fill('input[id="username"]', "test@example.com");
32+
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!);
33+
await page.getByText("Continue", { exact: true }).click();
3434

3535
// check that the page says "Welcome, test@example.com!"
3636
expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe(
3737
"Welcome, test@example.com!"
38-
)
38+
);
3939

4040
// ensure we're redirected back to the home page on logout
41-
await page.goto("/auth/logout")
41+
await page.goto("/auth/logout");
4242
expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe(
4343
"Home"
44-
)
44+
);
4545

4646
// check that `getSession()` returns null after logging out
47-
await page.goto("/pages-router/client")
48-
expect(page.getByRole("link", { name: "Log in" })).toBeVisible()
49-
})
47+
await page.goto("/pages-router/client");
48+
expect(page.getByRole("link", { name: "Log in" })).toBeVisible();
49+
});
5050

5151
test("getAccessToken()", async ({ page }) => {
52-
await page.goto("/auth/login?returnTo=/pages-router/client")
52+
await page.goto("/auth/login?returnTo=/pages-router/client");
5353

5454
// fill out Auth0 form
55-
await page.fill('input[id="username"]', "test@example.com")
56-
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!)
57-
await page.getByText("Continue", { exact: true }).click()
55+
await page.fill('input[id="username"]', "test@example.com");
56+
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!);
57+
await page.getByText("Continue", { exact: true }).click();
5858

5959
// fetch a token
60-
const requestPromise = page.waitForRequest("/auth/access-token")
61-
await page.getByText("Get token").click()
62-
const request = await requestPromise
63-
const tokenRequest = await (await request.response())?.json()
64-
expect(tokenRequest).toHaveProperty("token")
65-
expect(tokenRequest).toHaveProperty("expires_at")
66-
})
60+
const requestPromise = page.waitForRequest("/auth/access-token");
61+
await page.getByText("Get token").click();
62+
const request = await requestPromise;
63+
const tokenRequest = await (await request.response())?.json();
64+
expect(tokenRequest).toHaveProperty("token");
65+
expect(tokenRequest).toHaveProperty("expires_at");
66+
});
6767

6868
test("protected API route", async ({ page, request, context }) => {
6969
// before establishing a session, we should receive a 401
70-
const unauthedRes = await context.request.fetch("/api/pages-router/test")
71-
expect(unauthedRes.status()).toBe(401)
70+
const unauthedRes = await context.request.fetch("/api/pages-router/test");
71+
expect(unauthedRes.status()).toBe(401);
7272

73-
await page.goto("/auth/login?returnTo=/pages-router/server")
73+
await page.goto("/auth/login?returnTo=/pages-router/server");
7474

7575
// fill out Auth0 form
76-
await page.fill('input[id="username"]', "test@example.com")
77-
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!)
78-
await page.getByText("Continue", { exact: true }).click()
76+
await page.fill('input[id="username"]', "test@example.com");
77+
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!);
78+
await page.getByText("Continue", { exact: true }).click();
7979

8080
// after establishing a session, we should receive a 200
81-
const authedRes = await context.request.fetch("/api/pages-router/test")
82-
expect(authedRes.status()).toBe(200)
83-
expect(await authedRes.json()).toEqual({ email: "test@example.com" })
84-
})
81+
const authedRes = await context.request.fetch("/api/pages-router/test");
82+
expect(authedRes.status()).toBe(200);
83+
expect(await authedRes.json()).toEqual({ email: "test@example.com" });
84+
});
8585

8686
test("updateSession()", async ({ page, context }) => {
87-
const now = Date.now()
87+
const now = Date.now();
8888

89-
await page.goto("/auth/login?returnTo=/pages-router/server")
89+
await page.goto("/auth/login?returnTo=/pages-router/server");
9090

9191
// fill out Auth0 form
92-
await page.fill('input[id="username"]', "test@example.com")
93-
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!)
94-
await page.getByText("Continue", { exact: true }).click()
92+
await page.fill('input[id="username"]', "test@example.com");
93+
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!);
94+
await page.getByText("Continue", { exact: true }).click();
9595

9696
// check that the page says "Welcome, test@example.com!"
9797
expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe(
9898
"Welcome, test@example.com!"
99-
)
99+
);
100100

101101
// the session should not have an `updatedAt` field initially
102102
let getSessionRes = await context.request.fetch(
103103
"/api/pages-router/get-session"
104-
)
105-
let getSessionJson = await getSessionRes.json()
106-
expect(getSessionJson.updatedAt).toBeUndefined()
104+
);
105+
let getSessionJson = await getSessionRes.json();
106+
expect(getSessionJson.updatedAt).toBeUndefined();
107107

108108
// now update the session and check that the `updatedAt` field is updated
109109
const updateSessionRes = await context.request.fetch(
110110
"/api/pages-router/update-session"
111-
)
112-
expect(updateSessionRes.status()).toBe(200)
113-
getSessionRes = await context.request.fetch("/api/pages-router/get-session")
114-
getSessionJson = await getSessionRes.json()
115-
expect(getSessionJson.updatedAt).toBeGreaterThan(now)
116-
})
111+
);
112+
expect(updateSessionRes.status()).toBe(200);
113+
getSessionRes = await context.request.fetch("/api/pages-router/get-session");
114+
getSessionJson = await getSessionRes.json();
115+
expect(getSessionJson.updatedAt).toBeGreaterThan(now);
116+
});

0 commit comments

Comments
 (0)