From 9ba9e25fe383364ff161f90ff3facc83657b4a03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E5=B3=B0=E2=80=9C?= <3512137118@qq.com> Date: Mon, 11 Mar 2024 10:56:11 +0800 Subject: [PATCH] llf --- node/routes/llf_api.js | 8 ++++---- vite-project/src/axios/index.ts | 2 +- vite-project/src/views/LoginView.vue | 3 ++- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/node/routes/llf_api.js b/node/routes/llf_api.js index 065d26d..7a81221 100644 --- a/node/routes/llf_api.js +++ b/node/routes/llf_api.js @@ -18,12 +18,12 @@ const REFRESH_TOKEN_EXPIRES_IN_MS = 24 * 60 * 60 * 1000; // 示例值,设置 // 定义生成 Access Token 的函数 function generateAccessToken(userId) { - return jwt.sign({ userId }, SECRET_KEY, { expiresIn: '2s' }); + return jwt.sign({ userId }, SECRET_KEY, { expiresIn: '30m' }); } // 定义生成 Refresh Token 的函数 function generateRefreshToken(userId) { - return jwt.sign({ userId, purpose: 'refresh' }, SECRET_KEY, { expiresIn: '10s' }); + return jwt.sign({ userId, purpose: 'refresh' }, SECRET_KEY, { expiresIn: '1d' }); } @@ -39,7 +39,7 @@ router.post('/login', async (req, res) => { const newRefreshToken = new RefreshToken({ userId: user._id, token: refreshToken, expiresAt: new Date(Date.now() + REFRESH_TOKEN_EXPIRES_IN_MS) }); await newRefreshToken.save(); - res.json({ returncode: 200, accessToken, refreshToken }); + res.json({ returncode: 200, accessToken, refreshToken,user }); } else { res.status(401).json({ message: 'Invalid credentials' }); } @@ -63,7 +63,7 @@ router.post('/refresh', async (req, res) => { // 如果 Refresh Token 仍然有效,则: if (storedRefreshToken && storedRefreshToken.expiresAt > new Date()) { // 仅生成新的 Access Token - const accessToken = jwt.sign({ userId: decoded.userId }, SECRET_KEY, { expiresIn: '2s' }); + const accessToken = jwt.sign({ userId: decoded.userId }, SECRET_KEY, { expiresIn: '30m' }); // 返回新的 Access Token res.json({ success: '成功', accessToken }); diff --git a/vite-project/src/axios/index.ts b/vite-project/src/axios/index.ts index 52c67f3..69cef68 100644 --- a/vite-project/src/axios/index.ts +++ b/vite-project/src/axios/index.ts @@ -51,7 +51,7 @@ axiosInstance.interceptors.response.use( authStore.accessToken = refreshResponse.data.accessToken; localStorage.setItem(authStore.storageKeys.accessToken, authStore.accessToken); - // 使用新的token重新发起队列中的请求 + // 使用新的token重新发起队列中的请求 requestsQueue.forEach(request => { request.headers.Authorization = `Bearer ${authStore.accessToken}`; axiosInstance(request); diff --git a/vite-project/src/views/LoginView.vue b/vite-project/src/views/LoginView.vue index 6e7b02c..1768bf6 100644 --- a/vite-project/src/views/LoginView.vue +++ b/vite-project/src/views/LoginView.vue @@ -32,9 +32,10 @@ const loginInfo = ref({ async function handleSubmit() { try { const response = await axiosInstance.post('/llfApi/login', loginInfo.value); - console.log(response.data); if (response.data.returncode === 200) { useAuthStore().handleSuccessfulLogin(response.data.accessToken, response.data.refreshToken); + // 将JSON对象转化为字符串并存储 + sessionStorage.setItem('user', JSON.stringify(response.data.user)); router.push('/home'); } else { console.error("Login failed:", response.data.info || "登录失败"); -- Gitee