Преглед на файлове

路由保护:在mobile_app.tsx中用ProtectedRoute包裹ClassroomPage路由
自动登录:在pages_classroom_new.tsx中实现从useAuth获取用户ID自动登录IM
移除手动输入用户ID的相关代码

yourname преди 7 месеца
родител
ревизия
35fbd2e306
променени са 2 файла, в които са добавени 14 реда и са изтрити 32 реда
  1. 1 1
      client/mobile/mobile_app.tsx
  2. 13 31
      client/mobile/pages_classroom_new.tsx

+ 1 - 1
client/mobile/mobile_app.tsx

@@ -255,7 +255,7 @@ const App = () => {
     },
     {
       path: '/mobile/classroom/:id?/:role?',
-      element: <ClassroomPage />,
+      element: <ProtectedRoute><ClassroomPage /></ProtectedRoute>,
       errorElement: <ErrorPage />
     },
     {

+ 13 - 31
client/mobile/pages_classroom_new.tsx

@@ -1,4 +1,5 @@
-import React, { useState } from 'react';
+import React, { useState, useEffect } from 'react';
+import { useAuth } from './hooks.tsx';
 import { useNavigate } from 'react-router';
 import { Role, ClassStatus } from './components/Classroom/useClassroom.ts';
 import { TeacherView } from './components/Classroom/TeacherView.tsx';
@@ -99,38 +100,17 @@ const CreateClassSection = () => {
   );
 };
 
-const UserIdInput = () => {
-  const { login: onLogin, userId, setUserId } = useClassroomContext();
-  const handleLogin = async () => {
-    if (!userId.trim()) return;
-    await onLogin(userId);
-  };
-
-  return (
-    <div className="flex flex-col items-center justify-center h-full">
-      <h2 className="text-2xl font-bold mb-8">请输入您的用户ID</h2>
-      <div className="flex space-x-2 w-64">
-        <input
-          type="text"
-          value={userId}
-          onChange={(e) => setUserId(e.target.value)}
-          placeholder="用户ID"
-          className="flex-1 px-3 py-2 border rounded"
-        />
-        <button
-          onClick={handleLogin}
-          className="px-4 py-2 bg-blue-500 text-white rounded"
-        >
-          登录
-        </button>
-      </div>
-    </div>
-  );
-};
 
 const Classroom = () => {
   const context = useClassroomContext();
-  const { role, classStatus, isLoggedIn } = context;
+  const { role, classStatus, isLoggedIn, login } = context;
+  const { user, isAuthenticated } = useAuth();
+
+  useEffect(() => {
+    if (isAuthenticated && user && !isLoggedIn) {
+      login(user.id.toString());
+    }
+  }, [isAuthenticated, user, isLoggedIn]);
 
   if (!role) {
     return (
@@ -143,7 +123,9 @@ const Classroom = () => {
   if (!isLoggedIn) {
     return (
       <AuthLayout>
-        <UserIdInput />
+        <div className="flex items-center justify-center h-full">
+          <p>正在自动登录中...</p>
+        </div>
       </AuthLayout>
     );
   }