feat: 简化登录模型,移除 nickname/avatar/expiresAt
这个提交包含在:
父节点
9e11a63144
当前提交
92002ab19f
@ -286,7 +286,7 @@ export const ImSDK = {
|
|||||||
* Login to IM service. Fetches a token internally and opens the WebSocket connection.
|
* Login to IM service. Fetches a token internally and opens the WebSocket connection.
|
||||||
* Pass dbName to enable local SQLite message caching (requires @nozbe/watermelondb).
|
* Pass dbName to enable local SQLite message caching (requires @nozbe/watermelondb).
|
||||||
*/
|
*/
|
||||||
async login(userId: string, nickname?: string, avatar?: string, dbName?: string): Promise<void> {
|
async login(userId: string, dbName?: string): Promise<void> {
|
||||||
const config = getConfig()
|
const config = getConfig()
|
||||||
const device = await getDeviceInfo()
|
const device = await getDeviceInfo()
|
||||||
client?.disconnect()
|
client?.disconnect()
|
||||||
@ -301,8 +301,6 @@ export const ImSDK = {
|
|||||||
brand: device.brand,
|
brand: device.brand,
|
||||||
model: device.model,
|
model: device.model,
|
||||||
osVersion: device.osVersion,
|
osVersion: device.osVersion,
|
||||||
...(nickname ? { nickname } : {}),
|
|
||||||
...(avatar ? { avatar } : {}),
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
await _saveToken(res.token)
|
await _saveToken(res.token)
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
export { XuqmSDK } from './sdk'
|
export { XuqmSDK } from './sdk'
|
||||||
export type { UnifiedLoginOptions, UnifiedLoginRefreshResult } from './sdk'
|
export type { UnifiedLoginOptions } from './sdk'
|
||||||
export type { XuqmInitOptions, DeviceInfo } from '@xuqm/rn-common'
|
export type { XuqmInitOptions, DeviceInfo } from '@xuqm/rn-common'
|
||||||
export { getDeviceId, getDeviceInfo, detectPushVendor, setUserId, getUserId } from '@xuqm/rn-common'
|
export { getDeviceId, getDeviceInfo, detectPushVendor, setUserId, getUserId } from '@xuqm/rn-common'
|
||||||
export { ScaledImage } from '@xuqm/rn-common'
|
export { ScaledImage } from '@xuqm/rn-common'
|
||||||
|
|||||||
66
src/sdk.ts
66
src/sdk.ts
@ -1,39 +1,15 @@
|
|||||||
import { _clearToken, setUserId as setCommonUserId, getUserId as getCommonUserId, XuqmSDK as CommonXuqmSDK } from '@xuqm/rn-common'
|
import { _clearToken, setUserId as setCommonUserId, getUserId as getCommonUserId, XuqmSDK as CommonXuqmSDK } from '@xuqm/rn-common'
|
||||||
import { ImSDK } from '@xuqm/rn-im'
|
import { ImSDK } from '@xuqm/rn-im'
|
||||||
import { PushSDK } from '@xuqm/rn-push'
|
import { PushSDK } from '@xuqm/rn-push'
|
||||||
import type { UserProfile } from '@xuqm/rn-im'
|
|
||||||
|
|
||||||
export interface UnifiedLoginRefreshResult {
|
|
||||||
userSig: string
|
|
||||||
expiresAt?: number
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface UnifiedLoginOptions {
|
export interface UnifiedLoginOptions {
|
||||||
userId: string
|
userId: string
|
||||||
userSig: string
|
userSig: string
|
||||||
profile?: Pick<UserProfile, 'nickname' | 'avatar' | 'gender'>
|
|
||||||
expiresAt?: number
|
|
||||||
refreshUserSig?: () => Promise<UnifiedLoginRefreshResult>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type SessionState = UnifiedLoginOptions
|
let currentSession: UnifiedLoginOptions | null = null
|
||||||
|
|
||||||
const REFRESH_GRACE_MS = 5 * 60 * 1000
|
async function applyLoginSession(session: UnifiedLoginOptions): Promise<void> {
|
||||||
const REFRESH_RETRY_MS = 30 * 1000
|
|
||||||
|
|
||||||
let currentSession: SessionState | null = null
|
|
||||||
let refreshTimer: ReturnType<typeof setTimeout> | null = null
|
|
||||||
let refreshInFlight = false
|
|
||||||
|
|
||||||
function clearRefreshTimer(): void {
|
|
||||||
if (refreshTimer) {
|
|
||||||
clearTimeout(refreshTimer)
|
|
||||||
refreshTimer = null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function applyLoginSession(session: SessionState): Promise<void> {
|
|
||||||
clearRefreshTimer()
|
|
||||||
setCommonUserId(session.userId)
|
setCommonUserId(session.userId)
|
||||||
try {
|
try {
|
||||||
await ImSDK.loginWithUserSig(session.userId, session.userSig)
|
await ImSDK.loginWithUserSig(session.userId, session.userSig)
|
||||||
@ -48,42 +24,6 @@ async function applyLoginSession(session: SessionState): Promise<void> {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
void error
|
void error
|
||||||
}
|
}
|
||||||
scheduleRefresh()
|
|
||||||
}
|
|
||||||
|
|
||||||
async function refreshSession(): Promise<void> {
|
|
||||||
const session = currentSession
|
|
||||||
if (refreshInFlight || !session?.refreshUserSig) return
|
|
||||||
refreshInFlight = true
|
|
||||||
try {
|
|
||||||
const refreshed = await session.refreshUserSig()
|
|
||||||
if (currentSession !== session) return
|
|
||||||
await applyLoginSession({
|
|
||||||
...session,
|
|
||||||
userSig: refreshed.userSig,
|
|
||||||
expiresAt: refreshed.expiresAt ?? session.expiresAt,
|
|
||||||
})
|
|
||||||
} catch (error) {
|
|
||||||
clearRefreshTimer()
|
|
||||||
refreshTimer = setTimeout(() => {
|
|
||||||
void refreshSession()
|
|
||||||
}, REFRESH_RETRY_MS)
|
|
||||||
if (CommonXuqmSDK.getUserId() && process.env.NODE_ENV !== 'production') {
|
|
||||||
console.warn('[XuqmSDK] userSig refresh failed, will retry', error)
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
refreshInFlight = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function scheduleRefresh(): void {
|
|
||||||
clearRefreshTimer()
|
|
||||||
const session = currentSession
|
|
||||||
if (!session?.refreshUserSig || !session.expiresAt) return
|
|
||||||
const delayMs = Math.max(session.expiresAt - Date.now() - REFRESH_GRACE_MS, 0)
|
|
||||||
refreshTimer = setTimeout(() => {
|
|
||||||
void refreshSession()
|
|
||||||
}, delayMs)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function login(options: UnifiedLoginOptions): Promise<void> {
|
async function login(options: UnifiedLoginOptions): Promise<void> {
|
||||||
@ -94,10 +34,8 @@ async function login(options: UnifiedLoginOptions): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function logout(): Promise<void> {
|
async function logout(): Promise<void> {
|
||||||
clearRefreshTimer()
|
|
||||||
const userId = currentSession?.userId ?? getCommonUserId()
|
const userId = currentSession?.userId ?? getCommonUserId()
|
||||||
currentSession = null
|
currentSession = null
|
||||||
refreshInFlight = false
|
|
||||||
setCommonUserId(null)
|
setCommonUserId(null)
|
||||||
if (userId) {
|
if (userId) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
正在加载...
在新工单中引用
屏蔽一个用户