Commit 6ca90fb0 authored by Tharaka it19975696's avatar Tharaka it19975696 🎧

Fixed login page link issue

parent 186b4e79
......@@ -22,14 +22,10 @@ if (!JWT_SECRET) {
process.exit(1);
}
// Helper function to create a JWT token
const createToken = (userId, role) => {
return jwt.sign(
{ userId, role },
JWT_SECRET,
{ expiresIn: '1h' }
);
};
// Utility function to create JWT token
const createToken = (userId, userType) => {
return jwt.sign({ userId, userType }, JWT_SECRET, { expiresIn: '1h' });
};
// Registration endpoint for a student
app.post('/register/student', async (req, res) => {
......@@ -46,6 +42,7 @@ app.post('/register/student', async (req, res) => {
data: { username, password: hashedPassword }
});
//Token Generation
const token = createToken(student.id, 'student');
res.status(201).json({ token }); // Send the token in the response
} catch (error) {
......@@ -68,6 +65,7 @@ app.post('/register/student', async (req, res) => {
data: { username, password: hashedPassword }
});
//Token Generation
const token = createToken(teacher.id, 'teacher');
res.status(201).json({ token }); // Send the token in the response
} catch (error) {
......
......@@ -20,6 +20,12 @@ function App() {
setIsLoggedIn(!!token);
}, []);
// Function to call when the registration is successful
const onRegistrationSuccess = (token) => {
localStorage.setItem('token', token); // Save the token to localStorage
setIsLoggedIn(true); // Update the login state to true
};
const handleLogout = () => {
localStorage.removeItem('token');
setIsLoggedIn(false);
......@@ -29,9 +35,11 @@ function App() {
setIsLoggedIn(true);
};
const handleRegistrationSuccess = () => {
setIsLoggedIn(true);
};
// const handleRegistrationSuccess = () => {
// setIsLoggedIn(true);
// };
return (
<Router>
......@@ -46,7 +54,7 @@ function App() {
} />
<Route path="/register" element={
!isLoggedIn ? (
<RegisterPage onRegistrationSuccess={handleRegistrationSuccess} />
<RegisterPage onRegistrationSuccess={onRegistrationSuccess} />
) : (
<Navigate replace to="/home" />
)
......
......@@ -80,7 +80,7 @@ function LoginPage({ onLoginSuccess }) {
</button>
<p className="text-center">
Don't have an account yet?{' '}
<Link to="/" className="text-blue-600 hover:text-blue-800 transition duration-200 ease-in-out">
<Link to="/register" className="text-blue-600 hover:text-blue-800 transition duration-200 ease-in-out">
Register here
</Link>
</p>
......
......@@ -3,7 +3,7 @@ import React, { useState } from 'react';
import { Link, useNavigate } from 'react-router-dom';
import { registerStudent, registerTeacher } from '../api/api'; // Assuming these are API calls returning promises
const RegisterPage = () => {
const RegisterPage = (onRegistrationSuccess) => {
const [userData, setUserData] = useState({
username: '',
password: '',
......@@ -15,32 +15,33 @@ const RegisterPage = () => {
setUserData({ ...userData, [e.target.name]: e.target.value });
};
//Token generation in Registration success
const handleSubmit = async (e) => {
e.preventDefault();
try {
let token;
if (userData.role === 'student') {
const response = await registerStudent({ username: userData.username, password: userData.password });
token = response.token;
} else {
const response = await registerTeacher({ username: userData.username, password: userData.password });
token = response.token;
}
const response = userData.role === 'student' ?
await registerStudent(userData) :
await registerTeacher(userData);
if (token) {
console.log("Token received:", token); // Log for debugging
localStorage.setItem('token', token); // Store the token
if (response.token) {
console.log("Token received:", response.token); // Log for debugging
localStorage.setItem('token', response.token); // Store the token
if (onRegistrationSuccess) onRegistrationSuccess(response.token); // Update app state
navigate('/'); // Redirect to the homepage
} else {
console.error("No token received");
// This error message can be refined based on the response structure
alert('Registration successful but no token received');
}
} catch (error) {
console.error("Registration failed:", error);
alert(error.message);
// This error handling can be refined based on the response structure
alert('Registration failed: ' + (error.response?.data?.message || error.message));
}
};
return (
<div className="flex items-center justify-center h-screen bg-gray-200">
<div className="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4 flex flex-col">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment