Files
JayuCloud/JC-ArtList-WeChat/utils/auth.js
T

211 lines
7.7 KiB
JavaScript
Raw Normal View History

2026-04-01 15:18:35 +08:00
const { STORAGE_KEYS, getStorage, removeStorage, setStorage } = require('./storage')
2026-05-23 18:45:41 +08:00
/**
* 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
2026-05-23 18:45:41 +08:00
/**
* 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
}
2026-05-23 18:45:41 +08:00
/**
* 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
}
2026-04-01 15:18:35 +08:00
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
function getAccessToken() {
return getStorage(STORAGE_KEYS.ACCESS_TOKEN, '')
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
function getRefreshToken() {
return getStorage(STORAGE_KEYS.REFRESH_TOKEN, '')
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
function getStoredUserInfo() {
const userInfo = sanitizeStoredUserInfo(getStorage(STORAGE_KEYS.USER_INFO, null))
if (userInfo) {
setStorage(STORAGE_KEYS.USER_INFO, userInfo)
}
return userInfo
2026-04-01 15:18:35 +08:00
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
function getNeedSetPassword() {
return Boolean(getStorage(STORAGE_KEYS.NEED_SET_PASSWORD, false))
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-02 13:20:28 +08:00
function getPreferredMiniLoginShopId() {
return getStorage(STORAGE_KEYS.PREFERRED_MINI_LOGIN_SHOP_ID, '')
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
function isLoggedIn() {
return Boolean(getAccessToken())
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
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))
2026-04-01 15:18:35 +08:00
}
if (payload.needSetPassword) {
setStorage(STORAGE_KEYS.NEED_SET_PASSWORD, true)
} else {
removeStorage(STORAGE_KEYS.NEED_SET_PASSWORD)
}
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
function updateStoredUserInfo(userInfo) {
if (!userInfo) {
removeStorage(STORAGE_KEYS.USER_INFO)
return
}
setStorage(STORAGE_KEYS.USER_INFO, sanitizeStoredUserInfo(userInfo))
2026-04-01 15:18:35 +08:00
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
function setNeedSetPassword(value) {
if (value) {
setStorage(STORAGE_KEYS.NEED_SET_PASSWORD, true)
return
}
removeStorage(STORAGE_KEYS.NEED_SET_PASSWORD)
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
function clearSession() {
removeStorage(STORAGE_KEYS.ACCESS_TOKEN)
removeStorage(STORAGE_KEYS.REFRESH_TOKEN)
removeStorage(STORAGE_KEYS.USER_INFO)
removeStorage(STORAGE_KEYS.NEED_SET_PASSWORD)
}
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-02 13:20:28 +08:00
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))
}
2026-04-01 15:18:35 +08:00
module.exports = {
getAccessToken,
getRefreshToken,
getStoredUserInfo,
getNeedSetPassword,
2026-04-02 13:20:28 +08:00
getPreferredMiniLoginShopId,
2026-04-01 15:18:35 +08:00
isLoggedIn,
setSession,
updateStoredUserInfo,
setNeedSetPassword,
2026-04-02 13:20:28 +08:00
setPreferredMiniLoginShopId,
clearSession,
sanitizeStoredUserInfo
2026-04-01 15:18:35 +08:00
}