forked from clerk/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclerkExpressWithAuth.ts
More file actions
32 lines (29 loc) · 1.02 KB
/
Copy pathclerkExpressWithAuth.ts
File metadata and controls
32 lines (29 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { authenticateRequest, setResponseHeaders } from './authenticateRequest';
import type { CreateClerkExpressMiddlewareOptions } from './clerkExpressRequireAuth';
import type { ClerkMiddlewareOptions, MiddlewareWithAuthProp, WithAuthProp } from './types';
export const createClerkExpressWithAuth = (createOpts: CreateClerkExpressMiddlewareOptions) => {
const { clerkClient, secretKey = '', publishableKey = '' } = createOpts;
return (options: ClerkMiddlewareOptions = {}): MiddlewareWithAuthProp => {
return async (req, res, next) => {
const requestState = await authenticateRequest({
clerkClient,
secretKey,
publishableKey,
req,
options,
});
const err = setResponseHeaders(requestState, res);
if (err || res.writableEnded) {
if (err) {
next(err);
}
return;
}
(req as WithAuthProp<any>).auth = {
...requestState.toAuth(),
claims: requestState.toAuth()?.sessionClaims,
};
next();
};
};
};