···
1
1
// src/components/Admin/AdminRoute.jsx
2
2
import React, { useState, useEffect } from 'react';
3
3
-
import { Navigate } from 'react-router-dom';
4
3
import { supabase } from '../../lib/supabase';
5
4
import AdminPanel from './AdminPanel';
6
5
7
7
-
// This component protects the admin route by checking authentication
8
6
const AdminRoute = () => {
9
7
const [isAuthenticated, setIsAuthenticated] = useState(null);
10
8
const [isLoading, setIsLoading] = useState(true);
9
9
+
const [email, setEmail] = useState('');
10
10
+
const [password, setPassword] = useState('');
11
11
+
const [authError, setAuthError] = useState(null);
11
12
12
13
useEffect(() => {
13
14
const checkAuth = async () => {
···
36
37
};
37
38
}, []);
38
39
40
40
+
const handleLogin = async (e) => {
41
41
+
e.preventDefault();
42
42
+
setIsLoading(true);
43
43
+
setAuthError(null);
44
44
+
45
45
+
try {
46
46
+
const { data, error } = await supabase.auth.signInWithPassword({
47
47
+
email,
48
48
+
password
49
49
+
});
50
50
+
51
51
+
if (error) throw error;
52
52
+
53
53
+
setIsAuthenticated(true);
54
54
+
} catch (error) {
55
55
+
console.error('Error logging in:', error);
56
56
+
setAuthError(error.message);
57
57
+
} finally {
58
58
+
setIsLoading(false);
59
59
+
}
60
60
+
};
61
61
+
39
62
if (isLoading) {
40
63
return (
41
64
<div className="admin-loading">
···
45
68
);
46
69
}
47
70
48
48
-
// If not authenticated, redirect to home page
49
49
-
if (isAuthenticated === false) {
50
50
-
return <Navigate to="/" replace />;
71
71
+
// If not authenticated, show login form
72
72
+
if (!isAuthenticated) {
73
73
+
return (
74
74
+
<div className="admin-login-container">
75
75
+
<div className="admin-login-card">
76
76
+
<h2>Admin Login</h2>
77
77
+
{authError && <div className="auth-error">{authError}</div>}
78
78
+
<form onSubmit={handleLogin}>
79
79
+
<div className="form-group">
80
80
+
<label htmlFor="email">Email</label>
81
81
+
<input
82
82
+
type="email"
83
83
+
id="email"
84
84
+
value={email}
85
85
+
onChange={(e) => setEmail(e.target.value)}
86
86
+
required
87
87
+
/>
88
88
+
</div>
89
89
+
<div className="form-group">
90
90
+
<label htmlFor="password">Password</label>
91
91
+
<input
92
92
+
type="password"
93
93
+
id="password"
94
94
+
value={password}
95
95
+
onChange={(e) => setPassword(e.target.value)}
96
96
+
required
97
97
+
/>
98
98
+
</div>
99
99
+
<button type="submit" className="login-button">Login</button>
100
100
+
</form>
101
101
+
</div>
102
102
+
</div>
103
103
+
);
51
104
}
52
105
53
106
// If authenticated, render the admin panel