|
1 | | -import type { AuthObject } from '@clerk/backend'; |
2 | 1 | import { constants } from '@clerk/backend/internal'; |
3 | 2 | import { isTruthy } from '@clerk/shared/underscore'; |
4 | 3 |
|
5 | 4 | import { withLogger } from '../utils/debugLogger'; |
| 5 | +import { isNextWithUnstableServerActions } from '../utils/sdk-versions'; |
6 | 6 | import { getAuthDataFromRequest } from './data/getAuthDataFromRequest'; |
7 | 7 | import { getAuthAuthHeaderMissing } from './errors'; |
8 | | -import { getHeader } from './headers-utils'; |
| 8 | +import { detectClerkMiddleware, getHeader } from './headers-utils'; |
9 | 9 | import type { RequestLike } from './types'; |
10 | 10 | import { assertAuthStatus } from './utils'; |
11 | 11 |
|
12 | | -export const createGetAuth = ({ |
| 12 | +export const createAsyncGetAuth = ({ |
| 13 | + debugLoggerName, |
13 | 14 | noAuthStatusMessage, |
| 15 | +}: { |
| 16 | + debugLoggerName: string; |
| 17 | + noAuthStatusMessage: string; |
| 18 | +}) => |
| 19 | + withLogger(debugLoggerName, logger => { |
| 20 | + return async (req: RequestLike, opts?: { secretKey?: string }) => { |
| 21 | + if (isTruthy(getHeader(req, constants.Headers.EnableDebug))) { |
| 22 | + logger.enable(); |
| 23 | + } |
| 24 | + |
| 25 | + if (!detectClerkMiddleware(req)) { |
| 26 | + // Keep the same behaviour for versions that may have issues with bundling `node:fs` |
| 27 | + if (isNextWithUnstableServerActions) { |
| 28 | + assertAuthStatus(req, noAuthStatusMessage); |
| 29 | + } |
| 30 | + |
| 31 | + const missConfiguredMiddlewareLocation = await import('./keyless-node.js') |
| 32 | + .then(m => m.suggestMiddlewareLocation()) |
| 33 | + .catch(() => undefined); |
| 34 | + |
| 35 | + if (missConfiguredMiddlewareLocation) { |
| 36 | + throw new Error(missConfiguredMiddlewareLocation); |
| 37 | + } |
| 38 | + |
| 39 | + // still throw there is no suggested move location |
| 40 | + assertAuthStatus(req, noAuthStatusMessage); |
| 41 | + } |
| 42 | + |
| 43 | + return getAuthDataFromRequest(req, { ...opts, logger }); |
| 44 | + }; |
| 45 | + }); |
| 46 | + |
| 47 | +export const createSyncGetAuth = ({ |
14 | 48 | debugLoggerName, |
| 49 | + noAuthStatusMessage, |
15 | 50 | }: { |
16 | 51 | debugLoggerName: string; |
17 | 52 | noAuthStatusMessage: string; |
18 | 53 | }) => |
19 | 54 | withLogger(debugLoggerName, logger => { |
20 | | - return (req: RequestLike, opts?: { secretKey?: string }): AuthObject => { |
| 55 | + return (req: RequestLike, opts?: { secretKey?: string }) => { |
21 | 56 | if (isTruthy(getHeader(req, constants.Headers.EnableDebug))) { |
22 | 57 | logger.enable(); |
23 | 58 | } |
24 | 59 |
|
25 | 60 | assertAuthStatus(req, noAuthStatusMessage); |
26 | | - |
27 | 61 | return getAuthDataFromRequest(req, { ...opts, logger }); |
28 | 62 | }; |
29 | 63 | }); |
@@ -107,7 +141,7 @@ export const createGetAuth = ({ |
107 | 141 | * } |
108 | 142 | * ``` |
109 | 143 | */ |
110 | | -export const getAuth = createGetAuth({ |
| 144 | +export const getAuth = createSyncGetAuth({ |
111 | 145 | debugLoggerName: 'getAuth()', |
112 | 146 | noAuthStatusMessage: getAuthAuthHeaderMissing(), |
113 | 147 | }); |
0 commit comments