starchild-auth
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese🔑 Starchild Auth SDK
🔑 Starchild Auth SDK
Integrate Starchild OAuth login into any web application — React, Vue, or plain HTML. The SDK handles the full OAuth popup flow, token management, automatic refresh, and session restoration.
将Starchild OAuth登录集成到任意Web应用中——支持React、Vue或纯HTML项目。该SDK可处理完整的OAuth弹窗流程、令牌管理、自动刷新以及会话恢复。
When to Use
使用场景
- Adding Starchild login to a web project
- Implementing OAuth authentication with iamstarchild.com
- Building a third-party app that needs Starchild user identity
- Replacing a custom auth flow with Starchild SSO
- 为Web项目添加Starchild登录功能
- 基于iamstarchild.com实现OAuth身份验证
- 开发需要获取Starchild用户身份的第三方应用
- 使用Starchild单点登录替换自定义认证流程
Step 1 — Register Your App & Get Client ID
步骤1 — 注册应用并获取客户端ID
- Go to iamstarchild.com → click the More menu (left sidebar) → OAuth Apps
- Direct link:
https://iamstarchild.com/oauth-apps
- Direct link:
- Click Create App
- Fill in:
- App Name (required): your application name
- Allowed Origin (required): the origin of your website (e.g. ). For local development, use
https://your-app.com. Must be a valid origin — no paths, query params, or hash allowed. Non-localhost origins must usehttp://localhost:3000.https:// - Description (optional): only visible to you
- After creation, a dialog will show your Client ID — copy it immediately
⚠️ Keep your Client ID safe. Never commit it directly in source code — use environment variables. Each user can create up to 10 OAuth apps.
- 访问 iamstarchild.com → 点击左侧侧边栏的更多菜单 → OAuth应用
- 直接链接:
https://iamstarchild.com/oauth-apps
- 直接链接:
- 点击创建应用
- 填写信息:
- 应用名称(必填):你的应用名称
- 允许的源地址(必填):你的网站源地址(例如 )。本地开发时使用
https://your-app.com。必须是有效的源地址——不允许包含路径、查询参数或哈希值。非本地主机的源地址必须使用http://localhost:3000。https:// - 描述(可选):仅对你可见
- 创建完成后,弹窗会显示你的客户端ID——请立即复制保存
⚠️ 请妥善保管你的客户端ID。切勿直接提交到源代码中——请使用环境变量存储。 每个用户最多可创建10个OAuth应用。
Step 2 — Install the SDK
步骤2 — 安装SDK
npm / yarn / pnpm
npm / yarn / pnpm
bash
undefinedbash
undefinednpm
npm
npm install starchild-auth-sdk
npm install starchild-auth-sdk
yarn
yarn
yarn add starchild-auth-sdk
yarn add starchild-auth-sdk
pnpm
pnpm
pnpm add starchild-auth-sdk
undefinedpnpm add starchild-auth-sdk
undefinedCDN (for plain HTML)
CDN(适用于纯HTML项目)
The SDK provides two builds. For plain tags, use the UMD build:
<script>html
<!-- unpkg (recommended) -->
<script src="https://unpkg.com/starchild-auth-sdk/dist/starchild-auth.umd.cjs"></script>
<!-- npmmirror (alternative, for China mainland) -->
<script src="https://registry.npmmirror.com/starchild-auth-sdk/latest/files/dist/starchild-auth.umd.cjs"></script>⚠️ Do NOT usewith a plainstarchild-auth.jstag — that file is an ES Module and will fail silently. Use<script>forstarchild-auth.umd.cjstags, or use<script>with the ESM build (see examples below).<script type="module">
💡 CDN 加载失败? 如果两个 CDN 都无法访问(如在受限网络环境中),可以用下载到本地后通过相对路径引用:curlbashcurl -sL "https://unpkg.com/starchild-auth-sdk/dist/starchild-auth.umd.cjs" -o starchild-auth.umd.cjs然后<script src="starchild-auth.umd.cjs"></script>
When loaded via UMD CDN, the class is available directly as (it's the constructor itself, not a namespace object).
window.StarchildAuthSDK提供两种构建版本。对于普通标签,请使用UMD构建版本:
<script>html
<!-- unpkg(推荐) -->
<script src="https://unpkg.com/starchild-auth-sdk/dist/starchild-auth.umd.cjs"></script>
<!-- npmmirror(备选,适用于中国大陆地区) -->
<script src="https://registry.npmmirror.com/starchild-auth-sdk/latest/files/dist/starchild-auth.umd.cjs"></script>⚠️ 请勿将用于普通starchild-auth.js标签——该文件是ES模块,会静默失败。对于<script>标签,请使用<script>,或结合ESM构建版本使用starchild-auth.umd.cjs(见下方示例)。<script type="module">
💡 CDN 加载失败? 如果两个CDN都无法访问(如受限网络环境),可以使用下载到本地后通过相对路径引用:curlbashcurl -sL "https://unpkg.com/starchild-auth-sdk/dist/starchild-auth.umd.cjs" -o starchild-auth.umd.cjs然后使用<script src="starchild-auth.umd.cjs"></script>
通过UMD CDN加载后,类将直接作为可用(它本身是构造函数,而非命名空间对象)。
window.StarchildAuthStep 3 — Initialize the SDK
步骤3 — 初始化SDK
typescript
import { StarchildAuth } from 'starchild-auth-sdk'
const auth = new StarchildAuth({
clientId: 'your-client-id',
// Called after successful login (popup or auto-login session restore)
onLogin: ({ accessToken, refreshToken, expiresIn, userInfo }) => {
console.log('Logged in:', userInfo.agentName)
console.log('Avatar:', userInfo.agentAvatar)
},
// Called after logout
onLogout: () => {
console.log('Logged out')
},
// Token auto-refreshes every 12 minutes
onTokenRefresh: (newAccessToken) => {
console.log('Token refreshed')
},
// Called when refresh fails (session expired)
onTokenRefreshFailed: () => {
console.log('Session expired, please log in again')
},
})Auto-login: By default (), the SDK checks localStorage for a stored refresh token on initialization and automatically restores the session. This meansautoLogin: truemay fire immediately after construction without callingonLogin.login()
typescript
import { StarchildAuth } from 'starchild-auth-sdk'
const auth = new StarchildAuth({
clientId: 'your-client-id',
// 登录成功后调用(弹窗登录或自动登录恢复会话时)
onLogin: ({ accessToken, refreshToken, expiresIn, userInfo }) => {
console.log('Logged in:', userInfo.agentName)
console.log('Avatar:', userInfo.agentAvatar)
},
// 登出后调用
onLogout: () => {
console.log('Logged out')
},
// 令牌每12分钟自动刷新
onTokenRefresh: (newAccessToken) => {
console.log('Token refreshed')
},
// 刷新失败时调用(会话过期)
onTokenRefreshFailed: () => {
console.log('Session expired, please log in again')
},
})自动登录:默认情况下(),SDK初始化时会检查localStorage中存储的刷新令牌,并自动恢复会话。这意味着无需调用autoLogin: true,login()可能会在实例创建后立即触发。onLogin
Step 4 — Login / Logout / Status
步骤4 — 登录/登出/状态检查
typescript
// Trigger login (opens Starchild OAuth popup)
const { accessToken, refreshToken, expiresIn, userInfo } = await auth.login()
// Logout (clears tokens, revokes refresh token via API)
await auth.logout()
// Check login status
const isLoggedIn: boolean = auth.isLoggedIn()
// Get current access token (null if not logged in)
const token: string | null = auth.getToken()
// Get current user info (null if not logged in)
const user: UserInfo | null = auth.getUserInfo()
// user = { userInfoId: string, agentName: string, agentAvatar: string }
// Manually trigger token refresh (normally automatic every 12 min)
const newToken: string = await auth.refreshToken()
// Destroy instance (remove listeners, clear timers, clear tokens)
auth.destroy()typescript
// 触发登录(打开Starchild OAuth弹窗)
const { accessToken, refreshToken, expiresIn, userInfo } = await auth.login()
// 登出(清除令牌,通过API撤销刷新令牌)
await auth.logout()
// 检查登录状态
const isLoggedIn: boolean = auth.isLoggedIn()
// 获取当前访问令牌(未登录时返回null)
const token: string | null = auth.getToken()
// 获取当前用户信息(未登录时返回null)
const user: UserInfo | null = auth.getUserInfo()
// user = { userInfoId: string, agentName: string, agentAvatar: string }
// 手动触发令牌刷新(通常每12分钟自动刷新)
const newToken: string = await auth.refreshToken()
// 销毁实例(移除监听器、清除定时器、清除令牌)
auth.destroy()Framework Examples
框架示例
React
React
tsx
// src/hooks/useStarchildAuth.ts
import { useState, useEffect, useRef, useCallback } from 'react'
import { StarchildAuth } from 'starchild-auth-sdk'
import type { UserInfo, LoginResult } from 'starchild-auth-sdk'
export function useStarchildAuth() {
const [user, setUser] = useState<UserInfo | null>(null)
const [token, setToken] = useState<string | null>(null)
const [loading, setLoading] = useState(true)
const authRef = useRef<StarchildAuth | null>(null)
useEffect(() => {
const auth = new StarchildAuth({
clientId: import.meta.env.VITE_STARCHILD_CLIENT_ID,
onLogin: ({ accessToken, userInfo }: LoginResult) => {
setUser(userInfo)
setToken(accessToken)
setLoading(false)
},
onLogout: () => {
setUser(null)
setToken(null)
},
onTokenRefresh: (newToken: string) => {
setToken(newToken)
},
onTokenRefreshFailed: () => {
setUser(null)
setToken(null)
},
// autoLogin: true (default) — will restore session from localStorage
})
authRef.current = auth
// If auto-login doesn't fire onLogin (no stored session), stop loading
const timeout = setTimeout(() => setLoading(false), 1500)
return () => {
clearTimeout(timeout)
auth.destroy()
}
}, [])
const login = useCallback(async () => {
if (!authRef.current) return
return await authRef.current.login()
}, [])
const logout = useCallback(async () => {
if (!authRef.current) return
await authRef.current.logout()
}, [])
return { user, token, loading, login, logout, isLoggedIn: !!user }
}tsx
// src/App.tsx
import { useStarchildAuth } from './hooks/useStarchildAuth'
function App() {
const { user, loading, login, logout, isLoggedIn } = useStarchildAuth()
if (loading) return <div>Loading...</div>
return (
<div>
{isLoggedIn ? (
<div>
<p>Welcome, {user?.agentName}</p>
<img src={user?.agentAvatar} alt="avatar" width={40} />
<button onClick={logout}>Logout</button>
</div>
) : (
<button onClick={login}>Login with Starchild</button>
)}
</div>
)
}
export default Apptsx
// src/hooks/useStarchildAuth.ts
import { useState, useEffect, useRef, useCallback } from 'react'
import { StarchildAuth } from 'starchild-auth-sdk'
import type { UserInfo, LoginResult } from 'starchild-auth-sdk'
export function useStarchildAuth() {
const [user, setUser] = useState<UserInfo | null>(null)
const [token, setToken] = useState<string | null>(null)
const [loading, setLoading] = useState(true)
const authRef = useRef<StarchildAuth | null>(null)
useEffect(() => {
const auth = new StarchildAuth({
clientId: import.meta.env.VITE_STARCHILD_CLIENT_ID,
onLogin: ({ accessToken, userInfo }: LoginResult) => {
setUser(userInfo)
setToken(accessToken)
setLoading(false)
},
onLogout: () => {
setUser(null)
setToken(null)
},
onTokenRefresh: (newToken: string) => {
setToken(newToken)
},
onTokenRefreshFailed: () => {
setUser(null)
setToken(null)
},
// autoLogin: true(默认值)——将从localStorage恢复会话
})
authRef.current = auth
// 如果自动登录未触发onLogin(无存储会话),停止加载状态
const timeout = setTimeout(() => setLoading(false), 1500)
return () => {
clearTimeout(timeout)
auth.destroy()
}
}, [])
const login = useCallback(async () => {
if (!authRef.current) return
return await authRef.current.login()
}, [])
const logout = useCallback(async () => {
if (!authRef.current) return
await authRef.current.logout()
}, [])
return { user, token, loading, login, logout, isLoggedIn: !!user }
}tsx
// src/App.tsx
import { useStarchildAuth } from './hooks/useStarchildAuth'
function App() {
const { user, loading, login, logout, isLoggedIn } = useStarchildAuth()
if (loading) return <div>Loading...</div>
return (
<div>
{isLoggedIn ? (
<div>
<p>Welcome, {user?.agentName}</p>
<img src={user?.agentAvatar} alt="avatar" width={40} />
<button onClick={logout}>Logout</button>
</div>
) : (
<button onClick={login}>Login with Starchild</button>
)}
</div>
)
}
export default AppVue 3 (Composition API)
Vue 3(组合式API)
typescript
// src/composables/useStarchildAuth.ts
import { ref, onMounted, onUnmounted } from 'vue'
import { StarchildAuth } from 'starchild-auth-sdk'
import type { UserInfo, LoginResult } from 'starchild-auth-sdk'
export function useStarchildAuth() {
const user = ref<UserInfo | null>(null)
const token = ref<string | null>(null)
const loading = ref(true)
let authInstance: StarchildAuth | null = null
onMounted(() => {
authInstance = new StarchildAuth({
clientId: import.meta.env.VITE_STARCHILD_CLIENT_ID,
onLogin: ({ accessToken, userInfo }: LoginResult) => {
user.value = userInfo
token.value = accessToken
loading.value = false
},
onLogout: () => {
user.value = null
token.value = null
},
onTokenRefresh: (newToken: string) => {
token.value = newToken
},
onTokenRefreshFailed: () => {
user.value = null
token.value = null
},
})
// If no stored session, stop loading after timeout
setTimeout(() => { loading.value = false }, 1500)
})
onUnmounted(() => {
authInstance?.destroy()
})
const login = async () => {
if (!authInstance) return
return await authInstance.login()
}
const logout = async () => {
if (!authInstance) return
await authInstance.logout()
}
const isLoggedIn = () => !!user.value
return { user, token, loading, login, logout, isLoggedIn }
}vue
<!-- src/App.vue -->
<template>
<div v-if="loading">Loading...</div>
<div v-else-if="user">
<p>Welcome, {{ user.agentName }}</p>
<img :src="user.agentAvatar" alt="avatar" width="40" />
<button @click="logout">Logout</button>
</div>
<div v-else>
<button @click="login">Login with Starchild</button>
</div>
</template>
<script setup lang="ts">
import { useStarchildAuth } from './composables/useStarchildAuth'
const { user, loading, login, logout } = useStarchildAuth()
</script>typescript
// src/composables/useStarchildAuth.ts
import { ref, onMounted, onUnmounted } from 'vue'
import { StarchildAuth } from 'starchild-auth-sdk'
import type { UserInfo, LoginResult } from 'starchild-auth-sdk'
export function useStarchildAuth() {
const user = ref<UserInfo | null>(null)
const token = ref<string | null>(null)
const loading = ref(true)
let authInstance: StarchildAuth | null = null
onMounted(() => {
authInstance = new StarchildAuth({
clientId: import.meta.env.VITE_STARCHILD_CLIENT_ID,
onLogin: ({ accessToken, userInfo }: LoginResult) => {
user.value = userInfo
token.value = accessToken
loading.value = false
},
onLogout: () => {
user.value = null
token.value = null
},
onTokenRefresh: (newToken: string) => {
token.value = newToken
},
onTokenRefreshFailed: () => {
user.value = null
token.value = null
},
})
// 如果无存储会话,超时后停止加载
setTimeout(() => { loading.value = false }, 1500)
})
onUnmounted(() => {
authInstance?.destroy()
})
const login = async () => {
if (!authInstance) return
return await authInstance.login()
}
const logout = async () => {
if (!authInstance) return
await authInstance.logout()
}
const isLoggedIn = () => !!user.value
return { user, token, loading, login, logout, isLoggedIn }
}vue
<!-- src/App.vue -->
<template>
<div v-if="loading">Loading...</div>
<div v-else-if="user">
<p>Welcome, {{ user.agentName }}</p>
<img :src="user.agentAvatar" alt="avatar" width="40" />
<button @click="logout">Logout</button>
</div>
<div v-else>
<button @click="login">Login with Starchild</button>
</div>
</template>
<script setup lang="ts">
import { useStarchildAuth } from './composables/useStarchildAuth'
const { user, loading, login, logout } = useStarchildAuth()
</script>Plain HTML — UMD (recommended for <script>
tags)
<script>纯HTML — UMD(推荐用于<script>
标签)
<script>💡 如果 CDN 加载失败,参考上方 Step 2 中的 CDN 备选源和本地下载方案。
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Starchild Auth Demo</title>
<!-- UMD build: works with plain <script> tags -->
<!-- If CDN is blocked, download the file locally and use a relative path instead -->
<script src="https://unpkg.com/starchild-auth-sdk/dist/starchild-auth.umd.cjs"></script>
</head>
<body>
<div id="app">
<div id="logged-out">
<button id="login-btn">Login with Starchild</button>
</div>
<div id="logged-in" style="display: none">
<p>Welcome, <span id="user-name"></span></p>
<img id="user-avatar" width="40" />
<button id="logout-btn">Logout</button>
</div>
</div>
<script>
// UMD exposes window.StarchildAuth as the constructor directly
if (typeof window.StarchildAuth !== 'function') {
document.getElementById('logged-out').innerHTML =
'<p style="color:red">SDK failed to load. Check if the CDN script loaded correctly.</p>'
throw new Error('StarchildAuth SDK not loaded')
}
var auth = new StarchildAuth({
clientId: 'your-client-id',
onLogin: function (result) {
showLoggedIn(result.userInfo)
},
onLogout: function () {
showLoggedOut()
},
onTokenRefresh: function (newToken) {
console.log('Token refreshed')
},
onTokenRefreshFailed: function () {
showLoggedOut()
},
// autoLogin: true (default) — restores session automatically
})
document.getElementById('login-btn').addEventListener('click', async function () {
try {
await auth.login()
} catch (err) {
console.error('Login failed:', err.message)
}
})
document.getElementById('logout-btn').addEventListener('click', async function () {
await auth.logout()
})
function showLoggedIn(user) {
document.getElementById('logged-out').style.display = 'none'
document.getElementById('logged-in').style.display = 'block'
document.getElementById('user-name').textContent = user.agentName
if (user.agentAvatar) {
document.getElementById('user-avatar').src = user.agentAvatar
}
}
function showLoggedOut() {
document.getElementById('logged-out').style.display = 'block'
document.getElementById('logged-in').style.display = 'none'
}
</script>
</body>
</html>💡 如果CDN加载失败,请参考步骤2中的CDN备选源和本地下载方案。
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Starchild Auth Demo</title>
<!-- UMD构建版本:适用于普通<script>标签 -->
<!-- 如果CDN被屏蔽,可下载文件到本地并使用相对路径 -->
<script src="https://unpkg.com/starchild-auth-sdk/dist/starchild-auth.umd.cjs"></script>
</head>
<body>
<div id="app">
<div id="logged-out">
<button id="login-btn">Login with Starchild</button>
</div>
<div id="logged-in" style="display: none">
<p>Welcome, <span id="user-name"></span></p>
<img id="user-avatar" width="40" />
<button id="logout-btn">Logout</button>
</div>
</div>
<script>
// UMD版本直接暴露window.StarchildAuth作为构造函数
if (typeof window.StarchildAuth !== 'function') {
document.getElementById('logged-out').innerHTML =
'<p style="color:red">SDK加载失败,请检查CDN脚本是否正确加载。</p>'
throw new Error('StarchildAuth SDK not loaded')
}
var auth = new StarchildAuth({
clientId: 'your-client-id',
onLogin: function (result) {
showLoggedIn(result.userInfo)
},
onLogout: function () {
showLoggedOut()
},
onTokenRefresh: function (newToken) {
console.log('Token refreshed')
},
onTokenRefreshFailed: function () {
showLoggedOut()
},
// autoLogin: true(默认值)——自动恢复会话
})
document.getElementById('login-btn').addEventListener('click', async function () {
try {
await auth.login()
} catch (err) {
console.error('Login failed:', err.message)
}
})
document.getElementById('logout-btn').addEventListener('click', async function () {
await auth.logout()
})
function showLoggedIn(user) {
document.getElementById('logged-out').style.display = 'none'
document.getElementById('logged-in').style.display = 'block'
document.getElementById('user-name').textContent = user.agentName
if (user.agentAvatar) {
document.getElementById('user-avatar').src = user.agentAvatar
}
}
function showLoggedOut() {
document.getElementById('logged-out').style.display = 'block'
document.getElementById('logged-in').style.display = 'none'
}
</script>
</body>
</html>Plain HTML — ES Module (alternative)
纯HTML — ES模块(备选)
If you prefer ES Module syntax, use :
<script type="module">html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Starchild Auth Demo (ESM)</title>
</head>
<body>
<button id="login-btn">Login with Starchild</button>
<div id="logged-in" style="display: none">
<p>Welcome, <span id="user-name"></span></p>
<button id="logout-btn">Logout</button>
</div>
<script type="module">
import { StarchildAuth } from 'https://unpkg.com/starchild-auth-sdk/dist/starchild-auth.js'
const auth = new StarchildAuth({
clientId: 'your-client-id',
onLogin: ({ userInfo }) => {
document.getElementById('login-btn').style.display = 'none'
document.getElementById('logged-in').style.display = 'block'
document.getElementById('user-name').textContent = userInfo.agentName
},
onLogout: () => {
document.getElementById('login-btn').style.display = 'block'
document.getElementById('logged-in').style.display = 'none'
},
})
document.getElementById('login-btn').addEventListener('click', () => auth.login())
document.getElementById('logout-btn').addEventListener('click', () => auth.logout())
</script>
</body>
</html>如果你偏好ES模块语法,请使用:
<script type="module">html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Starchild Auth Demo (ESM)</title>
</head>
<body>
<button id="login-btn">Login with Starchild</button>
<div id="logged-in" style="display: none">
<p>Welcome, <span id="user-name"></span></p>
<button id="logout-btn">Logout</button>
</div>
<script type="module">
import { StarchildAuth } from 'https://unpkg.com/starchild-auth-sdk/dist/starchild-auth.js'
const auth = new StarchildAuth({
clientId: 'your-client-id',
onLogin: ({ userInfo }) => {
document.getElementById('login-btn').style.display = 'none'
document.getElementById('logged-in').style.display = 'block'
document.getElementById('user-name').textContent = userInfo.agentName
},
onLogout: () => {
document.getElementById('login-btn').style.display = 'block'
document.getElementById('logged-in').style.display = 'none'
},
})
document.getElementById('login-btn').addEventListener('click', () => auth.login())
document.getElementById('logout-btn').addEventListener('click', () => auth.logout())
</script>
</body>
</html>API Reference
API参考
StarchildAuthOptions
(Constructor Options)
StarchildAuthOptionsStarchildAuthOptions
(构造函数选项)
StarchildAuthOptions| Option | Type | Default | Description |
|---|---|---|---|
| | (required) | OAuth Client ID from iamstarchild.com |
| | — | Called after successful login (popup or auto-login) |
| | — | Called after logout |
| | — | Called when token is auto-refreshed |
| | — | Called when token refresh fails (session expired) |
| | | Attempt to restore session from localStorage on init |
| | | Starchild origin URL (for popup login page) |
| | | Go API base URL (for token refresh & logout) |
| | | AI API base URL (for |
| | | Login popup width in pixels |
| | | Login popup height in pixels |
| | | Auto-refresh interval in ms (default: 12 minutes) |
| 选项 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| | 必填 | 来自iamstarchild.com的OAuth客户端ID |
| | — | 登录成功后调用(弹窗登录或自动登录时) |
| | — | 登出后调用 |
| | — | 令牌自动刷新后调用 |
| | — | 令牌刷新失败时调用(会话过期) |
| | | 初始化时尝试从localStorage恢复会话 |
| | | Starchild源地址(用于弹窗登录页面) |
| | | Go API基础地址(用于令牌刷新和登出) |
| | | AI API基础地址(用于 |
| | | 登录弹窗宽度(像素) |
| | | 登录弹窗高度(像素) |
| | | 自动刷新间隔(毫秒,默认:12分钟) |
Methods
方法
| Method | Returns | Description |
|---|---|---|
| | Opens OAuth popup and returns credentials on success |
| | Clears session, revokes refresh token via API |
| | Whether user is currently authenticated |
| | Current access token, or |
| | Current user info, or |
| | Manually trigger token refresh, returns new access token |
| | Remove listeners, clear timers, clear tokens. Instance cannot be reused. |
| 方法 | 返回值 | 描述 |
|---|---|---|
| | 打开OAuth弹窗,成功时返回凭证信息 |
| | 清除会话,通过API撤销刷新令牌 |
| | 用户当前是否已认证 |
| | 当前访问令牌,未登录时返回 |
| | 当前用户信息,未登录时返回 |
| | 手动触发令牌刷新,返回新的访问令牌 |
| | 移除监听器、清除定时器、清除令牌。实例无法重复使用。 |
LoginResult
Type
LoginResultLoginResult
类型
LoginResulttypescript
interface LoginResult {
accessToken: string
refreshToken: string
expiresIn: number
userInfo: UserInfo
}typescript
interface LoginResult {
accessToken: string
refreshToken: string
expiresIn: number
userInfo: UserInfo
}UserInfo
Type
UserInfoUserInfo
类型
UserInfotypescript
interface UserInfo {
/** Unique user ID */
userInfoId: string
/** Display name (agent name) */
agentName: string
/** Avatar URL */
agentAvatar: string
}typescript
interface UserInfo {
/** 唯一用户ID */
userInfoId: string
/** 显示名称(agent名称) */
agentName: string
/** 头像URL */
agentAvatar: string
}Token Lifecycle
令牌生命周期
- Access tokens are short-lived and auto-refresh every 12 minutes (configurable via )
refreshInterval - The SDK stores the refresh token in (key:
localStorage)starchild_rt_{clientId} - On page reload, if is
autoLogin(default), the SDK uses the stored refresh token to restore the session automatically —truefires again with fresh tokensonLogin - When the page returns from background (visibility change), the SDK automatically refreshes the token
- If refresh fails (e.g. user revoked access, token expired), fires and the stored refresh token is cleared
onTokenRefreshFailed
- 访问令牌有效期短,每12分钟自动刷新(可通过配置)
refreshInterval - SDK将刷新令牌存储在中(键名:
localStorage)starchild_rt_{clientId} - 页面刷新时,如果为
autoLogin(默认值),SDK会使用存储的刷新令牌自动恢复会话——true会再次触发并返回新令牌onLogin - 页面从后台恢复可见性时,SDK会自动刷新令牌
- 如果刷新失败(例如用户撤销权限、令牌过期),会触发,并清除存储的刷新令牌
onTokenRefreshFailed
Sending Authenticated API Requests
发送已认证的API请求
Once logged in, include the access token in your API requests:
typescript
const response = await fetch('https://api.your-app.com/data', {
headers: {
Authorization: `Bearer ${auth.getToken()}`,
},
})For React / Vue, use the from the hook/composable:
tokentypescript
// React example
const { token } = useStarchildAuth()
const fetchData = async () => {
const res = await fetch('/api/data', {
headers: { Authorization: `Bearer ${token}` },
})
return res.json()
}登录成功后,在API请求中包含访问令牌:
typescript
const response = await fetch('https://api.your-app.com/data', {
headers: {
Authorization: `Bearer ${auth.getToken()}`,
},
})对于React / Vue项目,使用钩子/组合式函数中的:
tokentypescript
// React示例
const { token } = useStarchildAuth()
const fetchData = async () => {
const res = await fetch('/api/data', {
headers: { Authorization: `Bearer ${token}` },
})
return res.json()
}Important Notes
重要注意事项
CDN Build Selection
CDN构建版本选择
| Build | File | Usage |
|---|---|---|
| UMD | | |
| ESM | | |
Common mistake: Usingwith a plainstarchild-auth.jstag. This will fail silently because the ESM file uses<script>syntax which is invalid in non-module scripts. Always use theexportbuild for plain.umd.cjstags.<script>
| 构建版本 | 文件 | 使用场景 |
|---|---|---|
| UMD | | |
| ESM | | |
常见错误:将用于普通starchild-auth.js标签。这会静默失败,因为ESM文件使用<script>语法,在非模块脚本中无效。对于普通export标签,请始终使用<script>构建版本。.umd.cjs
Session Persistence
会话持久化
The SDK stores the refresh token in under the key . This enables:
localStoragestarchild_rt_{clientId}- Auto-login on page reload — no need to call again
login() - Cross-tab session sharing — all tabs with the same share the session
clientId
To fully clear a user's session, call which removes the stored token and revokes it server-side.
auth.logout()SDK将刷新令牌存储在中,键名为。这实现了:
localStoragestarchild_rt_{clientId}- 页面刷新时自动登录——无需再次调用
login() - 跨标签页会话共享——所有使用相同的标签页共享会话
clientId
要完全清除用户会话,请调用,它会移除存储的令牌并在服务器端撤销该令牌。
auth.logout()Troubleshooting
故障排除
| Issue | Solution |
|---|---|
| Popup blocked | Ensure |
| Origin mismatch | Verify the Allowed Origin in OAuth Apps settings matches your site's origin exactly (e.g. |
| You're using the ESM build ( |
| Token refresh failing | Check that the app hasn't been revoked in user's Starchild settings. Also check browser console for network errors. |
| This is expected behavior — |
| CDN blocked | unpkg.com may be slow/blocked in some environments. Use |
| 问题 | 解决方案 |
|---|---|
| 弹窗被拦截 | 确保 |
| 源地址不匹配 | 验证OAuth应用设置中的允许的源地址与你的网站源地址完全一致(例如 |
| 你正在将ESM构建版本( |
| 令牌刷新失败 | 检查用户是否在Starchild设置中撤销了应用权限。同时查看浏览器控制台的网络错误信息。 |
| 这是预期行为—— |
| CDN被屏蔽 | unpkg.com在某些环境中可能加载缓慢或被屏蔽。使用npmmirror作为备选( |