dvc890 commited on
Commit
65c1cc4
·
verified ·
1 Parent(s): 4949b0b

Upload 54 files

Browse files
Files changed (2) hide show
  1. server.js +27 -4
  2. services/api.ts +5 -1
server.js CHANGED
@@ -368,13 +368,33 @@ app.post('/api/achievements/exchange', async (req, res) => {
368
  if (rule.rewardType === 'DRAW_COUNT') { await Student.findByIdAndUpdate(studentId, { $inc: { drawAttempts: rule.rewardValue } }); }
369
  res.json({ success: true });
370
  });
 
 
371
  app.get('/api/auth/me', async (req, res) => {
 
372
  const username = req.headers['x-user-username'];
373
- if (!username) return res.status(401).json({ error: 'Unauthorized' });
374
- const user = await User.findOne({ username });
375
- if (!user) return res.status(404).json({ error: 'User not found' });
376
- res.json(user);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
377
  });
 
378
  app.post('/api/auth/update-profile', async (req, res) => {
379
  const { userId, trueName, phone, avatar, currentPassword, newPassword } = req.body;
380
  try {
@@ -504,6 +524,8 @@ app.post('/api/courses', async (req, res) => { const data = injectSchoolId(req,
504
  app.get('/api/public/schools', async (req, res) => { res.json(await School.find({}, 'name code _id')); });
505
  app.get('/api/public/config', async (req, res) => { const currentSem = getAutoSemester(); let config = await ConfigModel.findOne({ key: 'main' }); if (config) { let semesters = config.semesters || []; if (!semesters.includes(currentSem)) { semesters.unshift(currentSem); config.semesters = semesters; config.semester = currentSem; await ConfigModel.updateOne({ key: 'main' }, { semesters, semester: currentSem }); } } else { config = { key: 'main', allowRegister: true, semester: currentSem, semesters: [currentSem] }; } res.json(config); });
506
  app.get('/api/public/meta', async (req, res) => { res.json({ classes: await ClassModel.find({ schoolId: req.query.schoolId }), subjects: await SubjectModel.find({ schoolId: req.query.schoolId }) }); });
 
 
507
  app.post('/api/auth/login', async (req, res) => {
508
  const { username, password } = req.body;
509
  const user = await User.findOne({ username, password });
@@ -511,6 +533,7 @@ app.post('/api/auth/login', async (req, res) => {
511
  if (user.status !== 'active') return res.status(403).json({ error: 'PENDING_APPROVAL' });
512
  res.json({ token: 'mock-token-' + user._id, user });
513
  });
 
514
  app.get('/api/schools', async (req, res) => { res.json(await School.find()); });
515
  app.post('/api/schools', async (req, res) => { res.json(await School.create(req.body)); });
516
  app.put('/api/schools/:id', async (req, res) => { await School.findByIdAndUpdate(req.params.id, req.body); res.json({}); });
 
368
  if (rule.rewardType === 'DRAW_COUNT') { await Student.findByIdAndUpdate(studentId, { $inc: { drawAttempts: rule.rewardValue } }); }
369
  res.json({ success: true });
370
  });
371
+
372
+ // --- UPDATED AUTH/ME LOGIC ---
373
  app.get('/api/auth/me', async (req, res) => {
374
+ // 1. Try header (for internal AI calls if needed)
375
  const username = req.headers['x-user-username'];
376
+ if (username) {
377
+ const user = await User.findOne({ username });
378
+ if (user) return res.json(user);
379
+ }
380
+
381
+ // 2. Try Authorization Token (Standard way)
382
+ const authHeader = req.headers.authorization;
383
+ if (authHeader) {
384
+ const token = authHeader.split(' ')[1]; // Bearer <token>
385
+ if (token && token.startsWith('mock-token-')) {
386
+ const userId = token.replace('mock-token-', '');
387
+ // Validate ID format (Mongodb ObjectId is 24 hex chars)
388
+ if (/^[0-9a-fA-F]{24}$/.test(userId)) {
389
+ const user = await User.findById(userId);
390
+ if (user) return res.json(user);
391
+ }
392
+ }
393
+ }
394
+
395
+ return res.status(401).json({ error: 'Unauthorized' });
396
  });
397
+
398
  app.post('/api/auth/update-profile', async (req, res) => {
399
  const { userId, trueName, phone, avatar, currentPassword, newPassword } = req.body;
400
  try {
 
524
  app.get('/api/public/schools', async (req, res) => { res.json(await School.find({}, 'name code _id')); });
525
  app.get('/api/public/config', async (req, res) => { const currentSem = getAutoSemester(); let config = await ConfigModel.findOne({ key: 'main' }); if (config) { let semesters = config.semesters || []; if (!semesters.includes(currentSem)) { semesters.unshift(currentSem); config.semesters = semesters; config.semester = currentSem; await ConfigModel.updateOne({ key: 'main' }, { semesters, semester: currentSem }); } } else { config = { key: 'main', allowRegister: true, semester: currentSem, semesters: [currentSem] }; } res.json(config); });
526
  app.get('/api/public/meta', async (req, res) => { res.json({ classes: await ClassModel.find({ schoolId: req.query.schoolId }), subjects: await SubjectModel.find({ schoolId: req.query.schoolId }) }); });
527
+
528
+ // --- UPDATED LOGIN: RETURN { token, user } ---
529
  app.post('/api/auth/login', async (req, res) => {
530
  const { username, password } = req.body;
531
  const user = await User.findOne({ username, password });
 
533
  if (user.status !== 'active') return res.status(403).json({ error: 'PENDING_APPROVAL' });
534
  res.json({ token: 'mock-token-' + user._id, user });
535
  });
536
+
537
  app.get('/api/schools', async (req, res) => { res.json(await School.find()); });
538
  app.post('/api/schools', async (req, res) => { res.json(await School.create(req.body)); });
539
  app.put('/api/schools/:id', async (req, res) => { await School.findByIdAndUpdate(req.params.id, req.body); res.json({}); });
services/api.ts CHANGED
@@ -63,7 +63,11 @@ export const api = {
63
  getCurrentUser: (): User | null => {
64
  try {
65
  const userStr = localStorage.getItem('user');
66
- if (!userStr || userStr === 'undefined') return null;
 
 
 
 
67
  return JSON.parse(userStr);
68
  } catch (e) {
69
  console.warn('Failed to parse user session, clearing cache.');
 
63
  getCurrentUser: (): User | null => {
64
  try {
65
  const userStr = localStorage.getItem('user');
66
+ // Handle explicit "undefined" string or null
67
+ if (!userStr || userStr === 'undefined' || userStr === 'null') {
68
+ if(userStr) localStorage.removeItem('user');
69
+ return null;
70
+ }
71
  return JSON.parse(userStr);
72
  } catch (e) {
73
  console.warn('Failed to parse user session, clearing cache.');