dvc890 commited on
Commit
4949b0b
·
verified ·
1 Parent(s): 720777f

Upload 54 files

Browse files
Files changed (2) hide show
  1. server.js +7 -1
  2. services/api.ts +11 -3
server.js CHANGED
@@ -504,7 +504,13 @@ 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) => { const { username, password } = req.body; const user = await User.findOne({ username, password }); if (!user) return res.status(401).json({ message: 'Error' }); if (user.status !== 'active') return res.status(403).json({ error: 'PENDING_APPROVAL' }); res.json(user); });
 
 
 
 
 
 
508
  app.get('/api/schools', async (req, res) => { res.json(await School.find()); });
509
  app.post('/api/schools', async (req, res) => { res.json(await School.create(req.body)); });
510
  app.put('/api/schools/:id', async (req, res) => { await School.findByIdAndUpdate(req.params.id, req.body); res.json({}); });
 
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 });
510
+ if (!user) return res.status(401).json({ message: 'Error' });
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({}); });
services/api.ts CHANGED
@@ -1,3 +1,4 @@
 
1
  import { User } from '../types';
2
 
3
  const API_BASE = '/api';
@@ -60,8 +61,15 @@ export const api = {
60
  window.location.reload();
61
  },
62
  getCurrentUser: (): User | null => {
63
- const userStr = localStorage.getItem('user');
64
- return userStr ? JSON.parse(userStr) : null;
 
 
 
 
 
 
 
65
  },
66
  refreshSession: async () => {
67
  try {
@@ -224,4 +232,4 @@ export const api = {
224
  getKey: () => request('/ai/config/key'),
225
  },
226
  batchDelete: (resource: string, ids: string[]) => request(`/batch/${resource}`, { method: 'POST', body: JSON.stringify({ ids }) }),
227
- };
 
1
+
2
  import { User } from '../types';
3
 
4
  const API_BASE = '/api';
 
61
  window.location.reload();
62
  },
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.');
70
+ localStorage.removeItem('user');
71
+ return null;
72
+ }
73
  },
74
  refreshSession: async () => {
75
  try {
 
232
  getKey: () => request('/ai/config/key'),
233
  },
234
  batchDelete: (resource: string, ids: string[]) => request(`/batch/${resource}`, { method: 'POST', body: JSON.stringify({ ids }) }),
235
+ };