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