⁠⁠npm install magic-sdkimport { Magic } from 'magic-sdk';let magic;// Construct with an API key:// network configuration defaults to 'mainnet' (Magic's RPC)magic = new Magic('pk_live_4F94264055282A4A'); // Replace with your actual key// Construct with an API key and use Magic's RPC:magic = new Magic('pk_live_4F94264055282A4A', { network: 'sepolia' // or 'mainnet'});// Construct with an API key and set custom network:const customNodeOptions = { rpcUrl: 'https://polygon-rpc.com', // your rpc URL chainId: 137 // corresponding chainId for your rpc url};magic = new Magic('pk_live_4F94264055282A4A', { network: customNodeOptions // connected to Polygon Mainnet});// Construct with an API key and optional paramsmagic = new Magic('pk_live_4F94264055282A4A', { locale: 'es', testMode: true, deferPreload: true, endpoint: '...',});import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A', { deferPreload: true }); // API key added// ...magic.preload;import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key addedmagic.auth;magic.auth.loginWithMagicLink;magic.auth.loginWithEmailOTP;magic.auth.loginWithSMS;magic.auth.loginWithCredential;magic.auth.updateEmailWithUI;import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API Key added// log in a user by their emailtry { await magic.auth.loginWithMagicLink({ email: 'hello@example.com' });} catch { // Handle errors if required!}// log in a user by their email, without showing an out-of-the box UI.try { await magic.auth.loginWithMagicLink({ email: 'hello@example.com', showUI: false });} catch { // Handle errors if required!}import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key added// log in a user by their emailtry { await magic.auth.loginWithEmailOTP({ email: 'hello@example.com' });} catch { // Handle errors if required!}// log in a user by their email, without showing an out-of-the box UI.try { await magic.auth.loginWithEmailOTP({ email: 'hello@example.com', showUI: false });} catch { // Handle errors if required!}import { Magic, LoginWithEmailOTPEventOnReceived, LoginWithEmailOTPEventEmit, RecencyCheckEventOnReceived, // Corrected import RecencyCheckEventEmit, // Corrected import DeviceVerificationEventEmit, } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key addedtry { // Initiate login flow const handle = magic.auth.loginWithEmailOTP({ email: "hello@example.com", showUI: false, deviceCheckUI: false }); handle .on(LoginWithEmailOTPEventOnReceived.EmailOTPSent, () => { // The email has been sent to the user // Prompt the user for the OTP const otp = window.prompt('Enter Email OTP'); // Send the OTP for verification handle.emit(LoginWithEmailOTPEventEmit.VerifyEmailOtp, otp); }) .on(LoginWithEmailOTPEventOnReceived.InvalidEmailOtp, () => { // User entered invalid OTP // Have the user retry entering the OTP and emit via VerifyEmailOtp. // You can limit retries and emit Cancel to cancel login flow. // cancel login request handle.emit(LoginWithEmailOTPEventEmit.Cancel); }) .on('done', (result) => { // is called when the Promise resolves // convey login success to user alert('Login complete!'); // DID Token returned in result const didToken = result; }) .on('error', (reason) => { // is called if the Promise rejects console.error(reason); }) .on('settled', () => { // is called when the Promise either resolves or rejects }) //** MFA Verification Events (if enabled for app) .on(LoginWithEmailOTPEventOnReceived.MfaSentHandle, () => { // Corrected event name // Prompt the user for the MFA TOTP const mfa_totp = window.prompt('Enter MFA TOTP'); // Send the MFA TOTP for verification handle.emit(LoginWithEmailOTPEventEmit.VerifyMfaCode, mfa_totp) }) .on(LoginWithEmailOTPEventOnReceived.InvalidMfaOtp, () => { // Corrected event name // User entered invalid OTP // Have the user retry entering the MFA OTP and emit via VerifyMfaCode. // You can limit retries and emit Cancel to cancel login flow. // cancel login request handle.emit(LoginWithEmailOTPEventEmit.Cancel); }) //** Device Verification Events (if enabled for app) .on(DeviceVerificationEventOnReceived.DeviceNeedsApproval, () => { // Corrected event name // is called when device is not recognized and requires approval }) .on(DeviceVerificationEventOnReceived.DeviceVerificationEmailSent, () => { // Corrected event name // is called when the device verification email is sent }) .on(DeviceVerificationEventOnReceived.DeviceApproved, () => { // Corrected event name // is called when the device has been approved }) .on(DeviceVerificationEventOnReceived.DeviceVerificationLinkExpired, () => { // Corrected event name // is called when the device verification link is expired // Retry device verification handle.emit(DeviceVerificationEventEmit.Retry); }); // LoginWithEmailOTPEventEmit.Cancel can always be emitted to terminate the unresolved request.} catch (err) { // handle errors}import { Magic, RPCError, RPCErrorCode } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key addedtry { await magic.auth.loginWithEmailOTP({ email: 'hello@example.com' });} catch (err) { if (err instanceof RPCError) { switch (err.code) { case RPCErrorCode.MagicLinkExpired case RPCErrorCode.UserAlreadyLoggedIn: // Handle errors accordingly :) break; } }}import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key added// log in a user by their emailtry { await magic.auth.loginWithEmailOTP({ email: 'hello@example.com' });} catch { // Handle errors if required!}// log in a user by their email, without showing an out-of-the box UI.try { await magic.auth.loginWithEmailOTP({ email: 'hello@example.com', showUI: false });} catch { // Handle errors if required!}import { Magic, LoginWithEmailOTPEventOnReceived, LoginWithEmailOTPEventEmit, RecencyCheckEventOnReceived, // Corrected import RecencyCheckEventEmit, // Corrected import DeviceVerificationEventEmit, } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key addedtry { // Initiate login flow const handle = magic.auth.loginWithEmailOTP({ email: "hello@example.com", showUI: false, deviceCheckUI: false }); handle .on(LoginWithEmailOTPEventOnReceived.EmailOTPSent, () => { // The email has been sent to the user // Prompt the user for the OTP const otp = window.prompt('Enter Email OTP'); // Send the OTP for verification handle.emit(LoginWithEmailOTPEventEmit.VerifyEmailOtp, otp); }) .on(LoginWithEmailOTPEventOnReceived.InvalidEmailOtp, () => { // User entered invalid OTP // Have the user retry entering the OTP and emit via VerifyEmailOtp. // You can limit retries and emit Cancel to cancel login flow. // cancel login request handle.emit(LoginWithEmailOTPEventEmit.Cancel); }) .on('done', (result) => { // is called when the Promise resolves // convey login success to user alert('Login complete!'); // DID Token returned in result const didToken = result; }) .on('error', (reason) => { // is called if the Promise rejects console.error(reason); }) .on('settled', () => { // is called when the Promise either resolves or rejects }) //** MFA Verification Events (if enabled for app) .on(LoginWithEmailOTPEventOnReceived.MfaSentHandle, () => { // Corrected event name // Prompt the user for the MFA TOTP const mfa_totp = window.prompt('Enter MFA TOTP'); // Send the MFA TOTP for verification handle.emit(LoginWithEmailOTPEventEmit.VerifyMfaCode, mfa_totp) }) .on(LoginWithEmailOTPEventOnReceived.InvalidMfaOtp, () => { // Corrected event name // User entered invalid OTP // Have the user retry entering the MFA OTP and emit via VerifyMfaCode. // You can limit retries and emit Cancel to cancel login flow. // cancel login request handle.emit(LoginWithEmailOTPEventEmit.Cancel); }) //** Device Verification Events (if enabled for app) .on(DeviceVerificationEventOnReceived.DeviceNeedsApproval, () => { // Corrected event name // is called when device is not recognized and requires approval }) .on(DeviceVerificationEventOnReceived.DeviceVerificationEmailSent, () => { // Corrected event name // is called when the device verification email is sent }) .on(DeviceVerificationEventOnReceived.DeviceApproved, () => { // Corrected event name // is called when the device has been approved }) .on(DeviceVerificationEventOnReceived.DeviceVerificationLinkExpired, () => { // Corrected event name // is called when the device verification link is expired // Retry device verification handle.emit(DeviceVerificationEventEmit.Retry); }); // LoginWithEmailOTPEventEmit.Cancel can always be emitted to terminate the unresolved request.} catch (err) { // handle errors}import { Magic, RPCError, RPCErrorCode } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key addedtry { await magic.auth.loginWithEmailOTP({ email: 'hello@example.com' });} catch (err) { if (err instanceof RPCError) { switch (err.code) { case RPCErrorCode.MagicLinkExpired case RPCErrorCode.UserAlreadyLoggedIn: // Handle errors accordingly :) break; } }}import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key added// log in a user by their phone numbertry { await magic.auth.loginWithSMS({ phoneNumber: '+14151231234' }); } catch { // Handle errors if required!}import { Magic, RPCError, RPCErrorCode } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key addedtry { await magic.auth.loginWithSMS({ phoneNumber: "+14151231234" });} catch (err) { if (err instanceof RPCError) { switch (err.code) { case RPCErrorCode.AccessDeniedToUser: case RPCErrorCode.MagicLinkRateLimited: case RPCErrorCode.UserAlreadyLoggedIn: // Handle errors accordingly :) break; } }}import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key addedtry { await magic.auth.loginWithMagicLink({ email: 'hello@example.com', redirectURI: 'https://yourdomain.com/your/magic/link/callback', }); // When the user clicks their magic link, they will be logged-in here // and in the "callback" context.} catch { // Handle errors if required!}import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key addedtry { await magic.auth.loginWithCredential();} catch { // Handle errors if required!}// You can also provide the credential yourselftry { await magic.auth.loginWithCredential('iamacredentialtoken');} catch { // Handle errors if required!}// You can also provide the credential as a query stringtry { await magic.auth.loginWithCredential(window.location.search);} catch { // Handle errors if required!}import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key added// Initiates the flow to update a user's current email to a new one.try { /* Assuming user is logged in */ await magic.auth.updateEmailWithUI({ email: 'new_user_email@example.com' });} catch { // Handle errors if required!}/** * Initiates the flow to update a user's current email to a new one, * without showing an out-of-the box UI. */try { /* Assuming user is logged in */ await magic.auth.updateEmailWithUI({ email: 'new_user_email@example.com', showUI: false });} catch { // Handle errors if required!}import { Magic, RecencyCheckEventOnReceived, RecencyCheckEventEmit, UpdateEmailEventOnReceived, UpdateEmailEventEmit,} from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key addedtry { /* Initiates update email flow to update a user's current email to a new one */ const handle = await magic.auth.updateEmailWithUI({ email: 'new_user_email@example.com', showUI: false, }); /* Recency Check Events & Emit */ let recencyCheckRetries = 5; handle .on(RecencyCheckEventOnReceived.EmailSent, () => { // Email OTP has been sent to the user's primary email // Prompt the user for the OTP const otp = window.prompt('Primary Email OTP'); // Send the OTP for verification handle.emit(RecencyCheckEventEmit.VerifyEmailOtp, otp); }) .on(RecencyCheckEventOnReceived.PrimaryAuthFactorVerified, () => { window.alert('Primary Factor has been verified'); }) .on(RecencyCheckEventOnReceived.EmailNotDeliverable, () => { // Email OTP was undeliverable to user's primary email // Cancel update email request handle.emit(RecencyCheckEventEmit.Cancel); window.alert('Email Not Deliverable'); }) .on(RecencyCheckEventOnReceived.EmailExpired, () => { // User entered expired OTP handle.emit(RecencyCheckEventEmit.Cancel); window.alert('Expired OTP'); }) .on(RecencyCheckEventOnReceived.InvalidEmailOtp, () => { // User entered invalid OTP; you may limit retries and cancel the request if (!recencyCheckRetries) { // Cancel update email request alert('Too many attempts'); handle.emit(RecencyCheckEventEmit.Cancel); } else { const otp = window.prompt( `Invalid code, Please enter OTP again. Retries left: ${recencyCheckRetries}`, ); recencyCheckRetries--; // Send the OTP for verification handle.emit(RecencyCheckEventEmit.VerifyEmailOtp, otp); } }); /* Update Email Events & Emit */ let updateEmailRetries = 5; handle .on(UpdateEmailEventOnReceived.EmailSent, () => { // Email OTP has been sent to the user's secondary email // Prompt the user for the OTP const otp = window.prompt('Enter new Email OTP'); // Send the OTP for verification handle.emit(UpdateEmailEventEmit.VerifyEmailOtp, otp); }) .on(UpdateEmailEventOnReceived.InvalidEmail, () => { // Email OTP was undeliverable to user's secondary email const newEmail = window.prompt('Invalid Email, Enter a new Email'); // Try same or new email address handle.emit( UpdateEmailEventEmit.RetryWithNewEmail, newEmail || 'new_user_email@example.com', // Using the email from the initial request ); }) .on(UpdateEmailEventOnReceived.EmailAlreadyExists, () => { // Account already exists for new email address const newEmail = window.prompt('Email address already in use, Enter a different Email'); // Try same or new email address handle.emit( UpdateEmailEventEmit.RetryWithNewEmail, newEmail || 'new_user_email@example.com', // Using the email from the initial request ); }) .on(UpdateEmailEventOnReceived.InvalidEmailOtp, () => { // User entered invalid OTP; you may limit retries and cancel the request if (!updateEmailRetries) { // Cancel update email request alert('Too many attempts'); handle.emit(UpdateEmailEventEmit.Cancel); } else { const otp = window.prompt( `Invalid code, Please enter OTP again. Retries left: ${updateEmailRetries}`, ); updateEmailRetries--; // Send the OTP for verification handle.emit(UpdateEmailEventEmit.VerifyEmailOtp, otp); } }) .on(UpdateEmailEventOnReceived.EmailUpdated, () => { // Update email successful alert('Email Updated'); }) handle .on('error', () => { // is called if the Promise rejects alert('Error occurred'); }); const res = await handle; console.log(res); // Can also handle successful email update here alert('Email Updated');} catch { // Handle errors if required!}import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key addedmagic.wallet;magic.wallet.connectWithUI;magic.wallet.showUI;magic.wallet.showAddress;magic.wallet.showBalances;magic.wallet.showNFTs;magic.wallet.showSendTokensUI;magic.wallet.showOnRamp; // enterprise onlymagic.wallet.getProvider;import { Magic } from "magic-sdk"const accounts = await magic.wallet.connectWithUI();⁠/* Optionally, chain to the id token creation event if needed and configured (Universal Wallets only) */magic.wallet.connectWithUI().on('id-token-created', (params) => { const { idToken } = params console.log(idToken) // send to your resource server for validation // ...});import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key addedawait magic.wallet.showUI();import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key addedawait magic.wallet.showAddress();import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key addedawait magic.wallet.showBalances();import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key addedawait magic.wallet.showNFTs();import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key addedawait magic.wallet.showSendTokensUI();import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key addedawait magic.wallet.showOnRamp();import { Magic } from "magic-sdk"import Web3 from 'web3';const magic = new Magic('pk_live_4F94264055282A4A', { // API key added network: "goerli",});const provider = await magic.wallet.getProvider();const web3 = new Web3(provider);const accounts = await magic.wallet.connectWithUI();// Listen for eventsweb3.currentProvider.on('accountsChanged', handleAccountsChanged);web3.currentProvider.on('chainChanged', handleChainChanged);import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key added magic.user;magic.user.getIdToken;magic.user.generateIdToken;magic.user.getInfo;magic.user.isLoggedIn;magic.user.logout;magic.user.showSettings;magic.user.enableMFA;magic.user.disableMFA;magic.user.revealPrivateKey;magic.user.requestInfoWithUI;import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key added// Assumes a user is already logged intry { const idToken = await magic.user.getIdToken();} catch { // Handle errors if required!}import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A');// Assumes a user is already logged intry {  const idToken = await magic.user.generateIdToken({ attachment: 'sk_live_7FD316D361063495' });} catch {  // Handle errors if required!}import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key added// Assumes a user is already logged intry { const userInfo = await magic.user.getInfo();} catch { // Handle errors if required!}import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key addedtry { const isLoggedIn = await magic.user.isLoggedIn(); console.log(isLoggedIn); // => `true` or `false`} catch { // Handle errors if required!}import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key addedtry { await magic.user.logout(); console.log(await magic.user.isLoggedIn()); // => `false`} catch { // Handle errors if required!}import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key addedtry { await magic.user.showSettings();} catch { // Handle errors if required!}// Deeplink to MFA viewtry { await magic.user.showSettings({ page: 'mfa' });} catch { // Handle errors if required!}import { Magic } from 'magic-sdk';import { EnableMFAEventEmit, EnableMFAEventOnReceived,} from '@magic-sdk/types';const magic = new Magic('pk_live_4F94264055282A4A'); // API key added// user enables MFA through Magic UItry { await magic.user.enableMFA();} catch { // Handle errors if required!}// user enables MFA through whitelabel UItry { const handle = magic.user.enableMFA({ showUI: false }); handle .on(EnableMFAEventOnReceived.MFASecretGenerated, ({ QRCode, key }) => { // Display QR code window.alert(`QRCode: ${QRCode}
Key:${key}`); // Prompt for MFA TOTP and emit it to enable MFA const totp = window.prompt('Scan QR code and enter TOTP from MFA app'); handle.emit(EnableMFAEventEmit.VerifyMFACode, totp); }) .on(EnableMFAEventOnReceived.InvalidMFAOtp, ({ errorCode }) => { // User entered invalid MFA TOTP // Have the user retry entering the MFA TOTP and emit via VerifyMFACode. // You may display error and limit retries by emitting Cancel. handle.emit(EnableMFAEventEmit.Cancel); }) .on(EnableMFAEventOnReceived.MFARecoveryCodes, ({ recoveryCode }) => { // Enable MFA success, user entered valid MFA TOTP, display Recovery Code window.alert(`MFA enabled! Recovery code - ${recoveryCode}`); }) .on('error', (error) => { // Handle enable MFA errors }); } catch { // Handle errors if required!}import { Magic } from 'magic-sdk';import { DisableMFAEventEmit, DisableMFAEventOnReceived,} from '@magic-sdk/types';const magic = new Magic('pk_live_4F94264055282A4A'); // API key added// user disables MFA through Magic UItry { await magic.user.disableMFA();} catch { // Handle errors if required!}// user disables MFA through whitelabel UItry { const handle = magic.user.disableMFA({ showUI: false }); handle .on(DisableMFAEventOnReceived.MFACodeRequested, () => { // Prompt user for MFA TOTP and emit it to disable MFA const totp = window.prompt('Submit MFA TOTP'); handle.emit(DisableMFAEventEmit.VerifyMFACode, totp); // If user lost device and needs to recover with recovery code const recoveryCode = window.prompt('Enter Recovery Code'); handle.emit(DisableMFAEventEmit.LostDevice, recoveryCode); }) .on(DisableMFAEventOnReceived.InvalidMFAOtp, ({ errorCode }) => { // Handle user entered invalid MFA TOTP // Have the user retry entering the MFA TOTP and emit via VerifyMFACode. // You may display error and limit retries by emitting Cancel. handle.emit(DisableMFAEventEmit.Cancel); }) .on(DisableMFAEventOnReceived.InvalidRecoveryCode, () => { // Handle user entered invalid Recovery Code // Have the user retry entering the Recovery Code and emit via LostDevice. // You may display error and limit retries by emitting Cancel. handle.emit(DisableMFAEventEmit.Cancel); }) .on('done', () => { // Handle successful disable MFA }) .on('error', (error) => { // Handle disable MFA errors }); } catch { // Handle errors if required!}import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key addedtry { await magic.user.revealPrivateKey();} catch { // Handle errors if required!}import { Magic } from "magic-sdk"// after user has already logged inconst userInfo = await magic.user.requestInfoWithUI({ scope: { email: "required" }})⁠console.log(userInfo.email); // the user's email if they consented.import { Magic } from "magic-sdk"// Create Magic instance with useStorageCache set to trueconst magic = new Magic('pk_live_4F94264055282A4A', { // API key added useStorageCache: true});magic.user.onUserLoggedOut((isLoggedOut: boolean) => { // Do something when user is logged out navigation.navigate('LoginScreen') })import { Magic } from "magic-sdk"// Create Magic instance with useStorageCache set to trueconst magic = new Magic('pk_live_4F94264055282A4A', { // API key added useStorageCache: true});magic.user.onUserLoggedOut((isLoggedOut: boolean) => { // Do something when user is logged out navigation.navigate('LoginScreen') })import { Magic } from 'magic-sdk';import { OAuthExtension } from '@magic-ext/oauth2';const magic = new Magic('pk_live_4F94264055282A4A', { // API key added extensions: [new OAuthExtension()],});magic.oauth2;magic.oauth2.loginWithRedirect;magic.oauth2.getRedirectResult;import { Magic } from 'magic-sdk';import { OAuthExtension } from '@magic-ext/oauth2';const magic = new Magic('pk_live_4F94264055282A4A', { // API key added extensions: [new OAuthExtension()],});await magic.oauth2.loginWithRedirect({ provider: '...' /* 'google', 'facebook', 'apple', etc. */, redirectURI: 'https://your-app.com/your/oauth/callback', scope: ['user:email'] /* optional */,});import { Magic } from 'magic-sdk';import { OAuthExtension } from '@magic-ext/oauth2';const magic = new Magic('pk_live_4F94264055282A4A', { // API key added extensions: [new OAuthExtension()],});// optionally pass in number to method to set Decentralized ID token lifespanconst result = await magic.oauth2.getRedirectResult();// Result has the following interfaceinterface OAuthRedirectResult { magic: { idToken: string; userMetadata: MagicUserMetadata; // Make sure MagicUserMetadata is defined }, oauth: { provider: string; scope: string[]; accessToken: string; userHandle: string; userInfo: ...; }};import { Magic } from 'magic-sdk';import { OpenIdExtension } from '@magic-ext/oidc';const magic = new Magic('pk_live_4F94264055282A4A', { // API key added extensions: [new OpenIdExtension()],});magic.openid;magic.openid.loginWithOIDC;import { Magic } from 'magic-sdk';import { OpenIdExtension } from '@magic-ext/oidc';const magic = new Magic('pk_live_4F94264055282A4A', { // API key added extensions: [new OpenIdExtension()],});const DID = await magic.openid.loginWithOIDC({ // this oidcToken comes from the identity provider jwt: oidcToken, // this providerId is provided by Magic providerId: myProviderId, // this optional argument sets the resolved Decentralized ID token's lifespan lifespan: number });import { Magic } from 'magic-sdk';import { WebAuthnExtension } from '@magic-ext/webauthn';const magic = new Magic('pk_live_4F94264055282A4A', { // API key added extensions: [new WebAuthnExtension()],});magic.webauthn;magic.webauthn.registerNewUser;magic.webauthn.login;magic.webauthn.getMetadata;magic.webauthn.registerNewDevice;magic.webauthn.updateInfo;magic.webauthn.unregisterDevice;import { Magic } from 'magic-sdk';import { WebAuthnExtension } from '@magic-ext/webauthn';const magic = new Magic('pk_live_4F94264055282A4A', { // API key added extensions: [new WebAuthnExtension()],});// register a user by their usernametry { const token = await magic.webauthn.registerNewUser({ username: 'username' });} catch (e) { // Handle errors if required!}// log in a user by their username and set webauthn device nickname.try { const token = await magic.webauthn.registerNewUser({ username: 'username', nickname: 'nickname' });} catch (e) { // Handle errors if required!}import { Magic, SDKErrorCode } from 'magic-sdk';import { WebAuthnExtension } from '@magic-ext/webauthn';const magic = new Magic('pk_live_4F94264055282A4A', { // API key added extensions: [new WebAuthnExtension()],});try { await magic.webauthn.registerNewUser({ username: 'username' });} catch (err) { if (err instanceof SDKErrorCode) { switch (err.code) { case SDKErrorCode.WebAuthnNotSupported: case SDKErrorCode.WebAuthnCreateCredentialError: // Handle errors accordingly :) break; } }}import { Magic } from 'magic-sdk';import { WebAuthnExtension } from '@magic-ext/webauthn';const magic = new Magic('pk_live_4F94264055282A4A', { // API key added extensions: [new WebAuthnExtension()],});// login a user by their usernametry { const token = await magic.webauthn.login({ username: 'username' });} catch (e) { // Handle errors if required!}import { Magic, SDKErrorCode } from 'magic-sdk';import { WebAuthnExtension } from '@magic-ext/webauthn';const magic = new Magic('pk_live_4F94264055282A4A', { // API key added extensions: [new WebAuthnExtension()],});try { await magic.webauthn.login({ username: 'username' });} catch (err) { if (err instanceof SDKErrorCode) { switch (err.code) { case SDKErrorCode.WebAuthnNotSupported: case SDKErrorCode.WebAuthnGetCredentialError: // Corrected error code // Handle errors accordingly :) break; } }}import { Magic, SDKErrorCode } from 'magic-sdk';import { WebAuthnExtension } from '@magic-ext/webauthn';const magic = new Magic('pk_live_4F94264055282A4A', { // API key added extensions: [new WebAuthnExtension()],});// Initiates the flow to get webauthn metadata for current account.try { const metadata = await magic.webauthn.getMetadata(); /* webauthn metadata { "devicesInfo": [ { "id": "EjI_EFJhB6cdCj6rHPRHUcFCn6NnywALuWjQyPe0_dI=", "nickname": "", "transport": "internal", "user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36" } ], "username": "username" } */} catch (e) { // Handle errors if required!}import { Magic, SDKErrorCode } from 'magic-sdk';import { WebAuthnExtension } from '@magic-ext/webauthn';const magic = new Magic('pk_live_4F94264055282A4A', { // API key added extensions: [new WebAuthnExtension()],});// Initiates the flow to a new WebAuthn Device for current account.try { await magic.webauthn.registerNewDevice('new device nickname');} catch (e) { // Handle errors if required!}import { Magic, SDKErrorCode } from 'magic-sdk';import { WebAuthnExtension } from '@magic-ext/webauthn';const magic = new Magic('pk_live_4F94264055282A4A', { // API key added extensions: [new WebAuthnExtension()],});try { await magic.webauthn.registerNewDevice('new device nickname');} catch (err) { if (err instanceof SDKErrorCode) { switch (err.code) { case SDKErrorCode.WebAuthnNotSupported: case SDKErrorCode.WebAuthnCreateCredentialError: // Handle errors accordingly :) break; } }}import { Magic, SDKErrorCode } from 'magic-sdk';import { WebAuthnExtension } from '@magic-ext/webauthn';const magic = new Magic('pk_live_4F94264055282A4A', { // API key added extensions: [new WebAuthnExtension()],});// Initiates the flow to update WebAuthn Device's nickname.try { await magic.webauthn.updateInfo({ id, nickname }); } catch (e) { // Handle errors if required!}import { Magic, SDKErrorCode } from 'magic-sdk';import { WebAuthnExtension } from '@magic-ext/webauthn';const magic = new Magic('pk_live_4F94264055282A4A', { // API key added extensions: [new WebAuthnExtension()],});// Initiates the flow to unregister WebAuthn Device.try { /* Assuming user is logged in */ await magic.webauthn.unregisterDevice(id); } catch (e) { // Handle errors if required!}import { SDKError } from 'magic-sdk';try { // Something async...catch (err) { if (err instanceof SDKError) { // Handle... }}import { SDKErrorCode } from 'magic-sdk';SDKErrorCode.MissingApiKey;SDKErrorCode.ModalNotReady;SDKErrorCode.MalformedResponse;// and so forth...// Please reference the `Enum Key` column of the error table below.import { RPCError } from 'magic-sdk';try { // Something async...catch (err) { if (err instanceof RPCError) { // Handle... }}import { RPCErrorCode } from 'magic-sdk';RPCErrorCode.MagicLinkExpired;RPCErrorCode.UserAlreadyLoggedIn;RPCErrorCode.ParseError;RPCErrorCode.MethodNotFound;RPCErrorCode.InternalError;// and so forth...// Please reference the `Enum Key` column of the error table below.import { ExtensionError } from 'magic-sdk';try { // Something async...catch (err) { if (err instanceof ExtensionError) { // Handle... }}const req = magic.auth.loginWithMagicLink({ email: 'hello@magic.link' });req .on('email-sent', () => { /* ... */ }) .then(DIDToken => { /* ... */ }) .once('email-not-deliverable', () => { /* ... */ }) .catch(error => { /* ... */ }) .on('error', error => { /* ... */ });import { Magic } from 'magic-sdk';const magic = new Magic('pk_live_4F94264055282A4A'); // API key addedconst email = 'example@magic.link';if (await magic.user.isLoggedIn()) { const didToken = await magic.user.getIdToken(); // Do something with the DID token. // For instance, this could be a `fetch` call // to a protected backend endpoint. document.getElementById('your-access-token').innerHTML = didToken;} else { // Log in the user const user = await magic.auth.loginWithMagicLink({ email }); }
::
/ ::

대기줄

분명한