diff --git a/back-end/App.js b/back-end/App.js index b625576..ace63e8 100644 --- a/back-end/App.js +++ b/back-end/App.js @@ -2,6 +2,10 @@ const express = require("express"); const cors = require('cors'); // middleware for enabling CORS (Cross-Origin Resource Sharing) requests. const app = express(); + +// Import user data +const userData = require('./mockDatabase.json'); + app.use(cors()); // allow cross-origin resource sharing app.use(express.json()); // decode JSON-formatted incoming POST data @@ -307,6 +311,28 @@ app.post('/editprofile', (req, res) => { } }); +app.post('/login', (req, res) => { + const { username, password } = req.body; + console.log('Received login attempt:', username, password); // Debug + + let foundUser = false; + for (const key in userData) { + if (userData[key].login.username === username && userData[key].login.password === password) { + foundUser = true; + break; // Stop the loop once the user is found + } + } + + if (foundUser) { + console.log('Login successful for:', username); // Debug + res.json({ message: "Login successful" }); + } else { + console.log('Login failed for:', username); // Debug + res.status(401).json({ message: "Invalid username or password" }); + } +}); + + diff --git a/back-end/fakeDatabase.json b/back-end/fakeDatabase.json deleted file mode 100644 index c5ac7d3..0000000 --- a/back-end/fakeDatabase.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "BobbyImpasto": { - "username": "BobbyImpasto", - "password": "bobbyPass123" - }, - "BarackObama": { - "username": "BarackObama", - "password": "obamaSecure456" - }, - "TaylorSwift": { - "username": "TaylorSwift", - "password": "swiftMelody789" - }, - "MichaelBossi": { - "username": "MichaelBossi", - "password": "bossiRulez101" - }, - "BillClinton": { - "username": "BillClinton", - "password": "clintonSax102" - }, - "LadyGaga": { - "username": "LadyGaga", - "password": "gagaPoker103" - } - } \ No newline at end of file diff --git a/back-end/mockDatabase.json b/back-end/mockDatabase.json index 1280e0a..1fdaa1a 100644 --- a/back-end/mockDatabase.json +++ b/back-end/mockDatabase.json @@ -52,7 +52,7 @@ "TaylorSwift": { "login": { "username": "john123", - "password": "qwerasdf" + "password": "qwerasdff" }, "profile": { diff --git a/front-end/src/LoginForm.js b/front-end/src/LoginForm.js index 93950c4..d89c4b7 100644 --- a/front-end/src/LoginForm.js +++ b/front-end/src/LoginForm.js @@ -5,18 +5,42 @@ import './landingPages.css'; function LoginForm() { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); + const [loginError, setLoginError] = useState(''); const navigate = useNavigate(); - const handleSubmit = (e) => { + const handleSubmit = async (e) => { e.preventDefault(); console.log('Login with:', username, password); - navigate('/matches'); + + try { + const response = await fetch('http://localhost:3001/login', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ username, password }), + }); + + const data = await response.json(); + + if (response.ok) { + console.log('Login successful:', data); + navigate('/matches'); + } else { + console.error('Login failed:', data.message); + setLoginError(data.message); + } + } catch (error) { + console.error('Login error:', error); + setLoginError('Failed to connect to the server.'); + } }; return (

Welcome Back!

+ {loginError &&
{loginError}
}