
Product
Introducing Repository Access Permissions and Custom Roles
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.
@paraview/auth-sdk
Advanced tools
一个功能强大的身份认证 SDK,支持多种登录方式和多因素认证 (MFA)。
npm install @paraview/auth-sdk
import { createAuthSDK } from '@paraview/auth-sdk';
// 初始化 SDK
const authSDK = createAuthSDK({
baseURL: 'https://your-api-server.com',
timeout: 10000
});
// 用户登录
const loginResult = await authSDK.login({
type: 'password',
username: 'user@example.com',
password: 'password123'
});
if (loginResult.success) {
console.log('登录成功:', loginResult.userInfo);
// 如果需要 MFA 验证
if (loginResult.requiresMFA) {
const mfaResult = await authSDK.verifyMFA({
code: '123456',
methodType: 'OTP'
});
console.log('MFA 验证结果:', mfaResult);
}
}
import React, { useState } from 'react';
import { createAuthSDK } from '@paraview/auth-sdk';
const authSDK = createAuthSDK({
baseURL: 'https://your-api-server.com'
});
function LoginComponent() {
const [user, setUser] = useState(null);
const handleLogin = async (credentials) => {
try {
const result = await authSDK.login(credentials);
if (result.success) {
setUser(result.userInfo);
}
} catch (error) {
console.error('登录失败:', error);
}
};
return (
<div>
{user ? (
<div>欢迎, {user.username}!</div>
) : (
<LoginForm onLogin={handleLogin} />
)}
</div>
);
}
interface SDKConfig {
baseURL: string; // API 服务器地址
timeout?: number; // 请求超时时间 (ms)
apiKey?: string; // API 密钥
headers?: Record<string, string>; // 自定义请求头
endpoints?: { // 自定义端点配置
passwordLogin?: EndpointConfig;
oauthLogin?: EndpointConfig;
mfaVerify?: EndpointConfig;
// ... 更多端点配置
};
}
// 登录
login(request: LoginRequest): Promise<LoginResponse>
// 登出
logout(sessionId?: string): Promise<APIResponse<{ success: boolean }>>
// MFA 验证
verifyMFA(request: MFAVerificationRequest): Promise<MFAVerificationResponse>
// 绑定 MFA 方法
bindMFA(request: MFABindRequest): Promise<MFABindResponse>
// 解绑 MFA 方法
unbindMFA(request: MFAUnbindRequest): Promise<MFAUnbindResponse>
// 获取用户 MFA 方法
getMFAMethods(userId: string): Promise<APIResponse<MFAMethod[]>>
// 获取用户绑定信息
getUserBindings(userId: string): Promise<APIResponse<MFABinding[]>>
// 获取可用认证方法
getAvailableAuthMethods(): Promise<APIResponse<AvailableMFAMethod[]>>
支持多种登录方式:
interface LoginRequest {
type: 'password' | 'oauth' | 'oauth_callback' | 'cas' | 'cas_validate';
// 密码登录
username?: string;
password?: string;
// OAuth 登录
provider?: 'google' | 'github' | 'microsoft' | 'facebook' | 'custom';
redirectUri?: string;
state?: string;
scopes?: string[];
// CAS 登录
service?: string;
casServerUrl?: string;
// 自定义端点
customEndpoint?: string;
customMethod?: 'GET' | 'POST';
}
支持多种 MFA 方法:
<!-- UMD 格式 -->
<script src="https://unpkg.com/@paraview/auth-sdk/dist/auth-sdk.umd.js"></script>
<script>
const sdk = AuthSDK.createAuthSDK({
baseURL: 'https://api.example.com'
});
</script>
// ES Modules
import { createAuthSDK } from '@paraview/auth-sdk';
// CommonJS
const { createAuthSDK } = require('@paraview/auth-sdk');
import { createAuthSDK } from '@paraview/auth-sdk';
const authSDK = createAuthSDK({
baseURL: process.env.REACT_APP_API_URL
});
const sdk = createAuthSDK({
baseURL: 'https://api.example.com',
endpoints: {
passwordLogin: { url: '/custom/login', method: 'POST' },
mfaVerify: { url: '/custom/mfa/verify', method: 'POST' },
logout: { url: '/custom/logout', method: 'DELETE' }
}
});
const sdk = createAuthSDK({
baseURL: 'https://api.example.com',
headers: {
'Authorization': 'Bearer your-token',
'X-Client-Version': '1.0.0',
'X-Custom-Header': 'custom-value'
}
});
try {
const result = await authSDK.login(credentials);
if (result.success) {
// 处理成功情况
console.log('登录成功:', result.userInfo);
} else {
// 处理业务逻辑错误
console.error('登录失败:', result.message);
}
} catch (error) {
// 处理网络错误、系统错误等
if (error.code === 'NETWORK_ERROR') {
console.error('网络错误:', error.message);
} else if (error.code === 'TIMEOUT') {
console.error('请求超时:', error.message);
} else {
console.error('未知错误:', error.message);
}
}
查看 examples/ 目录获取完整的使用示例:
examples/sdk-demo.html - 浏览器直接使用示例examples/node-demo.cjs - Node.js CommonJS 示例examples/node-demo.mjs - Node.js ES Module 示例examples/es6-demo.js - ES6 模块示例examples/react-demo.tsx - React 组件示例# 安装依赖
npm install
# 构建 SDK
npm run build
# 开发模式(自动重新构建)
npm run dev
# 运行 Node.js 示例
node examples/node-demo.mjs
# 在浏览器中打开 HTML 示例
open examples/sdk-demo.html
MIT
欢迎提交 Issue 和 Pull Request!
如果您在使用过程中遇到问题,请通过以下方式联系我们:
FAQs
Authentication SDK with support for password, OAuth, CAS login and MFA
The npm package @paraview/auth-sdk receives a total of 4 weekly downloads. As such, @paraview/auth-sdk popularity was classified as not popular.
We found that @paraview/auth-sdk demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.

Product
Socket MCP now lets AI assistants review org alerts, investigate threats using the Socket threat feed, and inspect package files in addition to dependency scoring.

Product
Socket Firewall blocks malicious VS Code and Open VSX extensions before install, protecting developers from compromised editor marketplaces.