Skip to content

Commit 9e802b0

Browse files
chore(example): add @auth/express example app (#9570)
* feat: add @auth/express dev app * test: add signin e2e spec * chore: update pnpm-lock * refactor: move express app to examples * chore: rename files plus load env before import app * chore: update signed in layout banner * chore: update pnpm-lock * chore: add comment for credentials validator * fix: missing pug view engine * chore: cleanup server callbacks * chore: update dev concurrently tags * docs: standardize express auth example readme * chore: update error handler errors * test: no e2e in example * chore: cleanup scripts * docs: add section on node.js compatibility to express example * chore: remove credentials provider * docs: add comment on rate-limiting for authentication routes * chore: use AuthConfig type * chore: add node engine plus remove experimental flags * refactor: cleanup globals * chore: tsconfig module + moduleResolution -> nodenext to support cjs require * docs: remove experimental fetch discussion and include crypto polyfill * chore: temp include @auth/core dep * chore: remove name and version * chore: update gitignore * docs: update express img link * chore: add node types * feat: vercel ready express example * chore: cleanup * docs: update host copy * chore: remove @auth/core dependency * chore: add viewport meta * feat: sync example app --------- Co-authored-by: Nico Domino <[email protected]>
1 parent 0ddb8cc commit 9e802b0

25 files changed

+497
-19
lines changed

.github/sync.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
nextauthjs/express-auth-example:
2+
- source: apps/examples/express
3+
dest: .
4+
deleteOrphaned: true
5+
- .github/FUNDING.yml
6+
- LICENSE
7+
18
nextauthjs/sveltekit-auth-example:
29
- source: apps/examples/sveltekit
310
dest: .

apps/examples/express/.env.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
AUTH_SECRET=
2+
3+
AUTH_GITHUB_ID=
4+
AUTH_GITHUB_SECRET=
5+
6+
AUTH_GOOGLE_ID=
7+
AUTH_GOOGLE_SECRET=
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module.exports = {
2+
root: true,
3+
env: {
4+
browser: true,
5+
es2021: true,
6+
},
7+
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
8+
overrides: [
9+
{
10+
env: {
11+
node: true,
12+
},
13+
files: [".eslintrc.{js,cjs}"],
14+
parserOptions: {
15+
sourceType: "script",
16+
},
17+
},
18+
],
19+
parser: "@typescript-eslint/parser",
20+
parserOptions: {
21+
ecmaVersion: "latest",
22+
sourceType: "module",
23+
},
24+
plugins: ["@typescript-eslint"],
25+
rules: {},
26+
}

apps/examples/express/.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# API keys and secrets
2+
.env
3+
4+
# Dependency directory
5+
node_modules
6+
7+
# Editors
8+
.idea
9+
*.iml
10+
.vscode/settings.json
11+
12+
# OS metadata
13+
.DS_Store
14+
Thumbs.db
15+
16+
# Ignore built ts files
17+
dist/**/*
18+
19+
# Ignore built css files
20+
/public/css/output.css
21+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
.DS_Store
3+
node_modules
4+
/dist
5+
/.turbo
6+
/package
7+
.env
8+
.env.*
9+
!.env.example
10+
11+
# Ignore files for PNPM, NPM and YARN
12+
pnpm-lock.yaml
13+
package-lock.json
14+
yarn.lock

apps/examples/express/.prettierrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"semi": false,
3+
"plugins": ["@prettier/plugin-pug", "prettier-plugin-tailwindcss"],
4+
"pugClassNotation": "attribute"
5+
}

