UNPKG

@myarapat/plugin

Version:

OnePay login plugin for vendor sites

158 lines (157 loc) 5.39 kB
import { jsxs as d, jsx as i, Fragment as k } from "react/jsx-runtime"; import { useState as a } from "react"; const $ = ({ vendor: l, vendorId: p, onSuccess: V, apiBaseUrl: u, mode: O = "onepay", userEmail: h, vendorToken: g }) => { const o = O === "twofactor", [s, S] = a( () => o && h ? h : "" ), [v, C] = a(!1), [c, y] = a(""), [f, t] = a(""), [m, b] = a(!1), w = async (e) => { if (o) return; b(e === "signup"), t(""); const n = e === "signup" ? "/api/user-to-vendor-signup-request/" : "/api/generate-otp-for-vendors/"; try { const r = await fetch(`${u}${n}`, { method: "POST", headers: { "Content-Type": "application/json", "X-Vendor-Id": p }, body: JSON.stringify({ email: s, vendor: l }) }), T = await r.json(); r.ok ? (C(!0), t( e === "signup" ? "✅ Signup OTP sent! Check your EzAuth portal." : "✅ Login OTP sent. Please enter it below." )) : t(`❌ ${T.error || T.message}`); } catch { t("❌ Network error."); } }; return /* @__PURE__ */ d( "div", { style: { border: "1px solid #ccc", padding: "1.5rem", borderRadius: 10, maxWidth: 400 }, children: [ /* @__PURE__ */ i("h2", { children: "Login/Signup via EzAuth" }), !o && /* @__PURE__ */ i( "input", { type: "email", placeholder: "Enter your EzAuth email", value: s, onChange: (e) => S(e.target.value), style: { width: "100%", padding: "0.5rem", marginTop: "0.5rem" } } ), o ? /* @__PURE__ */ d("div", { style: { marginTop: "1rem" }, children: [ /* @__PURE__ */ i("p", { children: "🔒 Enter your 2FA code:" }), /* @__PURE__ */ i( "input", { type: "text", placeholder: "Enter 2FA code", value: c, onChange: (e) => y(e.target.value), style: { width: "100%", padding: "0.5rem" } } ), /* @__PURE__ */ i( "button", { onClick: async () => { if (o) { if (!g) { t("❌ Vendor token missing"); return; } t(""); try { const e = await fetch( `${u}/api/vendors/2fa/verify/`, { method: "POST", headers: { "Content-Type": "application/json", "X-Vendor-Id": p }, body: JSON.stringify({ email: s, vendor: l, code: c, vendor_token: g }) } ), n = await e.json(); e.ok && n.redirect_url ? window.location.href = n.redirect_url : t(`❌ ${n.error || n.message}`); } catch { t("❌ Verification failed."); } } }, style: { marginTop: "0.5rem" }, children: "Verify 2FA" } ) ] }) : /* @__PURE__ */ d("div", { style: { display: "flex", gap: "0.5rem", marginTop: "1rem" }, children: [ /* @__PURE__ */ i("button", { onClick: () => w("login"), children: "Login with EzAuth" }), /* @__PURE__ */ i("button", { onClick: () => w("signup"), children: "Signup with EzAuth" }) ] }), v && !o && /* @__PURE__ */ d(k, { children: [ /* @__PURE__ */ i( "input", { type: "text", placeholder: "Enter OTP", value: c, onChange: (e) => y(e.target.value), style: { width: "100%", padding: "0.5rem", marginTop: "1rem" } } ), /* @__PURE__ */ i( "button", { onClick: async () => { if (o) return; t(""); const e = m ? "/api/confirm-vendor-signup/" : "/api/verify-login-otp/"; try { const n = await fetch(`${u}${e}`, { method: "POST", headers: { "Content-Type": "application/json", "X-Vendor-Id": p }, body: JSON.stringify({ email: s, vendor: l, otp: c }) }), r = await n.json(); n.ok && r.redirect_url ? (t( m ? "✅ Signup complete! Redirecting..." : "✅ Login successful! Redirecting..." ), setTimeout(() => window.location.href = r.redirect_url, 500)) : t(`❌ ${r.error || r.message}`); } catch { t("❌ OTP verification failed."); } }, style: { marginTop: "0.5rem" }, children: "Confirm OTP" } ) ] }), f && /* @__PURE__ */ i("p", { style: { marginTop: "1rem" }, children: f }) ] } ); }; export { $ as LogOneWidget, $ as default };