shared/lib/auth/user-sync.ts

25 lines
1.0 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// shared/lib/auth/user-sync.ts — 用户同步工具OIDC userinfo → 本地 SQLite
import type { OidcUserinfo } from './types'
export interface UserSyncStore {
getUser(username: string): { id: number; role: string } | null
createUser(username: string, displayName: string, email: string): { id: number; role: string }
updateUser(username: string, displayName: string, email: string): void
}
// 首次 OIDC 登录时自动创建本地用户记录,已存在则更新信息
export function syncOidcUser(store: UserSyncStore, userinfo: OidcUserinfo): { id: number; role: string; isNew: boolean } {
const username = userinfo.preferred_username
const displayName = userinfo.name || username
const email = userinfo.email || ''
const existing = store.getUser(username)
if (existing) {
store.updateUser(username, displayName, email)
return { id: existing.id, role: existing.role, isNew: false }
}
const created = store.createUser(username, displayName, email)
return { id: created.id, role: created.role || 'viewer', isNew: true }
}