Commit 22c8834a authored by Tharaka it19975696's avatar Tharaka it19975696 🎧

Add login page feature

parent 02c1f325
......@@ -18,6 +18,9 @@
"react-scripts": "5.0.1",
"tailwindcss": "^3.4.1",
"web-vitals": "^2.1.4"
},
"devDependencies": {
"@babel/plugin-proposal-private-property-in-object": "^7.21.11"
}
},
"node_modules/@aashutoshrathi/word-wrap": {
......@@ -646,9 +649,17 @@
}
},
"node_modules/@babel/plugin-proposal-private-property-in-object": {
"version": "7.21.0-placeholder-for-preset-env.2",
"resolved": "https://registry.npmjs.org/@babel/plugin-proposal-private-property-in-object/-/plugin-proposal-private-property-in-object-7.21.0-placeholder-for-preset-env.2.tgz",
"integrity": "sha512-SOSkfJDddaM7mak6cPEpswyTRnuRltl429hMraQEglW+OkovnCzsiszTmsrlY//qLFjCpQDFRvjdm2wA5pPm9w==",
"version": "7.21.11",
"resolved": "https://registry.npmjs.org/@babel/plugin-proposal-private-property-in-object/-/plugin-proposal-private-property-in-object-7.21.11.tgz",
"integrity": "sha512-0QZ8qP/3RLDVBwBFoWAwCtgcDZJVwA5LUJRZU8x2YFfKNuFq161wK3cuGrALu5yiPu+vzwTAg/sMWVNeWeNyaw==",
"deprecated": "This proposal has been merged to the ECMAScript standard and thus this plugin is no longer maintained. Please use @babel/plugin-transform-private-property-in-object instead.",
"dev": true,
"dependencies": {
"@babel/helper-annotate-as-pure": "^7.18.6",
"@babel/helper-create-class-features-plugin": "^7.21.0",
"@babel/helper-plugin-utils": "^7.20.2",
"@babel/plugin-syntax-private-property-in-object": "^7.14.5"
},
"engines": {
"node": ">=6.9.0"
},
......@@ -1891,6 +1902,17 @@
"@babel/core": "^7.0.0-0"
}
},
"node_modules/@babel/preset-env/node_modules/@babel/plugin-proposal-private-property-in-object": {
"version": "7.21.0-placeholder-for-preset-env.2",
"resolved": "https://registry.npmjs.org/@babel/plugin-proposal-private-property-in-object/-/plugin-proposal-private-property-in-object-7.21.0-placeholder-for-preset-env.2.tgz",
"integrity": "sha512-SOSkfJDddaM7mak6cPEpswyTRnuRltl429hMraQEglW+OkovnCzsiszTmsrlY//qLFjCpQDFRvjdm2wA5pPm9w==",
"engines": {
"node": ">=6.9.0"
},
"peerDependencies": {
"@babel/core": "^7.0.0-0"
}
},
"node_modules/@babel/preset-env/node_modules/semver": {
"version": "6.3.1",
"resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz",
......
......@@ -37,5 +37,8 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@babel/plugin-proposal-private-property-in-object": "^7.21.11"
}
}
// App.js
import React from 'react';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
// src/App.js
import React, { useState } from 'react';
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
import Home from './Home';
import About from './About';
import Insights from './Insights';
import Navbar from './Navbar'; // Make sure to import the Navbar
import Navbar from './Navbar';
import LoginPage from './LoginPage';
function App() {
const [isLoggedIn, setIsLoggedIn] = useState(false);
const handleLoginSuccess = () => {
setIsLoggedIn(true);
};
return (
<Router>
<Navbar />
<Navbar />
<Routes>
<Route path="/about" element={<About />} />
<Route path="/insights" element={<Insights />} />
<Route path="/" element={<Home />} />
{isLoggedIn ? (
// Render these routes only if the user is logged in
<>
<Route path="/about" element={<About />} />
<Route path="/insights" element={<Insights />} />
<Route path="/" element={<Home />} />
</>
) : (
// If not logged in, redirect to login page
<>
<Route path="/login" element={<LoginPage onLoginSuccess={handleLoginSuccess} />} />
<Route path="*" element={<Navigate replace to="/login" />} />
</>
)}
</Routes>
</Router>
);
......
// LoginPage.js
import React, { useState } from 'react';
import { useNavigate } from 'react-router-dom';
function LoginPage({ onLoginSuccess }) {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const navigate = useNavigate();
const handleLogin = (event) => {
event.preventDefault();
// Check for teacher or student login
if ((username === 'teacher' && password === '1234') ||
(username === 'student' && password === '1234')) {
// Call the handler with the username
onLoginSuccess(username);
// Navigate to the homepage or dashboard
navigate('/');
} else {
alert('Invalid credentials!');
}
};
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">
<h1 className="mb-6 text-2xl font-bold text-center">O Level Maths Tutor</h1>
<form onSubmit={handleLogin} className="flex flex-col">
<label htmlFor="username" className="mb-2 text-gray-700">Username:</label>
<input
id="username"
type="text"
value={username}
onChange={(e) => setUsername(e.target.value)}
required
className="mb-4 p-2 border border-gray-300 rounded shadow"
/>
<label htmlFor="password" className="mb-2 text-gray-700">Password:</label>
<input
id="password"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
className="mb-4 p-2 border border-gray-300 rounded shadow"
/>
<button type="submit" className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded transition duration-200 ease-in-out">
Login
</button>
</form>
</div>
</div>
);
}
export default LoginPage;
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