@ connect-xyz/auth- react
一个能使前端反应应用程序能与Connect Auth产品无缝相接的SDK反应.
Connect Auth为加密存款提供了安全,自定义的认证流程. 学习更多在 连接 Auth 文档。 。 。 .
所需资源
- 反应18.0.0或以上
- 反应DOM 18.0.0或以上
安装
npm install @connect-xyz/auth-react
开始
遵循这些简单的步骤将连接 Auth 集成到您的 React 应用程序中 :
1. 导入 Auth 组件
import { Auth } from '@connect-xyz/auth-react';
2. 在您的应用程序中添加 Auth 组件
function App() {
const jwt = 'your-jwt-token'; // Obtain this from your backend authentication
return (
<Auth
jwt={jwt}
env="production" // or "sandbox" for testing
theme="auto" // 'auto' (default), 'light', or 'dark'
/>
);
}
3. 配置事件召回(可选)
听从 Auth SDK 处理用户交互的事件 :
function App() {
const jwt = 'your-jwt-token';
const handleError = ({ errorCode, reason }) => {
console.log('Auth error occurred:', errorCode, 'Reason:', reason);
};
const handleClose = () => {
console.log('Auth widget closed');
};
const handleDeposit = ({ data }) => {
console.log('Deposit data:', data);
};
const handleEvent = ({ type, data }) => {
console.log('Auth event:', type, 'Data:', data);
};
const handleLoaded = () => {
console.log('Auth widget loaded and ready');
};
return (
<Auth
jwt={jwt}
env="production"
theme="auto" // 'auto' (default), 'light', or 'dark'
onError={handleError}
onClose={handleClose}
onDeposit={handleDeposit}
onEvent={handleEvent}
onLoaded={handleLoaded}
/>
);
}
关于召回事件及其有效载荷的详情,见 Auth SDK 回调文档。 。 。 .
完整示例
以下是将 Connect Auth 整合到您的 React 应用程序的完整实例 :
import { Auth } from '@connect-xyz/auth-react';
import React from 'react';
function App() {
// JWT token should be obtained from your backend
const jwt = 'your-jwt-token';
// Choose environment: "production" for live apps, "sandbox" for testing
const environment = 'production';
return (
<div className="App">
<h1>My Crypto App</h1>
<Auth
jwt={jwt}
env={environment}
theme="auto" // 'auto' (default), 'light', or 'dark'
onError={({ errorCode, reason }) => {
console.log('Error:', errorCode, 'Reason:', reason);
}}
onClose={() => {
console.log('Auth widget closed');
}}
onDeposit={({ data }) => {
console.log('Deposit successful:', data);
}}
onEvent={({ type, data }) => {
console.log('Event type:', type, 'Event data:', data);
}}
onLoaded={() => {
console.log('Auth widget loaded and ready');
}}
/>
</div>
);
}
export default App;
API 参考
Auth 组件道具
| Prop | 类型 | 需求 | 默认 | 说明 |
|---|---|---|---|---|
jwt | string | 对 | - 说吧 | 连接 Auth 认证的 JWT 令牌 |
env | "production" | "sandbox" | 没有 | "production" | 目标环境 |
theme | "auto" | "light" | "dark" | 没有 | "auto" | 界面的主题模式 |
onError | ({ errorCode, reason }) => void | 没有 | - 说吧 | 调用错误事件 |
onClose | () => void | 没有 | - 说吧 | 部件关闭时召回 |
onDeposit | ({ data }) => void | 没有 | - 说吧 | 收回存款 |
onEvent | ({ type, data }) => void | 没有 | - 说吧 | 召回一般活动 |
onLoaded | () => void | 没有 | - 说吧 | 部件装入并准备就绪时召回 |
更多信息与支持
欲了解有关Connect Auth的全面文献或支持,请访问 页 次。 。 。 .