-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathqueryStateParams.ts
52 lines (40 loc) · 1.27 KB
/
queryStateParams.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
48
49
50
51
52
import { CLERK_MODAL_STATE } from '../core/constants';
import { encodeB64, getClerkQueryParam } from '../utils';
export const buildVirtualRouterUrl = ({ base, path }: { base: string; path: string | undefined }) => {
if (!path) {
return base;
}
return base + path;
};
export const readStateParam = () => {
const urlClerkState = getClerkQueryParam(CLERK_MODAL_STATE) ?? '';
return urlClerkState ? JSON.parse(atob(urlClerkState)) : null;
};
type SerializeAndAppendModalStateProps = {
url: string;
startPath?: string;
currentPath?: string;
componentName: string;
socialProvider?: string;
};
export const appendModalState = ({
url,
startPath = '/user',
currentPath = '',
componentName,
socialProvider = '',
}: SerializeAndAppendModalStateProps) => {
const regexPattern = /CLERK-ROUTER\/VIRTUAL\/.*\//;
const redirectParams = {
path: currentPath.replace(regexPattern, '') || '',
componentName,
startPath,
socialProvider,
};
const encodedRedirectParams = encodeB64(JSON.stringify(redirectParams));
const urlWithParams = new URL(url);
const searchParams = urlWithParams.searchParams;
searchParams.set(CLERK_MODAL_STATE, encodedRedirectParams);
urlWithParams.search = searchParams.toString();
return urlWithParams.toString();
};