apps/examples/express/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
> The example repository is maintained from a [monorepo](https://github.com/nextauthjs/next-auth/tree/main/apps/examples/express). Pull Requests should be opened against [`nextauthjs/next-auth`](https://github.com/nextauthjs/next-auth).
2+
3+
<p align="center">
4+
<br/>
5+
<a href="https://authjs.dev" target="_blank">
6+
<img height="64" src="https://authjs.dev/img/logo/logo-sm.png" />
7+
</a>
8+
<a href="https://expressjs.com" target="_blank">
9+
<img height="64" src="https://i.cloudup.com/zfY6lL7eFa-3000x3000.png" />
10+
</a>
11+
<h3 align="center"><b>Express Auth</b> - Example App</h3>
12+
<p align="center">
13+
Open Source. Full Stack. Own Your Data.
14+
</p>
15+
<p align="center" style="align: center;">
16+
<a href="https://npm.im/@auth/express">
17+
<img alt="npm" src="https://img.shields.io/npm/v/@auth/express?color=green&label=@auth/express&style=flat-square">
18+
</a>
19+
<a href="https://bundlephobia.com/result?p=@auth/express">
20+
<img src="https://img.shields.io/bundlephobia/minzip/@auth/express?label=size&style=flat-square" alt="Bundle Size"/>
21+
</a>
22+
<a href="https://www.npmtrends.com/@auth/express">
23+
<img src="https://img.shields.io/npm/dm/@auth/express?label=downloads&style=flat-square" alt="Downloads" />
24+
</a>
25+
<a href="https://npm.im/@auth/express">
26+
<img src="https://img.shields.io/badge/TypeScript-blue?style=flat-square" alt="TypeScript" />
27+
</a>
28+
</p>
29+
</p>
30+
31+
## Overview
32+
33+
This is the official Express Auth example for [Auth.js](https://express.authjs.dev).
34+
35+
## Getting started
36+
37+
You can instantly deploy this example to [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=express-auth-example) by clicking the following button.
38+
39+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/nextauthjs/express-auth-example&project-name=express-auth-example&repository-name=express-auth-example)
40+
41+
## Environment Variables
42+
43+
Once deployed, kindly ensure you set all [required environment variables](https://authjs.dev/getting-started/deployment#environment-variables) in the `Environment` section of your hosting service.
44+
45+
## Node.js Compatibility
46+
47+
The recommended version of Node.js to use in this example is Node.js v20.0.0.
48+
49+
If you are using a version of Node.js lower than this (for example the minimum supported node version v18.0.0), you may need to enable Web Crypto API via the `--experimental-webcrypto` flag in the `start` and `dev` scripts of your `package.json` file.
50+
51+
Instead of using the experimental flag, you may use the following polyfill:
52+
53+
```ts
54+
// polyfill.cts
55+
globalThis.crypto ??= require("crypto").webcrypto
56+
```
57+
58+
And then import it within a top-level file in the application:
59+
60+
```ts
61+
// server.ts
62+
import "./polyfill.cjs"
63+
```

apps/examples/express/api/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import "../src/globals.js"
2+
3+
const { app } = await import("../src/app.js")
4+
5+
export default app

apps/examples/express/package.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"type": "module",
3+
"description": "Express Auth example app",
4+
"engines": {
5+
"node": ">=20.0.0"
6+
},
7+
"private": true,
8+
"scripts": {
9+
"start": "node dist/server.js",
10+
"build": "pnpm build:ts && pnpm build:css",
11+
"build:ts": "tsc",
12+
"build:css": "tailwindcss -i ./public/css/style.css -o ./public/css/output.css",
13+
"build:watch": "pnpm build:ts -w & pnpm build:css -w",
14+
"dev": "concurrently -k -p \"[{name}]\" -n \"Build,Node\" -c \"cyan.bold,green.bold\" \"pnpm run build:watch\" \"pnpm run dev:node\"",
15+
"dev:node": "nodemon dist/server.js",
16+
"lint": "eslint src/*.ts --fix",
17+
"prettier": "prettier src/*.ts --write"
18+
},
19+
"author": "Rexford Essilfie <[email protected]>",
20+
"license": "MIT",
21+
"dependencies": {
22+
"@auth/express": "latest",
23+
"dotenv": "^16.3.1",
24+
"express": "^4.18.2",
25+
"morgan": "^1.10.0",
26+
"pug": "^3.0.2",
27+
"tailwindcss": "^3.4.1"
28+
},
29+
"devDependencies": {
30+
"@prettier/plugin-pug": "^3.0.0",
31+
"@types/express": "^4.17.21",
32+
"@types/morgan": "^1.9.9",
33+
"@types/node": "^20.10.7",
34+
"@types/pug": "^2.0.10",
35+
"@typescript-eslint/eslint-plugin": "^6.18.0",
36+
"@typescript-eslint/parser": "^6.18.0",
37+
"concurrently": "6.5.1",
38+
"eslint": "^8.56.0",
39+
"nodemon": "2.0.22",
40+
"prettier": "3.1.1",
41+
"prettier-plugin-tailwindcss": "^0.5.11",
42+
"typescript": "5.3.3"
43+
}
44+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@tailwind base;
2+
3+
@tailwind components;
4+
5+
@tailwind utilities;

0 commit comments

Comments
 (0)