The AuthsignalProvider component allows you to use the useAuthsignal hook. Render the AuthsignalProvider component
at the root of your application so that it is available everywhere you need it.
Copy
Ask AI
import { AuthsignalProvider } from "@authsignal/react";import { Checkout } from "./Checkout";export function App() { return ( <AuthsignalProvider tenantId="{{TENANT_ID}}" baseUrl="{{BASE_URL}}"> <Checkout /> </AuthsignalProvider> );}
The AuthsignalProvider component accepts the following props:
A callback function that is called when the challenge is successful. It receives an object with a
token property. This token should be sent to your server to validate the
challenge.
Alternatively, you can use the startChallengeAsync function, which returns a Promise that resolves with a token when the challenge is successful.
It will throw a ChallengeError if the user cancels the challenge or the token expires.
Copy
Ask AI
import { useAuthsignal, ChallengeError } from "@authsignal/react";export function Checkout() { const { startChallengeAsync } = useAuthsignal(); const handlePayment = async () => { const response = await fetch("/api/payment", { method: "POST", headers: { "Content-Type": "application/json", }, }); const data = await response.json(); if (data.token) { try { const { token } = await startChallengeAsync({ token: data.token, }); // Challenge was successful // Send the token to your server to validate the challenge } catch (e) { if (e instanceof ChallengeError) { if (e.code === "USER_CANCELED") { // User cancelled the challenge } else if (e.code === "TOKEN_EXPIRED") { // Token expired } } } } }; return ( <div> <button type="button" onClick={handlePayment}> Pay </button> </div> );}