aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/src/pages/Login.tsx
blob: 5703047622bd43b073f17a6c25acd8f26ac161ba (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { useState } from "react";
import { useLocation } from "wouter";
import { useAuth } from "../contexts/AuthContext";

export function Login() {
	const [username, setUsername] = useState("");
	const [password, setPassword] = useState("");
	const [error, setError] = useState("");
	const [isLoading, setIsLoading] = useState(false);
	const { login } = useAuth();
	const [, setLocation] = useLocation();

	const handleSubmit = async (e: React.FormEvent) => {
		e.preventDefault();
		setError("");
		setIsLoading(true);

		try {
			const success = await login(username, password);
			if (success) {
				setLocation("/");
			} else {
				setError("Invalid username or password");
			}
		} catch (_err) {
			setError("An error occurred during login");
		} finally {
			setIsLoading(false);
		}
	};

	return (
		<div
			style={{
				display: "flex",
				justifyContent: "center",
				alignItems: "center",
				minHeight: "100vh",
				backgroundColor: "#f5f5f5",
			}}
		>
			<div
				style={{
					backgroundColor: "white",
					padding: "2rem",
					borderRadius: "8px",
					boxShadow: "0 2px 4px rgba(0,0,0,0.1)",
					width: "100%",
					maxWidth: "400px",
				}}
			>
				<h1 style={{ marginBottom: "1.5rem", textAlign: "center" }}>
					Feedaka Login
				</h1>
				<form onSubmit={handleSubmit}>
					<div style={{ marginBottom: "1rem" }}>
						<label
							htmlFor="username"
							style={{ display: "block", marginBottom: "0.5rem" }}
						>
							Username
						</label>
						<input
							id="username"
							type="text"
							value={username}
							onChange={(e) => setUsername(e.target.value)}
							required
							style={{
								width: "100%",
								padding: "0.5rem",
								border: "1px solid #ccc",
								borderRadius: "4px",
							}}
							disabled={isLoading}
						/>
					</div>
					<div style={{ marginBottom: "1rem" }}>
						<label
							htmlFor="password"
							style={{ display: "block", marginBottom: "0.5rem" }}
						>
							Password
						</label>
						<input
							id="password"
							type="password"
							value={password}
							onChange={(e) => setPassword(e.target.value)}
							required
							style={{
								width: "100%",
								padding: "0.5rem",
								border: "1px solid #ccc",
								borderRadius: "4px",
							}}
							disabled={isLoading}
						/>
					</div>
					{error && (
						<div
							style={{
								color: "red",
								marginBottom: "1rem",
								padding: "0.5rem",
								backgroundColor: "#fee",
								borderRadius: "4px",
							}}
						>
							{error}
						</div>
					)}
					<button
						type="submit"
						disabled={isLoading}
						style={{
							width: "100%",
							padding: "0.75rem",
							backgroundColor: "#007bff",
							color: "white",
							border: "none",
							borderRadius: "4px",
							cursor: isLoading ? "not-allowed" : "pointer",
							opacity: isLoading ? 0.7 : 1,
						}}
					>
						{isLoading ? "Logging in..." : "Login"}
					</button>
				</form>
			</div>
		</div>
	);
}