hookehuyr

fix(auth): 修复登录状态更新和认证头设置问题

在登录和用户状态更新时,确保正确设置认证头信息
修复注销时未清理user_info的问题
添加错误处理确保注销时清理本地状态
/*
* @Date: 2025-03-20 21:11:31
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-07-07 11:27:17
* @LastEditTime: 2025-09-03 12:55:25
* @FilePath: /mlaj/src/contexts/auth.js
* @Description: 认证上下文管理模块,提供用户认证状态管理、登录登出功能
*/
......@@ -67,6 +67,11 @@ export function provideAuth() {
if (code) {
currentUser.value = { ...data.user, ...data.checkin }
localStorage.setItem('currentUser', JSON.stringify(currentUser.value))
// 重新设置认证头
const userInfo = JSON.parse(localStorage.getItem('user_info') || '{}')
if (userInfo.user_id && userInfo.HTTP_USER_TOKEN) {
setAuthHeaders(userInfo.user_id, userInfo.HTTP_USER_TOKEN)
}
} else {
logout()
}
......@@ -77,12 +82,22 @@ export function provideAuth() {
if (data.openid_has) {
currentUser.value = { ...data.user, ...data.checkin }
localStorage.setItem('currentUser', JSON.stringify(currentUser.value))
// 重新设置认证头
const userInfo = JSON.parse(localStorage.getItem('user_info') || '{}')
if (userInfo.user_id && userInfo.HTTP_USER_TOKEN) {
setAuthHeaders(userInfo.user_id, userInfo.HTTP_USER_TOKEN)
}
}
// 判断用户是否已经登录
if (data?.user?.id) {
// 已登录,更新用户状态
currentUser.value = { ...data.user, ...data.checkin }
localStorage.setItem('currentUser', JSON.stringify(currentUser.value))
// 重新设置认证头
const userInfo = JSON.parse(localStorage.getItem('user_info') || '{}')
if (userInfo.user_id && userInfo.HTTP_USER_TOKEN) {
setAuthHeaders(userInfo.user_id, userInfo.HTTP_USER_TOKEN)
}
}
}
}
......@@ -122,12 +137,18 @@ export function provideAuth() {
currentUser.value = null
// 清理本地存储的用户信息和登录时间戳
localStorage.removeItem('currentUser')
localStorage.removeItem('user_info')
// localStorage.removeItem('loginTimestamp')
// 清除认证请求头
clearAuthHeaders()
}
} catch (error) {
console.error('注销失败:', error)
// 即使API调用失败,也要清理本地状态
currentUser.value = null
localStorage.removeItem('currentUser')
localStorage.removeItem('user_info')
clearAuthHeaders()
}
}
......
......@@ -292,8 +292,8 @@ const handleSubmit = async () => {
const { code, data } = await getUserInfoAPI();
if (code) {
// 登录成功,更新auth状态
const success = login(data.user);
// 登录成功,更新auth状态,传递完整的用户信息包括打卡信息
const success = login({ ...data.user, ...data.checkin });
if (success) {
// 如果有重定向参数,登录成功后跳转到对应页面
......
<!--
* @Date: 2025-05-29 15:34:17
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-08-26 15:16:55
* @LastEditTime: 2025-08-26 15:23:38
* @FilePath: /mlaj/src/views/checkin/IndexCheckInPage.vue
* @Description: 文件描述
-->
......@@ -856,7 +856,6 @@ const formatData = (data) => {
/* 禁用未来日期的样式 */
:deep(.calendar-disabled) {
color: #c8c9cc !important;
background-color: #f7f8fa !important;
cursor: not-allowed !important;
}
......