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

[PR] Creating nextra with auth #2

Merged
merged 75 commits into from
Jul 16, 2024
Merged
Changes from 1 commit
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
32a578b
feat: Intro to README.
LuchoTurtle Jun 18, 2024
988e160
feat: Add set up project.
LuchoTurtle Jun 18, 2024
8ea0574
feat: Adding organized pages.
LuchoTurtle Jun 18, 2024
e709a4f
feat: Finishing up organization.
LuchoTurtle Jun 18, 2024
427cc49
chore: Adding intro to the authentication.
LuchoTurtle Jun 18, 2024
7556c8b
feat: Intro to auth.
LuchoTurtle Jun 18, 2024
c319097
feat: Adding auth configuration.
LuchoTurtle Jun 18, 2024
989c463
feat: Adding script that generates private routes based on `_meta.jso…
LuchoTurtle Jun 19, 2024
088f5e9
refactor: Formatting script.
LuchoTurtle Jun 19, 2024
4ef1fcb
feat: Adding sign in and sign out.
LuchoTurtle Jun 19, 2024
1e1c157
feat: Adding documentation of authentication (missing `generateMiddle…
LuchoTurtle Jun 20, 2024
cf1d82d
chore: Renaming `api_reference` to `reference_api`.
LuchoTurtle Jun 20, 2024
7276c89
fix: Fixing generate private routes script.
LuchoTurtle Jun 21, 2024
f498a58
feat: Adding script to generate routes and documenting it.
LuchoTurtle Jun 21, 2024
11e3d1e
feat: Adding note about error when compiling.
LuchoTurtle Jun 21, 2024
5d2537a
test: Setup Playwright for E2E testing.
LuchoTurtle Jun 21, 2024
96b359a
test: Adding E2E testing infrastructure.
LuchoTurtle Jun 21, 2024
da80741
chore: Deleting test-results folder that shouldn't be versioned in th…
LuchoTurtle Jun 21, 2024
24c66e2
test: Adding E2E tests for authentication with Playwright.
LuchoTurtle Jun 21, 2024
96bd19f
chore: Renaming e2e testing scripts in package.json
LuchoTurtle Jun 21, 2024
b1f0088
fix: Fixing CI
LuchoTurtle Jun 21, 2024
f42de88
fix: Trying to fix env var issue on CI
LuchoTurtle Jun 21, 2024
2f03eff
chore: misc.
LuchoTurtle Jun 24, 2024
ee78b27
feat: Adding component testing with Jest.
LuchoTurtle Jun 25, 2024
b96c0d2
refactor: Moving source files to `src` folder.
LuchoTurtle Jun 25, 2024
6caba61
test: Adding component testing to components.
LuchoTurtle Jun 25, 2024
8ae1cff
test: Adding way to test script for generating private routes.
LuchoTurtle Jun 25, 2024
d09d36e
fix: Fixing jest tests to run mocks differently in-between tests.
LuchoTurtle Jun 26, 2024
9b11135
test: Testing middleware changing script.
LuchoTurtle Jun 26, 2024
0fb44e9
refactor: Refactoring test.
LuchoTurtle Jun 26, 2024
e46f811
test: Testing when file doesn't exist, so it removes from private rou…
LuchoTurtle Jun 26, 2024
3649579
fix: Finishing generating private routes test.
LuchoTurtle Jun 26, 2024
de64ba1
fix: Fixing CI
LuchoTurtle Jun 27, 2024
7c4663c
fix: Fix CI pnpm version install.
LuchoTurtle Jun 27, 2024
c302f70
fix: Trying to fix frozen pnpm-lock in CI.
LuchoTurtle Jun 27, 2024
d81311d
fix: Fix frozen lock file command on CI.
LuchoTurtle Jun 27, 2024
3c9fb4e
fix: Installing next to run webserver when playwright tests execute.
LuchoTurtle Jun 27, 2024
407e7a7
fix: Re-adding next to package.json and fixing CI.
LuchoTurtle Jun 27, 2024
c7229fd
refactor: Renaming test.
LuchoTurtle Jun 27, 2024
21ef858
test: Getting coverage to 100%.
LuchoTurtle Jun 27, 2024
9a6d086
feat: Adding theme from nextra-theme-docks.
LuchoTurtle Jun 28, 2024
350df7c
feat: Successfully adding custom-theme
LuchoTurtle Jul 1, 2024
51f821c
feat: Adding nextra components.
LuchoTurtle Jul 2, 2024
57d871d
feat: Adding missing nextra styles.
LuchoTurtle Jul 2, 2024
75c1ebf
chore: Removing unneeded tailwind imports in global.css
LuchoTurtle Jul 2, 2024
f09c934
feat: Updating README inside `theme` and removing `constants.tsx`
LuchoTurtle Jul 2, 2024
cd3c3ea
fix: Removing `reexported as` not found warning.
LuchoTurtle Jul 2, 2024
c16471f
test: Adding test for `_app`
LuchoTurtle Jul 2, 2024
e70fd0c
refactor: Switching TailwindCSS from theme to root.
LuchoTurtle Jul 3, 2024
680443f
chore: Converting theme config to .tsx
LuchoTurtle Jul 3, 2024
34a6327
chore: Using .tsx config theme
LuchoTurtle Jul 3, 2024
9a4724e
feat: Conditionally rendering in navbar and documenting it.
LuchoTurtle Jul 4, 2024
ce13b7a
chore: Revert role in auth.
LuchoTurtle Jul 4, 2024
22ee53d
fix: Finishing switching "api_reference" to "reference_api"
LuchoTurtle Jul 4, 2024
fd8f09a
feat: Conditionally rendering the sidebar.
LuchoTurtle Jul 4, 2024
27c820f
readme: Documenting sidebar conditional rendering.
LuchoTurtle Jul 4, 2024
4706820
chore: Adding "star repo" CTA.
LuchoTurtle Jul 4, 2024
1123c11
chore: Updating README with new information about why we used the cus…
LuchoTurtle Jul 4, 2024
8794efd
fix: Fixing callout, clipboard and pre components icon imports
LuchoTurtle Jul 5, 2024
120b73d
fix: Fixing production build process.
LuchoTurtle Jul 6, 2024
cd460d9
test: Adding types test.
LuchoTurtle Jul 7, 2024
719aa70
tests: Normalizing tests
LuchoTurtle Jul 7, 2024
8816b89
chore: Adding types check in package.json
LuchoTurtle Jul 7, 2024
b38fe8e
ci: Adding build and unit testing to CI.
LuchoTurtle Jul 7, 2024
29498d3
readme: Adding badges.
LuchoTurtle Jul 7, 2024
5f258b2
chore: Removing erronous ; symbol
LuchoTurtle Jul 8, 2024
00e353c
tweak README.md intro, fix typos & cut excess words #1
nelsonic Jul 8, 2024
110d17f
shift bulk of instructions to tutorial.md #1
nelsonic Jul 13, 2024
853d40b
git mv src/pages/reference_api.mdx src/pages/reference_api/index.mdx #1
nelsonic Jul 13, 2024
ad71967
"user" -> "person" https://github.com/dwyl/app/issues/33 #1
nelsonic Jul 13, 2024
47ff499
remove "user" from person.mdx https://github.com/dwyl/app/issues/33 #1
nelsonic Jul 13, 2024
b25653b
add realistic env var examples to .env.local-example #1
nelsonic Jul 13, 2024
3e1a6f6
tidy page formatting #1
nelsonic Jul 13, 2024
8f692df
split private routes into one-per-line #1
nelsonic Jul 13, 2024
8271442
tidy components #1
nelsonic Jul 13, 2024
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
Prev Previous commit
Next Next commit
test: Adding E2E tests for authentication with Playwright.
LuchoTurtle committed Jun 21, 2024
commit 24c66e29cca0913bfd096ba1f99c46e8e79e06fd
4 changes: 3 additions & 1 deletion .env.local-example
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
AUTH_SECRET=

