forked from clerk/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-auth.ts
More file actions
52 lines (43 loc) · 2.08 KB
/
Copy pathget-auth.ts
File metadata and controls
52 lines (43 loc) · 2.08 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import type { AuthObject } from '@clerk/backend';
import { AuthStatus, signedInAuthObject, signedOutAuthObject } from '@clerk/backend/internal';
import { decodeJwt } from '@clerk/backend/jwt';
import type { APIContext } from 'astro';
import { getSafeEnv } from './get-safe-env';
import { getAuthKeyFromRequest } from './utils';
export type GetAuthReturn = AuthObject;
export const createGetAuth = ({ noAuthStatusMessage }: { noAuthStatusMessage: string }) => {
return (req: Request, locals: APIContext['locals'], opts?: { secretKey?: string }): GetAuthReturn => {
// When the auth status is set, we trust that the middleware has already run
// Then, we don't have to re-verify the JWT here,
// we can just strip out the claims manually.
const authToken = locals.authToken || getAuthKeyFromRequest(req, 'AuthToken');
const authStatus = locals.authStatus || (getAuthKeyFromRequest(req, 'AuthStatus') as AuthStatus);
const authMessage = locals.authMessage || getAuthKeyFromRequest(req, 'AuthMessage');
const authReason = locals.authReason || getAuthKeyFromRequest(req, 'AuthReason');
if (!authStatus) {
throw new Error(noAuthStatusMessage);
}
const options = {
authStatus,
apiUrl: getSafeEnv(locals).apiUrl,
apiVersion: getSafeEnv(locals).apiVersion,
authMessage,
secretKey: opts?.secretKey || getSafeEnv(locals).sk,
authReason,
};
if (authStatus !== AuthStatus.SignedIn) {
return signedOutAuthObject(options);
}
const jwt = decodeJwt(authToken as string);
// @ts-expect-error - TODO: Align types
return signedInAuthObject(options, jwt.raw.text, jwt.payload);
};
};
// TODO: Once docs for astro land, update the following message with this line
// "For more details, see <link-to-docs>"
const authAuthHeaderMissing = (helperName = 'auth') =>
`Clerk: ${helperName}() was called but Clerk can't detect usage of clerkMiddleware(). Please ensure that the clerkMiddleware() is used in your Astro Middleware.
`;
export const getAuth = createGetAuth({
noAuthStatusMessage: authAuthHeaderMissing(),
});