Files
WebDrop/web/src/core-bridge/worker/accountSession.ts
T

18 lines
1.0 KiB
TypeScript

import type { PersonalProfile } from './accountVault';
let identity: { username: string; token: string } | undefined;
let publicKey: JsonWebKey | undefined;
let personal: PersonalProfile | undefined;
export function setAccountIdentity(value: { username: string; token: string } | undefined, key?: JsonWebKey, profile?: PersonalProfile): void { identity = value; publicKey = key; personal = value ? profile : undefined; }
export function accountDisplay(): { username: string; displayName: string; avatar?: string } | undefined {
return identity ? { username: identity.username, displayName: personal?.displayName || identity.username, ...(personal?.avatar ? { avatar: personal.avatar } : {}) } : undefined;
}
export function accountPublicKey(): JsonWebKey {
if (!identity || !publicKey) throw new Error('请先解锁账号');
return publicKey;
}
export function accountToken(): string | undefined { return identity?.token; }
export function requireAccount(): string {
if (!identity) throw new Error('请先登录账号');
return identity.username;
}