AUTH_GITHUB_ID=
AUTH_GITHUB_SECRET=
AUTH_GITHUB_SECRET=

TEST_PASSWORD=
63 changes: 47 additions & 16 deletions auth.ts
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@ import GitHub from "next-auth/providers/github";
import NextAuth, { type DefaultSession } from "next-auth";
import { DefaultJWT } from "next-auth/jwt";
import { AdapterSession } from 'next-auth/adapters';
import { Provider } from "next-auth/providers";
import Credentials from "next-auth/providers/credentials";

// We need to add the role to the JWT inside `NextAuth` below, so the `middleware.ts` can have access to it.
// The problem is that it wasn't added this `role` custom field, even if we defined it in `auth.ts`.
@@ -43,23 +45,52 @@ declare module "next-auth/jwt" {

// ----------

export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
GitHub({
profile(profile) {
// GitHub's OAuth apps don't allow you to define roles.
// So `profile` here doesn't have a `role` property.
// But on other providers, you'd add the role here through it.
return {
id: profile.id.toString(),
name: profile.name ?? profile.login,
email: profile.email,
image: profile.avatar_url,
role: "user",
};
// OAuth providers to sign-in
const providers: Provider[] = [
GitHub({
profile(profile) {
// GitHub's OAuth apps don't allow you to define roles.
// So `profile` here doesn't have a `role` property.
// But on other providers, you'd add the role here through it.
return {
id: profile.id.toString(),
name: profile.name ?? profile.login,
email: profile.email,
image: profile.avatar_url,
role: "user",
};
},
}),
]

// Be sure to not put a development version in production!
// See https://authjs.dev/guides/testing#credentials-provider-in-development.
if (process.env.NODE_ENV === "development") {
providers.push(
Credentials({
id: "password",
name: "Password",
credentials: {
password: { label: "Password", type: "password" },
},
authorize: (credentials) => {
if (credentials.password === process.env.TEST_PASSWORD) {
return {
email: "bob@alice.com",
name: "Bob Alice",
image: "",
}
}
return null
},
}),
],
})
)
}


// The important part: where the `NextAuth` config is exported
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: providers,
callbacks: {
jwt({ token, user, account, profile }) {
// Normally, it would be like this
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@
"devDependencies": {
"@playwright/test": "^1.44.1",
"@types/node": "20.14.5",
"dotenv": "^16.4.5",
"fast-glob": "^3.3.2",
"monocart-reporter": "^2.5.0",
"open-cli": "^8.0.0",
5 changes: 3 additions & 2 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -6,8 +6,9 @@ import { getMonocartReporterOptions } from "./playwright.monocart-reporter";
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// import dotenv from 'dotenv';
// dotenv.config({ path: path.resolve(__dirname, '.env') });
import dotenv from 'dotenv';
dotenv.config({ path: "./.env.local" });


const _testResultsDir = path.resolve("./e2e-test-results");
const _codeCoverageDir = path.resolve(_testResultsDir, "code-coverage");
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 0 additions & 18 deletions tests/e2e/homepage.spec.ts

This file was deleted.

55 changes: 55 additions & 0 deletions tests/e2e/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// !! PLEASE READ !!
/*
Coverage for E2E effectively works, but the coverage is not using the sourcemaps properly, so coverage doesn't mean much.
We've decided to leave the infrastructure (coverage uses `monocart`) with its configuration for posteriority in case we need them.
E2E testing is more relevant knowing **if they pass or not**.
*/

import { test, expect } from "../_shared/app-fixtures"; // we use the overriden functions to get coverage

const BASE_URL = "http://localhost:3000"

test.describe("homepage", () => {
test("is mounted", async ({ page }) => {
await page.goto("/");

// Expect a title "to contain" a substring.
await expect(page).toHaveTitle(/Welcome to Nextra Documentation/);
});
});

test("Basic auth", async ({ page, browser }) => {
if (!process.env.TEST_PASSWORD) throw new TypeError("Missing TEST_PASSWORD");

await test.step("should login", async () => {
await page.goto(`${BASE_URL}/api/auth/signin`);
await page.getByLabel("Password").fill(process.env.TEST_PASSWORD ?? "");
await page.getByRole("button", { name: "Sign in with Password" }).click();
await page.waitForURL(`${BASE_URL}`);

const sessionResponse = await page.goto(
`${BASE_URL}/api/auth/session`
)
const session = await sessionResponse?.json() ?? {}
expect(session.user.email).toEqual("bob@alice.com")
expect(session.user.name).toEqual("Bob Alice")
expect(session.user.image).toEqual("")
});

await test.step("should logout", async () => {
// Move to the homepage to log out
await page.goto("/");

await page.getByText("SIGN OUT").click();
expect(page.url()).toBe(`${BASE_URL}/`)

await page.waitForURL(`${BASE_URL}`)
await page.waitForTimeout(1000); // wait for sign out to persist in IDP

const sessionResponse = await page.goto(
`${BASE_URL}/api/auth/session`
)
const session = await sessionResponse?.json()
expect(session).toBe(null)
});
});