JavaScript SDK
认证与用户
登录、注册、OAuth、短信、密码重置、邮箱验证和用户资料管理。
client.auth 模块处理所有认证流程。令牌自动存储,401 时自动刷新。
邮箱与密码
登录
const result = await client.auth.login("admin@raisfast.dev", "admin123");
console.log(result.access_token);
console.log(result.refresh_token);
console.log(result.user);注册
await client.auth.register({
email: "user@example.com",
password: "strongpassword",
username: "newuser",
});登出
await client.auth.logout();获取当前用户
const me = await client.auth.getMe();检查认证状态
client.auth.isAuthenticated; // boolean
client.auth.token; // string | null
client.auth.user; // User | null自动刷新
当请求返回 401 且存在 refresh token 时,SDK 自动调用 /auth/refresh 并重试原始请求。如果刷新失败,认证存储会被清除。
OAuth
列出提供商
const providers = await client.auth.listOAuthProviders();
// [{ name: "github", display_name: "GitHub", ... }, ...]获取重定向 URL
const url = client.auth.getOAuthRedirectURL("github");
// 将用户重定向到此 URL 进行授权
window.location.href = url;处理回调
用户被重定向回来后,URL 中会携带 code:
const result = await client.auth.authWithOAuth({
provider: "github",
code: "oauth_code_from_callback",
redirect_url: "https://yourapp.com/callback",
});
// result.access_token, result.user管理绑定
// 列出已绑定的提供商
const bindings = await client.auth.listOAuthBindings();
// 解绑提供商
await client.auth.unbindOAuth("github");短信(手机号)
发送验证码
await client.auth.sendSmsCode("+8613800138000", "login");验证并登录
const result = await client.auth.verifySms({
phone: "+8613800138000",
code: "123456",
purpose: "login",
});绑定手机号
await client.auth.bindPhone({
phone: "+8613800138000",
code: "123456",
});密码重置
// 第 1 步:请求重置(发送含 token 的邮件)
await client.auth.requestPasswordReset("user@example.com");
// 第 2 步:用 token 确认重置
await client.auth.confirmPasswordReset({
token: "reset_token_from_email",
new_password: "newStrongPassword",
});管理员设置密码
await client.auth.setPassword({
email: "user@example.com",
new_password: "temporaryPassword",
});邮箱验证
// 请求验证邮件
await client.auth.resendVerification("user@example.com");
// 用 token 验证
await client.auth.verifyEmail({ token: "verification_token" });认证配置
获取服务器的认证配置(启用的方法、OAuth 提供商等):
const config = await client.auth.getConfig();凭证管理
// 列出所有凭证(邮箱、手机号、OAuth 绑定)
const credentials = await client.auth.listCredentials();
// 绑定邮箱
await client.auth.bindEmail({
email: "user@example.com",
password: "password",
});
// 删除凭证
await client.auth.deleteCredential(credentialId);用户资料
获取资料
const me = await client.users.getMe();更新资料
const updated = await client.users.updateMe({
username: "newname",
bio: "全栈开发者",
website: "https://example.com",
avatar: "media_id",
social_links: {
github: "https://github.com/user",
twitter: "https://twitter.com/user",
},
});修改密码
await client.auth.changePassword({
old_password: "currentPassword",
new_password: "newStrongPassword",
});查看其他用户
const user = await client.users.getUser("user_id");列出用户
const result = await client.users.listUsers(1, 20);认证状态变化
const unsubscribe = client.authStore.onChange((token, user) => {
if (token) {
console.log("已登录:", user?.email);
} else {
console.log("已登出");
}
}, true); // 立即触发一次当前状态
// 之后:停止监听
unsubscribe();