Files
SharedClassManager/.cospec/plan/changes/fix-login-session-sync/task.md
2026-04-13 18:29:48 +08:00

37 lines
2.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
## 实施
- [ ] 1.1 创建 `frontend/api/save_session.php` Session 同步接口
【目标对象】`frontend/api/save_session.php`
【修改目的】提供接收前端登录信息并设置 PHP Session 的接口,解决登录后跳转回登录页的问题
【修改方式】新建 PHP 文件,实现 Session 设置逻辑
【相关依赖】`frontend/config.php`(用于引入必要的常量定义)
【修改内容】
- 文件顶部添加 `<?php` 声明
- 引入 config.php`require_once __DIR__ . '/../config.php';`
- 启用 Session`session_start();`
- 设置 CORS 头(如果需要跨域):`header('Content-Type: application/json');`
- 接收 POST 请求的 JSON 数据user_id, user_type, username, real_name, student_id
- 将用户数据存入 `$_SESSION` 变量
- 返回 JSON 响应:`{success: true}` 或错误信息
- 添加错误处理JSON 解析失败、缺少必要字段等情况的处理
- [ ] 1.2 修改 `frontend/index.php` 登录成功逻辑
【目标对象】`frontend/index.php:84-87`localStorage.setItem 和 window.location.href 所在区域)
【修改目的】登录成功后同步设置 PHP Session解决 dashboard 页面 Session 检查失败的问题
【修改方式】在 window.location.href 跳转前,增加 fetch 调用 save_session.php
【相关依赖】`frontend/api/save_session.php`
【修改内容】
-`localStorage.setItem(USER_STORAGE_KEY, JSON.stringify(data.data));` 之后、`window.location.href = data.data.redirect;` 之前
- 增加 `await fetch('/api/save_session.php', {...})` 调用
- 将登录 API 返回的 user_id、user_type、username、real_name、student_id 等数据作为请求体发送
- 使用 async/await 等待 Session 设置完成后再执行跳转
- 添加错误处理fetch 失败时记录错误但不影响跳转流程
- [ ] 1.3 验证修复效果
【验证方式】
- 清除浏览器 localStorage 和 Cookie
- 使用学生账号/家长账号/管理员账号执行登录操作
- 登录成功后应正常进入对应角色的 dashboard 页面,不再跳转回登录页
- 检查浏览器开发者工具 Network 面板,确认 `/api/save_session.php` 请求成功HTTP 200
- 检查浏览器 Cookies 中有 PHP Session ID如 PHPSESSID
- 在 dashboard 页面刷新,确认 Session 认证仍然有效