UNPKG

@passkey-fas/webauthn-sdk

Version:

Official JavaScript SDK for FaS (FIDO2 as Service) Platform - Easy passwordless authentication integration

3 lines (2 loc) 1.07 kB
import{useState as t,useEffect as e,useCallback as n}from"react";import r from"./index.js";function i(i){const[s,o]=t(null),[l,a]=t(null),[u,c]=t(!1),[h,w]=t(null);e(()=>{if(i?.clientId&&i?.clientSecret){const t=new r(i);o(t),t.isAuthenticated()&&a({authenticated:!0})}},[i]);return{user:l,loading:u,error:h,register:n(async(t,e)=>{if(!s)throw new Error("SDK not initialized");c(!0),w(null);try{const n=await s.registerPasskey(t,e);return a(n.user),n}catch(t){throw w(t.message),t}finally{c(!1)}},[s]),login:n(async t=>{if(!s)throw new Error("SDK not initialized");c(!0),w(null);try{const e=await s.authenticatePasskey(t);return a(e.user),e}catch(t){throw w(t.message),t}finally{c(!1)}},[s]),passwordlessLogin:n(async()=>{if(!s)throw new Error("SDK not initialized");c(!0),w(null);try{const t=await s.passwordlessLogin();return a(t.user),t}catch(t){throw w(t.message),t}finally{c(!1)}},[s]),logout:n(()=>{s&&(s.logout(),a(null),w(null))},[s]),isAuthenticated:null!==l,isWebAuthnSupported:r.isWebAuthnSupported()}}export{i as useFaSAuth}; //# sourceMappingURL=react.js.map