Skip to content

Commit

Permalink
4.0.0-beta.4 (#1807)
Browse files Browse the repository at this point in the history
  • Loading branch information
guabu authored Nov 19, 2024
1 parent 18da6fb commit 673495d
Show file tree
Hide file tree
Showing 40 changed files with 4,426 additions and 152 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Playwright Tests
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: lts/*
- name: Install dependencies
run: npm install -g pnpm && pnpm install
- name: Install Playwright Browsers
run: pnpm exec playwright install --with-deps
- name: Run Playwright tests
run: pnpm exec playwright test
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: playwright-report
path: playwright-report/
retention-days: 30
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,8 @@ dist
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
.pnp.*
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
### 1. Install the SDK

```shell
npm i @auth0/nextjs-auth0
npm i @auth0/nextjs-auth0@4.0.0-beta.4
```

### 2. Add the environment variables
Expand Down
103 changes: 103 additions & 0 deletions e2e/app-router.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { expect, test } from "@playwright/test"

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

// fill out Auth0 form
await page.fill('input[id="username"]', "[email protected]")
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!)
await page.getByText("Continue", { exact: true }).click()

// check that the page says "Welcome, [email protected]!"
expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe(
"Welcome, [email protected]!"
)

// ensure we're redirected back to the home page on logout
await page.goto("/auth/logout")
expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe(
"Home"
)

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

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

// fill out Auth0 form
await page.fill('input[id="username"]', "[email protected]")
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!)
await page.getByText("Continue", { exact: true }).click()

// check that the page says "Welcome, [email protected]!"
expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe(
"Welcome, [email protected]!"
)

// ensure we're redirected back to the home page on logout
await page.goto("/auth/logout")
expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe(
"Home"
)

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

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

// fill out Auth0 form
await page.fill('input[id="username"]', "[email protected]")
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!)
await page.getByText("Continue", { exact: true }).click()

// fetch a token
const requestPromise = page.waitForRequest("/auth/access-token")
await page.getByText("Get token").click()
const request = await requestPromise
const tokenRequest = await (await request.response())?.json()
expect(tokenRequest).toHaveProperty("token")
expect(tokenRequest).toHaveProperty("expires_at")
})

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

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

// fill out Auth0 form
await page.fill('input[id="username"]', "[email protected]")
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!)
await page.getByText("Continue", { exact: true }).click()

// after establishing a session, we should receive a 200
const authedRes = await context.request.fetch("/app-router/api/test")
expect(authedRes.status()).toBe(200)
expect(await authedRes.json()).toEqual({ email: "[email protected]" })
})

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

// call protected server action
await page.getByText("Call server action").click()
await expect(page.locator("#status")).toHaveValue("unauthenticated")

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

// fill out Auth0 form
await page.fill('input[id="username"]', "[email protected]")
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!)
await page.getByText("Continue", { exact: true }).click()

// call protected server action, now authenticated
await page.getByText("Call server action").click()
await expect(page.locator("#status")).toHaveValue("authenticated")
})
84 changes: 84 additions & 0 deletions e2e/pages-router.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { expect, test } from "@playwright/test"

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

// fill out Auth0 form
await page.fill('input[id="username"]', "[email protected]")
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!)
await page.getByText("Continue", { exact: true }).click()

// check that the page says "Welcome, [email protected]!"
expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe(
"Welcome, [email protected]!"
)

// ensure we're redirected back to the home page on logout
await page.goto("/auth/logout")
expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe(
"Home"
)

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

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

// fill out Auth0 form
await page.fill('input[id="username"]', "[email protected]")
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!)
await page.getByText("Continue", { exact: true }).click()

// check that the page says "Welcome, [email protected]!"
expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe(
"Welcome, [email protected]!"
)

// ensure we're redirected back to the home page on logout
await page.goto("/auth/logout")
expect(await page.getByRole("heading", { level: 1 }).textContent()).toBe(
"Home"
)

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

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

// fill out Auth0 form
await page.fill('input[id="username"]', "[email protected]")
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!)
await page.getByText("Continue", { exact: true }).click()

// fetch a token
const requestPromise = page.waitForRequest("/auth/access-token")
await page.getByText("Get token").click()
const request = await requestPromise
const tokenRequest = await (await request.response())?.json()
expect(tokenRequest).toHaveProperty("token")
expect(tokenRequest).toHaveProperty("expires_at")
})

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

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

// fill out Auth0 form
await page.fill('input[id="username"]', "[email protected]")
await page.fill('input[id="password"]', process.env.TEST_USER_PASSWORD!)
await page.getByText("Continue", { exact: true }).click()

// after establishing a session, we should receive a 200
const authedRes = await context.request.fetch("/api/pages-router/test")
expect(authedRes.status()).toBe(200)
expect(await authedRes.json()).toEqual({ email: "[email protected]" })
})
3 changes: 3 additions & 0 deletions e2e/test-app/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["next/core-web-vitals", "next/typescript"]
}
40 changes: 40 additions & 0 deletions e2e/test-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/versions

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# env files (can opt-in for committing if needed)
.env*

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
36 changes: 36 additions & 0 deletions e2e/test-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.

This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details.
15 changes: 15 additions & 0 deletions e2e/test-app/app/app-router/action/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use server"

import { auth0 } from "@/lib/auth0"

export async function testServerAction() {
const session = await auth0.getSession()

if (!session) {
return { status: "unauthenticated" }
}

return {
status: "authenticated",
}
}
27 changes: 27 additions & 0 deletions e2e/test-app/app/app-router/action/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use client"

import { useState } from "react"

import { testServerAction } from "./action"

export default function Profile() {
const [status, setStatus] = useState("")

return (
<main>
<input
id="status"
value={status}
onChange={(e) => setStatus(e.target.value)}
></input>
<button
onClick={async () => {
const { status } = await testServerAction()
setStatus(status)
}}
>
Call server action
</button>
</main>
)
}
13 changes: 13 additions & 0 deletions e2e/test-app/app/app-router/api/test/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { NextResponse } from "next/server"

import { auth0 } from "@/lib/auth0"

export async function GET() {
const session = await auth0.getSession()

if (!session) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
}

return NextResponse.json({ email: session.user.email }, { status: 200 })
}
Loading

0 comments on commit 673495d

Please sign in to comment.