const { STORAGE_KEYS, getStorage, removeStorage, setStorage } = require('./storage') /** * Purpose: SIGNED_URL_SAFE_BUFFER_MS supports auth.js by holding or executing the signed url safe buffer ms page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const SIGNED_URL_SAFE_BUFFER_MS = 60 * 1000 /** * Purpose: parseSignedUrlExpireAt supports auth.js by holding or executing the parse signed url expire at page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function parseSignedUrlExpireAt(url) { const rawUrl = String(url || '').trim() if (!rawUrl || rawUrl.indexOf('?') < 0) { return 0 } const queryString = rawUrl.slice(rawUrl.indexOf('?') + 1) const params = {} queryString.split('&').forEach(segment => { if (!segment) { return } const dividerIndex = segment.indexOf('=') const key = dividerIndex >= 0 ? segment.slice(0, dividerIndex) : segment const value = dividerIndex >= 0 ? segment.slice(dividerIndex + 1) : '' params[decodeURIComponent(key)] = decodeURIComponent(value) }) const awsDate = params['X-Amz-Date'] const awsExpireSeconds = Number(params['X-Amz-Expires'] || 0) if (awsDate && awsExpireSeconds > 0 && awsDate.length >= 16) { const year = Number(awsDate.slice(0, 4)) const month = Number(awsDate.slice(4, 6)) - 1 const day = Number(awsDate.slice(6, 8)) const hour = Number(awsDate.slice(9, 11)) const minute = Number(awsDate.slice(11, 13)) const second = Number(awsDate.slice(13, 15)) const baseTimestamp = Date.UTC(year, month, day, hour, minute, second) if (!Number.isNaN(baseTimestamp)) { return baseTimestamp + awsExpireSeconds * 1000 } } const expires = Number(params.Expires || 0) if (expires > 0) { return expires * 1000 } return 0 } /** * Purpose: sanitizeStoredUserInfo supports auth.js by holding or executing the sanitize stored user info page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function sanitizeStoredUserInfo(userInfo) { if (!userInfo || typeof userInfo !== 'object') { return userInfo } const nextUserInfo = { ...userInfo } const avatarExpireAt = parseSignedUrlExpireAt(nextUserInfo.avatar) if (avatarExpireAt > 0 && avatarExpireAt <= Date.now() + SIGNED_URL_SAFE_BUFFER_MS) { nextUserInfo.avatar = '' } return nextUserInfo } /** * Purpose: getAccessToken supports auth.js by holding or executing the get access token page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function getAccessToken() { return getStorage(STORAGE_KEYS.ACCESS_TOKEN, '') } /** * Purpose: getRefreshToken supports auth.js by holding or executing the get refresh token page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function getRefreshToken() { return getStorage(STORAGE_KEYS.REFRESH_TOKEN, '') } /** * Purpose: getStoredUserInfo supports auth.js by holding or executing the get stored user info page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function getStoredUserInfo() { const userInfo = sanitizeStoredUserInfo(getStorage(STORAGE_KEYS.USER_INFO, null)) if (userInfo) { setStorage(STORAGE_KEYS.USER_INFO, userInfo) } return userInfo } /** * Purpose: getNeedSetPassword supports auth.js by holding or executing the get need set password page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function getNeedSetPassword() { return Boolean(getStorage(STORAGE_KEYS.NEED_SET_PASSWORD, false)) } /** * Purpose: getPreferredMiniLoginShopId supports auth.js by holding or executing the get preferred mini login shop id page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function getPreferredMiniLoginShopId() { return getStorage(STORAGE_KEYS.PREFERRED_MINI_LOGIN_SHOP_ID, '') } /** * Purpose: isLoggedIn supports auth.js by holding or executing the is logged in page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function isLoggedIn() { return Boolean(getAccessToken()) } /** * Purpose: setSession supports auth.js by holding or executing the set session page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function setSession(payload) { if (!payload || !payload.accessToken || !payload.refreshToken) { return } setStorage(STORAGE_KEYS.ACCESS_TOKEN, payload.accessToken) setStorage(STORAGE_KEYS.REFRESH_TOKEN, payload.refreshToken) if (payload.userInfo) { setStorage(STORAGE_KEYS.USER_INFO, sanitizeStoredUserInfo(payload.userInfo)) } if (payload.needSetPassword) { setStorage(STORAGE_KEYS.NEED_SET_PASSWORD, true) } else { removeStorage(STORAGE_KEYS.NEED_SET_PASSWORD) } } /** * Purpose: updateStoredUserInfo supports auth.js by holding or executing the update stored user info page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function updateStoredUserInfo(userInfo) { if (!userInfo) { removeStorage(STORAGE_KEYS.USER_INFO) return } setStorage(STORAGE_KEYS.USER_INFO, sanitizeStoredUserInfo(userInfo)) } /** * Purpose: setNeedSetPassword supports auth.js by holding or executing the set need set password page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function setNeedSetPassword(value) { if (value) { setStorage(STORAGE_KEYS.NEED_SET_PASSWORD, true) return } removeStorage(STORAGE_KEYS.NEED_SET_PASSWORD) } /** * Purpose: clearSession supports auth.js by holding or executing the clear session page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function clearSession() { removeStorage(STORAGE_KEYS.ACCESS_TOKEN) removeStorage(STORAGE_KEYS.REFRESH_TOKEN) removeStorage(STORAGE_KEYS.USER_INFO) removeStorage(STORAGE_KEYS.NEED_SET_PASSWORD) } /** * Purpose: setPreferredMiniLoginShopId supports auth.js by holding or executing the set preferred mini login shop id page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function setPreferredMiniLoginShopId(shopId) { if (shopId === undefined || shopId === null || shopId === '') { removeStorage(STORAGE_KEYS.PREFERRED_MINI_LOGIN_SHOP_ID) return } setStorage(STORAGE_KEYS.PREFERRED_MINI_LOGIN_SHOP_ID, String(shopId)) } module.exports = { getAccessToken, getRefreshToken, getStoredUserInfo, getNeedSetPassword, getPreferredMiniLoginShopId, isLoggedIn, setSession, updateStoredUserInfo, setNeedSetPassword, setPreferredMiniLoginShopId, clearSession, sanitizeStoredUserInfo }