Skip to content

Building 70 Projects ranging from beginner to advanced so anyone can — learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company 👇

782 B
Raw Blame
// ===================
// © AngelaMos | 2026
// session.ts
// ===================

import { STORAGE_KEYS } from '@/config'

function createId(): string {
  if (typeof crypto !== 'undefined' && 'randomUUID' in crypto) {
    return crypto.randomUUID()
  }
  return `s-${Math.random().toString(36).slice(2)}-${Date.now().toString(36)}`
}

let cached: string | null = null

export function getSessionId(): string {
  if (cached !== null) {
    return cached
  }
  try {
    const existing = localStorage.getItem(STORAGE_KEYS.SESSION)
    if (existing) {
      cached = existing
      return existing
    }
    const fresh = createId()
    localStorage.setItem(STORAGE_KEYS.SESSION, fresh)
    cached = fresh
    return fresh
  } catch {
    cached = createId()
    return cached
  }
}