@frank-auth/react
Version:
Flexible and customizable React UI components for Frank Authentication
310 lines (309 loc) • 9.53 kB
JavaScript
const l = (t) => {
try {
const e = new URL(t), n = i(e.search);
return {
protocol: e.protocol,
hostname: e.hostname,
port: e.port || void 0,
pathname: e.pathname,
search: e.search,
hash: e.hash,
query: n,
origin: e.origin,
href: e.href
};
} catch {
return null;
}
}, u = (t, e) => {
try {
const n = new URL(t);
if (e)
for (const [r, o] of Object.entries(e))
o != null && n.searchParams.set(r, String(o));
return n.toString();
} catch {
return t;
}
}, p = (t, e) => u(t, e), i = (t) => {
const e = new URLSearchParams(t), n = {};
for (const [r, o] of e.entries())
r in n ? Array.isArray(n[r]) ? n[r].push(o) : n[r] = [n[r], o] : n[r] = o;
return n;
}, g = (t) => {
const e = new URLSearchParams();
for (const [n, r] of Object.entries(t))
if (r != null)
if (Array.isArray(r))
for (const o of r)
e.append(n, String(o));
else
e.set(n, String(r));
return e.toString();
}, y = (t, e) => {
try {
const n = new URL(t);
for (const r of e)
n.searchParams.delete(r);
return n.toString();
} catch {
return t;
}
}, U = (t, e) => {
try {
return new URL(t).searchParams.get(e);
} catch {
return null;
}
}, R = (t, e) => {
try {
return new URL(t).searchParams.has(e);
} catch {
return !1;
}
}, L = (t, e, n) => {
const r = {
signin: "/sign-in",
signup: "/sign-up",
reset: "/reset-password",
verify: "/verify"
}, o = new URL(r[e], t);
if (n)
for (const [s, a] of Object.entries(n))
a != null && o.searchParams.set(s, String(a));
return o.toString();
}, b = (t, e, n, r) => {
const s = {
google: "https://accounts.google.com/o/oauth2/v2/auth",
github: "https://github.com/login/oauth/authorize",
microsoft: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
facebook: "https://www.facebook.com/v18.0/dialog/oauth",
apple: "https://appleid.apple.com/auth/authorize",
twitter: "https://twitter.com/i/oauth2/authorize",
linkedin: "https://www.linkedin.com/oauth/v2/authorization",
discord: "https://discord.com/api/oauth2/authorize",
slack: "https://slack.com/oauth/v2/authorize",
spotify: "https://accounts.spotify.com/authorize"
}[t.toLowerCase()];
if (!s)
throw new Error(`Unsupported OAuth provider: ${t}`);
const a = {
client_id: e,
redirect_uri: n,
response_type: r?.responseType || "code"
};
if (r?.state && (a.state = r.state), r?.scope && (a.scope = Array.isArray(r.scope) ? r.scope.join(" ") : r.scope), r?.codeChallenge && r?.codeChallengeMethod && (a.code_challenge = r.codeChallenge, a.code_challenge_method = r.codeChallengeMethod), r?.organizationId && (a.organization_id = r.organizationId), r)
for (const [d, c] of Object.entries(r))
c != null && !["state", "scope", "responseType", "codeChallenge", "codeChallengeMethod", "organizationId"].includes(d) && (a[d] = String(c));
return u(s, a);
}, P = (t) => {
const e = l(t);
return e ? {
code: typeof e.query.code == "string" ? e.query.code : void 0,
state: typeof e.query.state == "string" ? e.query.state : void 0,
error: typeof e.query.error == "string" ? e.query.error : void 0,
errorDescription: typeof e.query.error_description == "string" ? e.query.error_description : void 0,
organizationId: typeof e.query.organization_id == "string" ? e.query.organization_id : void 0
} : {};
}, z = (t, e, n) => {
const r = new URL("/auth/magic-link", t);
return r.searchParams.set("token", e), n?.redirectUrl && r.searchParams.set("redirect_url", n.redirectUrl), n?.organizationId && r.searchParams.set("organization_id", n.organizationId), n?.mode && r.searchParams.set("mode", n.mode), r.toString();
}, v = (t, e, n, r) => {
const o = new URL(`/auth/verify-${n}`, t);
return o.searchParams.set("token", e), r?.redirectUrl && o.searchParams.set("redirect_url", r.redirectUrl), r?.organizationId && o.searchParams.set("organization_id", r.organizationId), o.toString();
}, O = (t, e, n) => {
const r = new URL("/auth/reset-password", t);
return r.searchParams.set("token", e), n?.redirectUrl && r.searchParams.set("redirect_url", n.redirectUrl), n?.organizationId && r.searchParams.set("organization_id", n.organizationId), r.toString();
}, j = (t, e, n) => {
const r = new URL("/auth/invitation", t);
return r.searchParams.set("token", e), n?.redirectUrl && r.searchParams.set("redirect_url", n.redirectUrl), n?.organizationId && r.searchParams.set("organization_id", n.organizationId), r.toString();
}, C = (t) => {
try {
return new URL(t), !0;
} catch {
return !1;
}
}, h = (t) => {
try {
const e = new URL(t);
return e.protocol === "http:" || e.protocol === "https:";
} catch {
return !1;
}
}, S = (t) => {
try {
return new URL(t).protocol === "https:";
} catch {
return !1;
}
}, f = (t, e) => {
try {
const r = new URL(t).hostname.toLowerCase();
return e.some((o) => {
const s = o.toLowerCase();
return r === s || r.endsWith(`.${s}`);
});
} catch {
return !1;
}
}, k = (t, e) => {
try {
const r = new URL(t).hostname.toLowerCase(), o = e.toLowerCase();
return r === o || r.endsWith(`.${o}`);
} catch {
return !1;
}
}, w = (t) => {
try {
return new URL(t).hostname;
} catch {
return null;
}
}, _ = (t) => {
const e = w(t);
if (!e) return null;
const n = e.split(".");
return n.length >= 2 ? n.slice(-2).join(".") : e;
}, I = (t) => {
try {
const e = new URL(t);
(e.protocol === "http:" && e.port === "80" || e.protocol === "https:" && e.port === "443") && (e.port = ""), e.pathname !== "/" && e.pathname.endsWith("/") && (e.pathname = e.pathname.slice(0, -1));
const n = Array.from(e.searchParams.entries()).sort(([r], [o]) => r.localeCompare(o));
e.search = "";
for (const [r, o] of n)
e.searchParams.append(r, o);
return e.toString();
} catch {
return t;
}
}, A = (...t) => {
if (t.length === 0) return "";
const [e, ...n] = t;
let r = e.replace(/\/+$/, "");
for (const o of n) {
const s = o.replace(/^\/+|\/+$/g, "");
s && (r += "/" + s);
}
return r;
}, q = (t) => {
try {
return new URL(t).pathname;
} catch {
return "";
}
}, $ = (t) => {
try {
const e = new URL(t);
return i(e.search);
} catch {
return {};
}
}, m = (t, e, n = !0) => t ? n && t.startsWith("/") ? !0 : h(t) ? f(t, e) : !1 : !1, D = (t, e, n = "/") => m(t, e) ? t : n, Q = () => typeof window < "u" ? window.location.href : "", H = () => typeof window < "u" ? window.location.pathname : "", M = () => typeof window < "u" ? window.location.hostname : "", T = () => typeof window < "u" ? window.location.origin : "", V = () => typeof window < "u" ? i(window.location.search) : {}, W = (t, e = !1) => {
typeof window < "u" && (e ? window.location.replace(t) : window.location.href = t);
}, x = (t) => {
typeof window < "u" && window.open(t, "_blank", "noopener,noreferrer");
}, E = () => typeof window < "u" ? window.location.hash : "", N = (t) => {
typeof window < "u" && (window.location.hash = t);
}, B = () => {
typeof window < "u" && window.history.replaceState("", document.title, window.location.pathname + window.location.search);
}, F = (t) => encodeURIComponent(t).replace(/[!'()*]/g, (e) => `%${e.charCodeAt(0).toString(16).toUpperCase()}`), G = (t) => {
try {
return decodeURIComponent(t);
} catch {
return t;
}
}, J = {
// Parsing
parseURL: l,
parseQueryString: i,
parseOAuthCallback: P,
// Building
buildURL: u,
buildQueryString: g,
buildAuthURL: L,
buildOAuthURL: b,
buildMagicLinkURL: z,
buildVerificationURL: v,
buildPasswordResetURL: O,
buildInvitationURL: j,
// Manipulation
addQueryParams: p,
removeQueryParams: y,
getQueryParam: U,
hasQueryParam: R,
joinURL: A,
normalizeURL: I,
// Validation
isValidURL: C,
isValidHttpURL: h,
isValidHttpsURL: S,
isDomainAllowed: f,
isSubdomain: k,
isSafeRedirectURL: m,
// Extraction
extractDomain: w,
extractRootDomain: _,
getURLPath: q,
getURLParams: $,
// Browser
getCurrentURL: Q,
getCurrentPath: H,
getCurrentDomain: M,
getCurrentOrigin: T,
getCurrentParams: V,
redirectTo: W,
openInNewTab: x,
// Hash
getHash: E,
setHash: N,
removeHash: B,
// Encoding
encodeURIComponentSafe: F,
decodeURIComponentSafe: G,
// Security
sanitizeRedirectURL: D
};
export {
J as URLUtils,
p as addQueryParams,
L as buildAuthURL,
j as buildInvitationURL,
z as buildMagicLinkURL,
b as buildOAuthURL,
O as buildPasswordResetURL,
g as buildQueryString,
u as buildURL,
v as buildVerificationURL,
G as decodeURIComponentSafe,
F as encodeURIComponentSafe,
w as extractDomain,
_ as extractRootDomain,
M as getCurrentDomain,
T as getCurrentOrigin,
V as getCurrentParams,
H as getCurrentPath,
Q as getCurrentURL,
E as getHash,
U as getQueryParam,
$ as getURLParams,
q as getURLPath,
R as hasQueryParam,
f as isDomainAllowed,
m as isSafeRedirectURL,
k as isSubdomain,
h as isValidHttpURL,
S as isValidHttpsURL,
C as isValidURL,
A as joinURL,
I as normalizeURL,
x as openInNewTab,
P as parseOAuthCallback,
i as parseQueryString,
l as parseURL,
W as redirectTo,
B as removeHash,
y as removeQueryParams,
D as sanitizeRedirectURL,
N as setHash
};
//# sourceMappingURL=url.js.map