Skip to content

Commit a34d1cf

Browse files
authored
chore(repo): Add Express SDK E2E tests (#6499)
1 parent 9084759 commit a34d1cf

File tree

18 files changed

+198
-194
lines changed

18 files changed

+198
-194
lines changed

.changeset/eight-walls-flash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/express': patch
3+
---
4+
5+
Deprecates `enableHandshake` option in `clerkMiddleware`. This option is unnecessary for API requests since they don't trigger handshake flows. The option will be removed in a future version.

integration/presets/express.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import { linkPackage } from './utils';
66
const vite = applicationConfig()
77
.setName('express-vite')
88
.useTemplate(templates['express-vite'])
9+
.setEnvFormatter('public', key => `VITE_${key}`)
910
.addScript('setup', 'pnpm install')
1011
.addScript('dev', 'pnpm dev')
1112
.addScript('build', 'pnpm build')
1213
.addScript('serve', 'pnpm start')
13-
.addDependency('@clerk/express', constants.E2E_CLERK_VERSION || linkPackage('express'));
14+
.addDependency('@clerk/express', constants.E2E_CLERK_VERSION || linkPackage('express'))
15+
.addDependency('@clerk/clerk-js', constants.E2E_CLERK_VERSION || linkPackage('clerk-js'));
1416

1517
export const express = {
1618
vite,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite + TS + Express</title>
8+
</head>
9+
<body>
10+
<div id="app"></div>
11+
<script type="module" src="/src/client/main.ts"></script>
12+
</body>
13+
</html>

integration/templates/express-vite/package.json

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,20 @@
44
"private": true,
55
"scripts": {
66
"build": "vite build",
7-
"dev": "PORT=$PORT ts-node src/server/main.ts",
8-
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
7+
"dev": "PORT=$PORT tsx src/server/main.ts",
98
"preview": "vite preview --port $PORT --no-open",
10-
"start": "PORT=$PORT ts-node src/server/main.ts"
9+
"start": "PORT=$PORT NODE_ENV=production tsx src/server/main.ts"
1110
},
1211
"dependencies": {
13-
"dotenv": "^16.4.7",
14-
"ejs": "^3.1.6",
15-
"express": "^4.18.2",
16-
"ts-node": "^10.9.1",
17-
"vite-express": "^0.20.0"
12+
"dotenv": "^17.2.1",
13+
"express": "^5.1.0",
14+
"tsx": "^4.20.3",
15+
"vite-express": "^0.21.1"
1816
},
1917
"devDependencies": {
20-
"@types/express": "^4.17.21",
21-
"@types/node": "^20.9.3",
22-
"typescript": "^5.7.3",
23-
"vite": "^5.0.2"
18+
"@types/express": "^5.0.3",
19+
"@types/node": "^24.2.1",
20+
"typescript": "^5.8.3",
21+
"vite": "^6.3.3"
2422
}
2523
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Clerk } from '@clerk/clerk-js';
2+
3+
const publishableKey = import.meta.env.VITE_CLERK_PUBLISHABLE_KEY;
4+
5+
document.addEventListener('DOMContentLoaded', async function () {
6+
const clerk = new Clerk(publishableKey);
7+
await clerk.load();
8+
9+
if (clerk.isSignedIn) {
10+
document.getElementById('app')!.innerHTML = `
11+
<div id="user-button"></div>
12+
`;
13+
14+
const userButtonDiv = document.getElementById('user-button');
15+
16+
clerk.mountUserButton(userButtonDiv);
17+
} else {
18+
document.getElementById('app')!.innerHTML = `
19+
<div id="sign-in"></div>
20+
`;
21+
22+
const signInDiv = document.getElementById('sign-in');
23+
24+
clerk.mountSignIn(signInDiv);
25+
}
26+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"module": "ESNext",
5+
"moduleResolution": "Bundler"
6+
}
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference types="vite/client" />

integration/templates/express-vite/src/server/main.ts

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,44 @@
1-
// Should be at the top of the file - used to load clerk secret key
21
import 'dotenv/config';
32

4-
import { clerkMiddleware } from '@clerk/express';
3+
import { clerkMiddleware, getAuth } from '@clerk/express';
54
import express from 'express';
65
import ViteExpress from 'vite-express';
76

87
const app = express();
98

10-
app.use(clerkMiddleware());
11-
app.set('view engine', 'ejs');
12-
app.set('views', 'src/views');
9+
app.use(
10+
clerkMiddleware({
11+
publishableKey: process.env.VITE_CLERK_PUBLISHABLE_KEY,
12+
}),
13+
);
14+
15+
app.get('/api/protected', (req: any, res: any, _next: any) => {
16+
const { userId } = getAuth(req);
17+
if (!userId) {
18+
res.status(401).send('Unauthorized');
19+
return;
20+
}
21+
22+
res.send('Protected API response');
23+
});
1324

1425
const legacyRequireAuth = (req: any, _res: any, next: any) => {
1526
if (!req.auth.userId) {
16-
return next(new Error('Unauthenticated'));
27+
return next(new Error('Unauthorized'));
1728
}
1829

1930
next();
2031
};
2132

22-
app.get('/api/protected', legacyRequireAuth, (_req: any, res: any, _next: any) => {
23-
return res.send('Protected API response');
24-
});
25-
26-
app.get('/sign-in', (_req: any, res: any) => {
27-
return res.render('sign-in.ejs', {
28-
publishableKey: process.env.CLERK_PUBLISHABLE_KEY,
29-
signInUrl: process.env.CLERK_SIGN_IN_URL,
30-
});
31-
});
32-
33-
app.get('/', (_req: any, res: any) => {
34-
return res.render('index.ejs', {
35-
publishableKey: process.env.CLERK_PUBLISHABLE_KEY,
36-
signInUrl: process.env.CLERK_SIGN_IN_URL,
37-
});
38-
});
39-
40-
app.get('/sign-up', (_req: any, res: any) => {
41-
return res.render('sign-up.ejs', {
42-
publishableKey: process.env.CLERK_PUBLISHABLE_KEY,
43-
signUpUrl: process.env.CLERK_SIGN_UP_URL,
44-
});
45-
});
46-
47-
app.get('/protected', (_req: any, res: any) => {
48-
return res.render('protected.ejs', {
49-
publishableKey: process.env.CLERK_PUBLISHABLE_KEY,
50-
signInUrl: process.env.CLERK_SIGN_IN_URL,
51-
signUpUrl: process.env.CLERK_SIGN_UP_URL,
52-
});
33+
app.get('/api/legacy/protected', legacyRequireAuth, (_req: any, res: any, _next: any) => {
34+
res.send('Protected API response');
5335
});
5436

5537
// Handle authentication error, otherwise application will crash
5638
// @ts-ignore
5739
app.use((err, req, res, next) => {
5840
if (err) {
59-
console.error(err);
60-
res.status(401).end();
41+
res.status(401).send('Unauthorized');
6142
return;
6243
}
6344

integration/templates/express-vite/src/views/index.ejs

Lines changed: 0 additions & 32 deletions
This file was deleted.

integration/templates/express-vite/src/views/protected.ejs

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)