forked from clerk/javascript
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthorization-errors.ts
47 lines (40 loc) · 1.17 KB
/
authorization-errors.ts
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
import type { ReverificationConfig } from '@clerk/types';
type ClerkError<T> = {
clerk_error: T;
};
const REVERIFICATION_REASON = 'reverification-error';
type ReverificationError<M extends { metadata?: any } = { metadata: unknown }> = ClerkError<
{
type: 'forbidden';
reason: typeof REVERIFICATION_REASON;
} & M
>;
const reverificationError = <MC extends ReverificationConfig>(
missingConfig?: MC,
): ReverificationError<{
metadata?: {
reverification?: MC;
};
}> => ({
clerk_error: {
type: 'forbidden',
reason: REVERIFICATION_REASON,
metadata: {
reverification: missingConfig,
},
},
});
const reverificationErrorResponse = (...args: Parameters<typeof reverificationError>) =>
new Response(JSON.stringify(reverificationError(...args)), {
status: 403,
});
const isReverificationHint = (result: any): result is ReturnType<typeof reverificationError> => {
return (
result &&
typeof result === 'object' &&
'clerk_error' in result &&
result.clerk_error?.type === 'forbidden' &&
result.clerk_error?.reason === REVERIFICATION_REASON
);
};
export { reverificationError, reverificationErrorResponse, isReverificationHint };