diff --git a/backend/package.json b/backend/package.json index 89507d5..e101279 100644 --- a/backend/package.json +++ b/backend/package.json @@ -13,6 +13,7 @@ "dependencies": { "bcryptjs": "^2.4.3", "body-parser": "^1.20.3", + "cors": "^2.8.5", "dotenv": "^16.4.5", "express": "^4.21.1", "express-session": "^1.18.1", diff --git a/backend/server.js b/backend/server.js index 13654c2..3f19f00 100644 --- a/backend/server.js +++ b/backend/server.js @@ -4,21 +4,23 @@ const session = require('express-session'); const passport = require('passport'); const bodyParser = require('body-parser'); require('dotenv').config(); +const cors = require('cors'); // Passport configuration require('./config/passportConfig'); const app = express(); +// CORS configuration +app.use(cors('*')); + // Middleware app.use(bodyParser.json()); - app.use(session({ secret: process.env.SESSION_SECRET, resave: false, saveUninitialized: false, })); - app.use(passport.initialize()); app.use(passport.session()); @@ -29,7 +31,6 @@ app.use('/api/auth', authRoutes); // Connect to MongoDB mongoose.connect(process.env.MONGO_URI, {}).then(() => { console.log('Connected to MongoDB'); - app.listen(process.env.PORT, () => { console.log(`Server running on port ${process.env.PORT}`); }); diff --git a/src/Routes/Login/Login.tsx b/src/Routes/Login/Login.tsx new file mode 100644 index 0000000..201f190 --- /dev/null +++ b/src/Routes/Login/Login.tsx @@ -0,0 +1,79 @@ +import React, { useState, ChangeEvent, FormEvent } from "react"; +import axios from "axios"; +import { useNavigate } from "react-router-dom"; // Import the hook for navigation + +interface LoginFormData { + email: string; + password: string; +} + +const Login: React.FC = () => { + const [formData, setFormData] = useState({ email: "", password: "" }); + const [message, setMessage] = useState(""); + + const navigate = useNavigate(); // Initialize the navigate hook + + const handleChange = (e: ChangeEvent) => { + const { name, value } = e.target; + setFormData({ ...formData, [name]: value }); + }; + + const handleSubmit = async (e: FormEvent) => { + e.preventDefault(); + try { + const response = await axios.post( + "http://localhost:5000/api/auth/login", // Backend endpoint + formData, + + ); + setMessage(response.data.message); // Show success message from backend + + // Navigate to /home if login is successful + if (response.data.message === 'Login successful') { + navigate("/home"); + } + } catch (error: any) { + setMessage(error.response?.data?.message || "Something went wrong"); + } + }; + + return ( +
+

Login

+
+
+ +
+
+ +
+ +
+ {message &&

{message}

} +
+ ); +}; + +export default Login; + diff --git a/src/Routes/Router.tsx b/src/Routes/Router.tsx index 5307a85..867672a 100644 --- a/src/Routes/Router.tsx +++ b/src/Routes/Router.tsx @@ -4,11 +4,16 @@ import Home from "../pages/Home/Home"; // Import the Home component import About from "../pages/About/About"; // Import the About component import Contact from "../pages/Contact/Contact"; // Import the Contact component import Contributors from "../pages/Contributors/Contributors"; +import Signup from "./Signup/Signup.tsx"; +import Login from "./Login/Login.tsx"; + const Router = () => { return ( {/* Redirect from root (/) to the home page */} + } /> + } /> } /> } /> } /> diff --git a/src/Routes/Signup/Signup.tsx b/src/Routes/Signup/Signup.tsx new file mode 100644 index 0000000..32422ef --- /dev/null +++ b/src/Routes/Signup/Signup.tsx @@ -0,0 +1,89 @@ +import React, { useState, ChangeEvent, FormEvent } from "react"; +import axios from "axios"; +import { useNavigate } from "react-router-dom"; + +interface SignUpFormData { + username: string; + email: string; + password: string; +} + +const SignUp: React.FC = () => { + const [formData, setFormData] = useState({ username: "", email: "", password: "" }); + const [message, setMessage] = useState(""); + + const navigate = useNavigate(); + + const handleChange = (e: ChangeEvent) => { + const { name, value } = e.target; + setFormData({ ...formData, [name]: value }); + }; + + const handleSubmit = async (e: FormEvent) => { + e.preventDefault(); + try { + const response = await axios.post( + "http://localhost:5000/api/auth/signup", // Backend endpoint + formData // Include cookies for session + ); + setMessage(response.data.message); // Show success message from backend + + // Navigate to login page after successful signup + if (response.data.message === 'User created successfully') { + navigate("/login"); + } + } catch (error: any) { + setMessage(error.response?.data?.message || "Something went wrong"); + } + }; + + return ( +
+

Sign Up

+
+
+ +
+
+ +
+
+ +
+ +
+ {message &&

{message}

} +
+ ); +}; + +export default SignUp;