Episode 6: Login Functionality with LocalStorage
Build Login functionality with simple authentication (based on email/password in db.json) and use LocalStorage to save login state
Episode 6: Login Functionality with LocalStorage
In this episode, you’ll build a basic login system:
- Match email/password from JSON-Server
- Save login info in
localStorage - Show conditional content in Navbar
1. Create Login Form
Update src/pages/Login.jsx:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { useState } from "react";
import axios from "axios";
import { useNavigate } from "react-router-dom";
const Login = () => {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const navigate = useNavigate();
const handleLogin = async (e) => {
e.preventDefault();
try {
const res = await axios.get(`http://localhost:3001/users?email=${email}&password=${password}`);
if (res.data.length > 0) {
const user = res.data[0];
localStorage.setItem("user", JSON.stringify(user));
alert("Login successful!");
navigate("/");
} else {
alert("Invalid credentials");
}
} catch (err) {
console.error(err);
alert("Login failed");
}
};
return (
<div className="max-w-md mx-auto p-6 mt-10 bg-white shadow rounded">
<h2 className="text-2xl font-bold mb-4">Login</h2>
<form onSubmit={handleLogin} className="space-y-4">
<input
type="email"
placeholder="Email"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
className="w-full border px-4 py-2 rounded"
/>
<input
type="password"
placeholder="Password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
className="w-full border px-4 py-2 rounded"
/>
<button
type="submit"
className="w-full bg-blue-600 text-white py-2 rounded hover:bg-blue-700"
>
Login
</button>
</form>
</div>
);
};
export default Login;
2. Update Navbar with User Info
Update Navbar.jsx to show the user email and Logout button if logged in:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { Link, useNavigate } from "react-router-dom";
const Navbar = () => {
const user = JSON.parse(localStorage.getItem("user"));
const navigate = useNavigate();
const handleLogout = () => {
localStorage.removeItem("user");
navigate("/");
};
return (
<nav className="bg-gray-800 text-white px-6 py-4 flex justify-between items-center">
<Link to="/" className="text-xl font-bold">E-Shop</Link>
<div className="space-x-4 flex items-center">
<Link to="/" className="hover:underline">Home</Link>
<Link to="/cart" className="hover:underline">Cart</Link>
{user ? (
<>
<span className="text-sm text-gray-300">Hello, {user.email}</span>
<button onClick={handleLogout} className="hover:underline">Logout</button>
</>
) : (
<Link to="/login" className="hover:underline">Login</Link>
)}
</div>
</nav>
);
};
export default Navbar;
- Sample User in db.json Ensure your
db.jsonhas at least one valid user:
1
2
3
4
5
6
7
"users": [
{
"id": 1,
"email": "user@example.com",
"password": "123456"
}
]
This post is licensed under CC BY 4.0 by the author.