@charmr/oauth-button
Version:
A lightweight, OAuth login button for React, designed to work seamlessly with the `@charmr/oauth-core` controller. Supports Google, Facebook (implicit flow), and GitHub (authorization code flow)
391 lines (390 loc) • 11.4 kB
JavaScript
import ee from "react";
var h = { exports: {} }, E = {};
/**
* @license React
* react-jsx-runtime.production.js
*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var Y;
function re() {
if (Y) return E;
Y = 1;
var r = Symbol.for("react.transitional.element"), o = Symbol.for("react.fragment");
function s(l, n, i) {
var d = null;
if (i !== void 0 && (d = "" + i), n.key !== void 0 && (d = "" + n.key), "key" in n) {
i = {};
for (var b in n)
b !== "key" && (i[b] = n[b]);
} else i = n;
return n = i.ref, {
$$typeof: r,
type: l,
key: d,
ref: n !== void 0 ? n : null,
props: i
};
}
return E.Fragment = o, E.jsx = s, E.jsxs = s, E;
}
var _ = {};
/**
* @license React
* react-jsx-runtime.development.js
*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var $;
function te() {
return $ || ($ = 1, process.env.NODE_ENV !== "production" && function() {
function r(e) {
if (e == null) return null;
if (typeof e == "function")
return e.$$typeof === H ? null : e.displayName || e.name || null;
if (typeof e == "string") return e;
switch (e) {
case v:
return "Fragment";
case M:
return "Profiler";
case G:
return "StrictMode";
case V:
return "Suspense";
case X:
return "SuspenseList";
case Z:
return "Activity";
}
if (typeof e == "object")
switch (typeof e.tag == "number" && console.error(
"Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."
), e.$$typeof) {
case W:
return "Portal";
case z:
return (e.displayName || "Context") + ".Provider";
case q:
return (e._context.displayName || "Context") + ".Consumer";
case J:
var t = e.render;
return e = e.displayName, e || (e = t.displayName || t.name || "", e = e !== "" ? "ForwardRef(" + e + ")" : "ForwardRef"), e;
case B:
return t = e.displayName || null, t !== null ? t : r(e.type) || "Memo";
case j:
t = e._payload, e = e._init;
try {
return r(e(t));
} catch {
}
}
return null;
}
function o(e) {
return "" + e;
}
function s(e) {
try {
o(e);
var t = !1;
} catch {
t = !0;
}
if (t) {
t = console;
var a = t.error, c = typeof Symbol == "function" && Symbol.toStringTag && e[Symbol.toStringTag] || e.constructor.name || "Object";
return a.call(
t,
"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",
c
), o(e);
}
}
function l(e) {
if (e === v) return "<>";
if (typeof e == "object" && e !== null && e.$$typeof === j)
return "<...>";
try {
var t = r(e);
return t ? "<" + t + ">" : "<...>";
} catch {
return "<...>";
}
}
function n() {
var e = T.A;
return e === null ? null : e.getOwner();
}
function i() {
return Error("react-stack-top-frame");
}
function d(e) {
if (y.call(e, "key")) {
var t = Object.getOwnPropertyDescriptor(e, "key").get;
if (t && t.isReactWarning) return !1;
}
return e.key !== void 0;
}
function b(e, t) {
function a() {
C || (C = !0, console.error(
"%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",
t
));
}
a.isReactWarning = !0, Object.defineProperty(e, "key", {
get: a,
configurable: !0
});
}
function g() {
var e = r(this.type);
return N[e] || (N[e] = !0, console.error(
"Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release."
)), e = this.props.ref, e !== void 0 ? e : null;
}
function D(e, t, a, c, m, f, w, S) {
return a = f.ref, e = {
$$typeof: x,
type: e,
key: t,
props: f,
_owner: m
}, (a !== void 0 ? a : null) !== null ? Object.defineProperty(e, "ref", {
enumerable: !1,
get: g
}) : Object.defineProperty(e, "ref", { enumerable: !1, value: null }), e._store = {}, Object.defineProperty(e._store, "validated", {
configurable: !1,
enumerable: !1,
writable: !0,
value: 0
}), Object.defineProperty(e, "_debugInfo", {
configurable: !1,
enumerable: !1,
writable: !0,
value: null
}), Object.defineProperty(e, "_debugStack", {
configurable: !1,
enumerable: !1,
writable: !0,
value: w
}), Object.defineProperty(e, "_debugTask", {
configurable: !1,
enumerable: !1,
writable: !0,
value: S
}), Object.freeze && (Object.freeze(e.props), Object.freeze(e)), e;
}
function P(e, t, a, c, m, f, w, S) {
var u = t.children;
if (u !== void 0)
if (c)
if (Q(u)) {
for (c = 0; c < u.length; c++)
O(u[c]);
Object.freeze && Object.freeze(u);
} else
console.error(
"React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead."
);
else O(u);
if (y.call(t, "key")) {
u = r(e);
var p = Object.keys(t).filter(function(K) {
return K !== "key";
});
c = 0 < p.length ? "{key: someKey, " + p.join(": ..., ") + ": ...}" : "{key: someKey}", F[u + c] || (p = 0 < p.length ? "{" + p.join(": ..., ") + ": ...}" : "{}", console.error(
`A props object containing a "key" prop is being spread into JSX:
let props = %s;
<%s {...props} />
React keys must be passed directly to JSX without using spread:
let props = %s;
<%s key={someKey} {...props} />`,
c,
u,
p,
u
), F[u + c] = !0);
}
if (u = null, a !== void 0 && (s(a), u = "" + a), d(t) && (s(t.key), u = "" + t.key), "key" in t) {
a = {};
for (var A in t)
A !== "key" && (a[A] = t[A]);
} else a = t;
return u && b(
a,
typeof e == "function" ? e.displayName || e.name || "Unknown" : e
), D(
e,
u,
f,
m,
n(),
a,
w,
S
);
}
function O(e) {
typeof e == "object" && e !== null && e.$$typeof === x && e._store && (e._store.validated = 1);
}
var R = ee, x = Symbol.for("react.transitional.element"), W = Symbol.for("react.portal"), v = Symbol.for("react.fragment"), G = Symbol.for("react.strict_mode"), M = Symbol.for("react.profiler"), q = Symbol.for("react.consumer"), z = Symbol.for("react.context"), J = Symbol.for("react.forward_ref"), V = Symbol.for("react.suspense"), X = Symbol.for("react.suspense_list"), B = Symbol.for("react.memo"), j = Symbol.for("react.lazy"), Z = Symbol.for("react.activity"), H = Symbol.for("react.client.reference"), T = R.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, y = Object.prototype.hasOwnProperty, Q = Array.isArray, k = console.createTask ? console.createTask : function() {
return null;
};
R = {
"react-stack-bottom-frame": function(e) {
return e();
}
};
var C, N = {}, L = R["react-stack-bottom-frame"].bind(
R,
i
)(), U = k(l(i)), F = {};
_.Fragment = v, _.jsx = function(e, t, a, c, m) {
var f = 1e4 > T.recentlyCreatedOwnerStacks++;
return P(
e,
t,
a,
!1,
c,
m,
f ? Error("react-stack-top-frame") : L,
f ? k(l(e)) : U
);
}, _.jsxs = function(e, t, a, c, m) {
var f = 1e4 > T.recentlyCreatedOwnerStacks++;
return P(
e,
t,
a,
!0,
c,
m,
f ? Error("react-stack-top-frame") : L,
f ? k(l(e)) : U
);
};
}()), _;
}
var I;
function ne() {
return I || (I = 1, process.env.NODE_ENV === "production" ? h.exports = re() : h.exports = te()), h.exports;
}
var oe = ne();
function ae(r, o) {
return `https://www.facebook.com/v12.0/dialog/oauth?${new URLSearchParams({
client_id: r,
redirect_uri: o,
response_type: "token",
scope: "email public_profile"
}).toString()}`;
}
function se({ clientId: r, redirectUri: o, onError: s }) {
return { startLogin: () => {
try {
const n = ae(r, o);
window.location.href = n;
} catch (n) {
s && n instanceof Error && s({ message: n.message });
}
} };
}
function ce(r, o) {
return `https://github.com/login/oauth/authorize?${new URLSearchParams({
client_id: r,
redirect_uri: o,
scope: "read:user user:email",
allow_signup: "true"
}).toString()}`;
}
function ue({ clientId: r, redirectUri: o, onError: s }) {
return { startLogin: () => {
try {
const n = ce(r, o);
window.location.href = n;
} catch (n) {
s && n instanceof Error && s({ message: n.message });
}
} };
}
function ie(r, o) {
return `https://accounts.google.com/o/oauth2/v2/auth?${new URLSearchParams({
response_type: "token",
client_id: r,
redirect_uri: o,
scope: "email profile"
}).toString()}`;
}
function le({ clientId: r, redirectUri: o, onError: s }) {
return { startLogin: () => {
try {
const n = ie(r, o);
window.location.href = n;
} catch (n) {
s && n instanceof Error && s(n);
}
} };
}
function fe(r) {
switch (r.provider) {
case "google":
return le(r);
case "facebook":
return se(r);
case "github":
return ue(r);
default:
throw new Error(`Unsupported provider: ${r.provider}`);
}
}
function de(r) {
const o = r.startsWith("#") ? r.slice(1) : r;
return new URLSearchParams(o).get("access_token");
}
function be(r) {
const o = r.startsWith("#") ? r.slice(1) : r;
return new URLSearchParams(o).get("access_token");
}
function pe(r) {
return new URLSearchParams(r).get("code");
}
const Ee = ({
provider: r,
clientId: o,
redirectUri: s,
buttonText: l = `Continue with ${r}`,
onSuccess: n,
onError: i,
className: d,
style: b
}) => {
const { startLogin: g } = fe({
provider: r,
clientId: o,
redirectUri: s,
onError: i
});
return /* @__PURE__ */ oe.jsx(
"button",
{
className: d,
style: b,
onClick: g,
children: l
}
);
};
export {
Ee as OAuthLoginButton,
be as extractFacebookTokenFromUrl,
pe as extractGitHubCodeFromUrl,
de as extractGoogleTokenFromUrl
};