feat(auth): 完善缓存机制

This commit is contained in:
liuyonghe0111 2025-12-19 15:34:52 +08:00
parent 8fe37d8814
commit f9777b8785
1 changed files with 5 additions and 4 deletions

View File

@ -17,6 +17,7 @@ function generateDeviceId(): string {
return `did_${timestamp}_${randomStr}_${browserInfo}`.toLowerCase(); return `did_${timestamp}_${randomStr}_${browserInfo}`.toLowerCase();
} }
const cookieSecure = false;
export const tokenManager = { export const tokenManager = {
// 获取token - 支持客户端和服务端 // 获取token - 支持客户端和服务端
getToken: (cookieString?: string): string | null => { getToken: (cookieString?: string): string | null => {
@ -84,7 +85,7 @@ export const tokenManager = {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
Cookies.set(TOKEN_COOKIE_NAME, token, { Cookies.set(TOKEN_COOKIE_NAME, token, {
expires: 365, expires: 365,
secure: false, secure: cookieSecure,
sameSite: 'lax', sameSite: 'lax',
}); });
} }
@ -96,7 +97,7 @@ export const tokenManager = {
// 设置cookie365天过期设备ID应该长期保存 // 设置cookie365天过期设备ID应该长期保存
Cookies.set(DEVICE_ID_COOKIE_NAME, deviceId, { Cookies.set(DEVICE_ID_COOKIE_NAME, deviceId, {
expires: 365, expires: 365,
secure: false, secure: cookieSecure,
sameSite: 'lax', sameSite: 'lax',
}); });
} }
@ -108,7 +109,7 @@ export const tokenManager = {
console.log('remove token'); console.log('remove token');
// 删除cookie时需要指定与设置时相同的选项 // 删除cookie时需要指定与设置时相同的选项
Cookies.remove(TOKEN_COOKIE_NAME, { Cookies.remove(TOKEN_COOKIE_NAME, {
secure: process.env.NODE_ENV === 'production', secure: cookieSecure,
sameSite: 'lax', sameSite: 'lax',
path: '/', path: '/',
}); });
@ -122,7 +123,7 @@ export const tokenManager = {
clearAll: (): void => { clearAll: (): void => {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
const cookieOptions = { const cookieOptions = {
secure: process.env.NODE_ENV === 'production', secure: cookieSecure,
sameSite: 'lax' as const, sameSite: 'lax' as const,
path: '/', path: '/',
}; };