import CryptoJS from 'crypto-js' const SECRET_KEY = 'mock-sso-secret-key-2024' const TOKEN_KEY = 'sso_token' const USER_KEY = 'sso_user' export const encrypt = (data: string) => { try { return CryptoJS.AES.encrypt(data, SECRET_KEY).toString() } catch { return data } } export const decrypt = (data: string) => { try { const bytes = CryptoJS.AES.decrypt(data, SECRET_KEY) return bytes.toString(CryptoJS.enc.Utf8) } catch { return data } } export const setAuth = (token: string, user: object) => { if (typeof window === 'undefined') return localStorage.setItem(TOKEN_KEY, encrypt(token)) localStorage.setItem(USER_KEY, encrypt(JSON.stringify(user))) } export const getToken = (): string | null => { if (typeof window === 'undefined') return null const encrypted = localStorage.getItem(TOKEN_KEY) if (!encrypted) return null return decrypt(encrypted) } export const getUser = (): object | null => { if (typeof window === 'undefined') return null const encrypted = localStorage.getItem(USER_KEY) if (!encrypted) return null try { return JSON.parse(decrypt(encrypted)) } catch { return null } } export const clearAuth = () => { if (typeof window === 'undefined') return localStorage.removeItem(TOKEN_KEY) localStorage.removeItem(USER_KEY) } export const isAuthenticated = (): boolean => { return !!getToken() }