-
Notifications
You must be signed in to change notification settings - Fork 334
/
Copy pathcreateRedirect.ts
117 lines (102 loc) · 4.12 KB
/
createRedirect.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { constants } from './constants';
import { errorThrower, parsePublishableKey } from './util/shared';
const buildUrl = (
_baseUrl: string | URL,
_targetUrl: string | URL,
_returnBackUrl?: string | URL | null,
_devBrowserToken?: string | null,
) => {
if (_baseUrl === '') {
return legacyBuildUrl(_targetUrl.toString(), _returnBackUrl?.toString());
}
const baseUrl = new URL(_baseUrl);
const returnBackUrl = _returnBackUrl ? new URL(_returnBackUrl, baseUrl) : undefined;
const res = new URL(_targetUrl, baseUrl);
if (returnBackUrl) {
res.searchParams.set('redirect_url', returnBackUrl.toString());
}
// For cross-origin redirects, we need to pass the dev browser token for URL session syncing
if (_devBrowserToken && baseUrl.hostname !== res.hostname) {
res.searchParams.set(constants.QueryParameters.DevBrowser, _devBrowserToken);
}
return res.toString();
};
/**
* In v5, we deprecated the top-level redirectToSignIn and redirectToSignUp functions
* in favor of the new auth().redirectToSignIn helpers
* In order to allow for a smooth transition, we need to support the legacy redirectToSignIn for now
* as we will remove it in v6.
* In order to make sure that the legacy function works as expected, we will use legacyBuildUrl
* to build the url if baseUrl is not provided (which is the case for legacy redirectToSignIn)
* This function can be safely removed when we remove the legacy redirectToSignIn function
*/
const legacyBuildUrl = (targetUrl: string, redirectUrl?: string) => {
let url;
if (!targetUrl.startsWith('http')) {
if (!redirectUrl || !redirectUrl.startsWith('http')) {
throw new Error('destination url or return back url should be an absolute path url!');
}
const baseURL = new URL(redirectUrl);
url = new URL(targetUrl, baseURL.origin);
} else {
url = new URL(targetUrl);
}
if (redirectUrl) {
url.searchParams.set('redirect_url', redirectUrl);
}
return url.toString();
};
const buildAccountsBaseUrl = (frontendApi?: string) => {
if (!frontendApi) {
return '';
}
// convert url from FAPI to accounts for Kima and legacy (prod & dev) instances
const accountsBaseUrl = frontendApi
// staging accounts
.replace(/(clerk\.accountsstage\.)/, 'accountsstage.')
.replace(/(clerk\.accounts\.|clerk\.)/, 'accounts.');
return `https://${accountsBaseUrl}`;
};
type RedirectAdapter<RedirectReturn> = (url: string) => RedirectReturn;
type RedirectToParams = { returnBackUrl?: string | URL | null };
export type RedirectFun<ReturnType> = (params?: RedirectToParams) => ReturnType;
/**
* @internal
*/
type CreateRedirect = <ReturnType>(params: {
publishableKey: string;
devBrowserToken?: string;
redirectAdapter: RedirectAdapter<ReturnType>;
baseUrl: URL | string;
signInUrl?: URL | string;
signUpUrl?: URL | string;
}) => {
redirectToSignIn: RedirectFun<ReturnType>;
redirectToSignUp: RedirectFun<ReturnType>;
};
export const createRedirect: CreateRedirect = params => {
const { publishableKey, redirectAdapter, signInUrl, signUpUrl, baseUrl } = params;
const parsedPublishableKey = parsePublishableKey(publishableKey);
const frontendApi = parsedPublishableKey?.frontendApi;
const isDevelopment = parsedPublishableKey?.instanceType === 'development';
const accountsBaseUrl = buildAccountsBaseUrl(frontendApi);
const redirectToSignUp = ({ returnBackUrl }: RedirectToParams = {}) => {
if (!signUpUrl && !accountsBaseUrl) {
errorThrower.throwMissingPublishableKeyError();
}
const accountsSignUpUrl = `${accountsBaseUrl}/sign-up`;
return redirectAdapter(
buildUrl(baseUrl, signUpUrl || accountsSignUpUrl, returnBackUrl, isDevelopment ? params.devBrowserToken : null),
);
};
const redirectToSignIn = ({ returnBackUrl }: RedirectToParams = {}) => {
if (!signInUrl && !accountsBaseUrl) {
errorThrower.throwMissingPublishableKeyError();
}
const accountsSignInUrl = `${accountsBaseUrl}/sign-in`;
return redirectAdapter(
buildUrl(baseUrl, signInUrl || accountsSignInUrl, returnBackUrl, isDevelopment ? params.devBrowserToken : null),
);
};
return { redirectToSignUp, redirectToSignIn };
};