forked from clerk/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateGetAuth.ts
More file actions
40 lines (34 loc) · 1.33 KB
/
Copy pathcreateGetAuth.ts
File metadata and controls
40 lines (34 loc) · 1.33 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
import type { AuthObject } from '@clerk/backend';
import { constants } from '@clerk/backend/internal';
import { decodeJwt } from '@clerk/backend/jwt';
import { isTruthy } from '@clerk/shared/underscore';
import { withLogger } from '../utils/debugLogger';
import { getAuthDataFromRequest } from './data/getAuthDataFromRequest';
import { getAuthAuthHeaderMissing } from './errors';
import type { RequestLike } from './types';
import { assertAuthStatus, getCookie, getHeader } from './utils';
export const createGetAuth = ({
noAuthStatusMessage,
debugLoggerName,
}: {
debugLoggerName: string;
noAuthStatusMessage: string;
}) =>
withLogger(debugLoggerName, logger => {
return (req: RequestLike, opts?: { secretKey?: string }): AuthObject => {
if (isTruthy(getHeader(req, constants.Headers.EnableDebug))) {
logger.enable();
}
assertAuthStatus(req, noAuthStatusMessage);
return getAuthDataFromRequest(req, { ...opts, logger });
};
});
export const getAuth = createGetAuth({
debugLoggerName: 'getAuth()',
noAuthStatusMessage: getAuthAuthHeaderMissing(),
});
export const parseJwt = (req: RequestLike) => {
const cookieToken = getCookie(req, constants.Cookies.Session);
const headerToken = getHeader(req, 'authorization')?.replace('Bearer ', '');
return decodeJwt(cookieToken || headerToken || '');
};