39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
/**
|
|
* STOMP·실시간 시그널 소유자 검증 — 로그인 사용자는 user_id, 비로그인은 device_id 기준.
|
|
*/
|
|
export interface SignalOwnership {
|
|
userId?: number | null;
|
|
deviceId?: string | null;
|
|
}
|
|
|
|
function readDeviceId(): string {
|
|
return localStorage.getItem('gc_device_id') ?? '';
|
|
}
|
|
|
|
function readUserId(): number | null {
|
|
const raw = localStorage.getItem('gc_user_id');
|
|
if (!raw) return null;
|
|
const id = Number(raw);
|
|
return Number.isFinite(id) && id > 0 ? id : null;
|
|
}
|
|
|
|
export function getCurrentSignalSession(): { userId: number | null; deviceId: string } {
|
|
return { userId: readUserId(), deviceId: readDeviceId() };
|
|
}
|
|
|
|
/** STOMP 페이로드·DB 시그널이 현재 세션 소유인지 */
|
|
export function signalBelongsToCurrentSession(owner: SignalOwnership): boolean {
|
|
const { userId, deviceId } = getCurrentSignalSession();
|
|
|
|
if (owner.userId != null && owner.userId > 0) {
|
|
return userId != null && userId === owner.userId;
|
|
}
|
|
|
|
if (owner.deviceId != null && owner.deviceId !== '') {
|
|
return userId == null && owner.deviceId === deviceId;
|
|
}
|
|
|
|
// 소유자 메타 없음(레거시 브로드캐스트) — 타 사용자 시그널 유입 방지
|
|
return false;
|
|
}
|