Skip to content

Commit

Permalink
update patch 1
Browse files Browse the repository at this point in the history
  • Loading branch information
nhattpn committed Apr 27, 2024
1 parent bbd3e6e commit b13e14e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
3 changes: 2 additions & 1 deletion login-frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Course from './pages/coursePage/Course';
import Course1 from './pages/coursePage/Course1';
import CourseRegistration from './pages/coursePage/CourseRegistration';
import TeacherDashBoard from './pages/dashboardPage/TeacherDashBoard';
import AdminDashBoard from './pages/dashboardPage/TeacherDashBoard';

function App() {
return (
Expand All @@ -25,7 +26,7 @@ function App() {

<Route path='/teacher/dashboard' element={< TeacherDashBoard/>} />
<Route path='/student/dashboard' element={< StudentDashBoard/>} />
<Route path='/admin/dashboard' element={< StudentDashBoard/>} />
<Route path='/admin/dashboard' element={< AdminDashBoard/>} />
<Route path='/course' element={<Course />} />
<Route path='/course1' element={<Course1 />} />
<Route path='/courseRegistration' element={<CourseRegistration />} />
Expand Down
4 changes: 2 additions & 2 deletions login-frontend/src/components/auth/changePassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ function ChangePassword(props) {
}
const response = await auth.login(email, password);
console.log("response data: ", response);
if (response.message === "Login successful") {
if (response.message === "Password changed successfully.") {
setErrorMsg(null);
setSuccessMsg("Login successful!");
setSuccessMsg("Password changed successfully.");
navigate('/' + path[1] + '/dashboard');
} else {
setErrorMsg(response.message);
Expand Down
3 changes: 1 addition & 2 deletions login-frontend/src/components/auth/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { useState } from 'react';
import { useAuth } from '../../hooks/useAuth'; // Assuming useAuth hook exists
import Alert from '@mui/material/Alert'
import { useNavigate } from 'react-router-dom';
import ChangePassword from './changePassword';
function Login(props) {
const path = window.location.pathname.split('/');
const [email, setEmail] = useState('');
Expand All @@ -20,7 +19,7 @@ function Login(props) {
if (response.message === "Login successful") {
setErrorMsg(null);
setSuccessMsg("Login successful!");
navigate('/' + path[0] + '/dashboard');
navigate('/' + path[1] + '/dashboard');
} else if(response.message === "Default password in use. Password change required."){
setErrorMsg(response.message);
setSuccessMsg(null);
Expand Down
13 changes: 6 additions & 7 deletions login-frontend/src/context/authContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ export function AuthProvider({ children }) {
const login = async (email, password) => {
try {
const data = await authService.login(email, password);
if (data) {
if (data && data.token) {
sessionStorage.setItem('jwtToken'), data.token;
setUser(data);
return data;
} else {
// Xử lý trường hợp không nhận được dữ liệu từ phản hồi
console.error('Không nhận được dữ liệu từ phản hồi khi đăng nhập');
return null; // Hoặc trả về giá trị mặc định khác
console.error('No data or token received on login');
return null;
}
} catch (error) {
// Xử lý lỗi từ phản hồi
console.error('Lỗi khi gọi hàm login:', error);
return null; // Hoặc trả về giá trị mặc định khác
console.error('Error during login:', error);
return null;
}
};

Expand Down
14 changes: 14 additions & 0 deletions login-frontend/src/hooks/useAuth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import { useContext } from 'react';
import { AuthContext } from '../context/authContext';
import { useEffect, useNavigate } from 'react';

export function useAuth() {
return useContext(AuthContext);
}
export function useRequireAuth(redirectUrl = '/login') {
const auth = useAuth();
const navigate = useNavigate();

useEffect(() => {
const token = sessionStorage.getItem('jwtToken');
if (!token) {
navigate(redirectUrl);
}
}, [navigate, redirectUrl]);

return auth;
}

0 comments on commit b13e14e

Please sign in to comment.