@myassis/gateway 1.0.72 → 1.0.73
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/routes/auth.js +21 -3
- package/dist/services/dataService.js +8 -2
- package/dist/stores/authStore.js +30 -11
- package/package.json +1 -1
package/dist/routes/auth.js
CHANGED
|
@@ -100,10 +100,28 @@ router.post('/refresh', async (req, res) => {
|
|
|
100
100
|
res.status(400).json({ success: false, error: 'Missing refresh token' });
|
|
101
101
|
return;
|
|
102
102
|
}
|
|
103
|
+
// 刷新前先找到持有该 refreshToken 的旧 accessToken,用于定位记录
|
|
104
|
+
const oldToken = Array.from(index_js_2.authStore.getAll()?.values() ?? [])
|
|
105
|
+
.find(x => x.refreshToken === refreshToken)?.accessToken;
|
|
103
106
|
const response = await index_js_1.authApi.refresh(refreshToken);
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
+
// server 可能返回 {accessToken} 或 {data:{accessToken}},两种都兼容
|
|
108
|
+
const accessToken = response.data?.accessToken ?? response.accessToken;
|
|
109
|
+
const newRefreshToken = response.data?.refreshToken ?? response.refreshToken;
|
|
110
|
+
const expiresIn = response.data?.expiresIn ?? response.expiresIn;
|
|
111
|
+
if (response.success && accessToken) {
|
|
112
|
+
index_js_2.authStore.updateToken(oldToken ?? accessToken, accessToken, newRefreshToken, expiresIn);
|
|
113
|
+
// 必须将新 token 回传给客户端,否则客户端无法更新本地 token
|
|
114
|
+
res.json({
|
|
115
|
+
success: true,
|
|
116
|
+
data: {
|
|
117
|
+
accessToken,
|
|
118
|
+
refreshToken: newRefreshToken,
|
|
119
|
+
expiresIn,
|
|
120
|
+
},
|
|
121
|
+
accessToken,
|
|
122
|
+
refreshToken: newRefreshToken,
|
|
123
|
+
expiresIn,
|
|
124
|
+
});
|
|
107
125
|
}
|
|
108
126
|
else {
|
|
109
127
|
res.status(401).json({ success: false, error: response.error || 'Invalid refresh token' });
|
|
@@ -75,9 +75,15 @@ exports.authService = {
|
|
|
75
75
|
getUser: (token) => index_js_2.authStore.get(token)?.user || null,
|
|
76
76
|
// 刷新 Token
|
|
77
77
|
refresh: async (refreshToken) => {
|
|
78
|
+
// 刷新前先定位旧 accessToken
|
|
79
|
+
const oldToken = Array.from(index_js_2.authStore.getAll()?.values() ?? [])
|
|
80
|
+
.find(x => x.refreshToken === refreshToken)?.accessToken;
|
|
78
81
|
const response = await index_js_1.authApi.refresh(refreshToken);
|
|
79
|
-
|
|
80
|
-
|
|
82
|
+
const accessToken = response.data?.accessToken ?? response.accessToken;
|
|
83
|
+
const newRefreshToken = response.data?.refreshToken ?? response.refreshToken;
|
|
84
|
+
const expiresIn = response.data?.expiresIn ?? response.expiresIn;
|
|
85
|
+
if (response.success && accessToken) {
|
|
86
|
+
index_js_2.authStore.updateToken(oldToken ?? accessToken, accessToken, newRefreshToken, expiresIn);
|
|
81
87
|
}
|
|
82
88
|
return response;
|
|
83
89
|
}
|
package/dist/stores/authStore.js
CHANGED
|
@@ -123,19 +123,38 @@ exports.authStore = {
|
|
|
123
123
|
},
|
|
124
124
|
/**
|
|
125
125
|
* 更新 Token
|
|
126
|
+
* @param oldToken 刷新前的旧 accessToken,用于定位记录
|
|
127
|
+
* @param newToken 刷新后的新 accessToken
|
|
128
|
+
* @param refreshToken 新的 refreshToken(可选)
|
|
129
|
+
* @param expiresIn 有效期秒数(可选)
|
|
130
|
+
* @returns 是否更新成功
|
|
126
131
|
*/
|
|
127
|
-
updateToken(
|
|
128
|
-
if (authMap) {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
saveAuthToFile(authMap);
|
|
136
|
-
logger.debug('Token updated');
|
|
137
|
-
}
|
|
132
|
+
updateToken(oldToken, newToken, refreshToken, expiresIn) {
|
|
133
|
+
if (!authMap) {
|
|
134
|
+
this.load();
|
|
135
|
+
}
|
|
136
|
+
if (!newToken) {
|
|
137
|
+
logger.warn('updateToken called without newToken');
|
|
138
|
+
return false;
|
|
138
139
|
}
|
|
140
|
+
// 按旧 token 定位记录;若传入的旧 token 已被替换过,则回退用新 token 匹配(幂等)
|
|
141
|
+
const auth = Array.from(authMap.values()).find(x => x.accessToken === oldToken)
|
|
142
|
+
|| Array.from(authMap.values()).find(x => x.accessToken === newToken);
|
|
143
|
+
if (!auth) {
|
|
144
|
+
logger.warn('updateToken: no auth record matched the given token');
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
auth.accessToken = newToken;
|
|
148
|
+
if (refreshToken) {
|
|
149
|
+
auth.refreshToken = refreshToken;
|
|
150
|
+
}
|
|
151
|
+
if (expiresIn) {
|
|
152
|
+
auth.expiresIn = expiresIn;
|
|
153
|
+
auth.expiresAt = Date.now() + expiresIn * 1000;
|
|
154
|
+
}
|
|
155
|
+
saveAuthToFile(authMap);
|
|
156
|
+
logger.debug('Token updated for user:', auth.user?.nickname || auth.user?.account);
|
|
157
|
+
return true;
|
|
139
158
|
},
|
|
140
159
|
/**
|
|
141
160
|
* 获取用户 ID
|