From e0ab73361a8bb3598ced59270e6824aa80b80b48 Mon Sep 17 00:00:00 2001 From: Biswakalyan Bhuyan Date: Thu, 25 Jul 2024 13:13:05 +0530 Subject: added admin page --- src/App.js | 24 ++++++++++++++++++------ src/components/Admin.js | 35 +++++++++++++++++++++++++++++++++++ src/components/Login.js | 4 +++- src/components/MyComponent.js | 7 ------- 4 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 src/components/Admin.js delete mode 100644 src/components/MyComponent.js (limited to 'src') diff --git a/src/App.js b/src/App.js index 36c6c6a..7f23517 100644 --- a/src/App.js +++ b/src/App.js @@ -1,15 +1,27 @@ import React from 'react'; +import { BrowserRouter as Router, Route, Routes, Navigate } from 'react-router-dom'; import './App.css'; import Login from './components/Login'; +import Admin from './components/Admin'; function App() { + const isAuthenticated = () => { + return !!localStorage.getItem('jwtToken'); + }; + return ( -
-
-

Admin Panel

- -
-
+ +
+ + } /> + : } + /> + } /> + +
+
); } diff --git a/src/components/Admin.js b/src/components/Admin.js new file mode 100644 index 0000000..aeb8bf6 --- /dev/null +++ b/src/components/Admin.js @@ -0,0 +1,35 @@ +import React, { useEffect, useState } from 'react'; +import axios from 'axios'; + +function Admin() { + const [message, setMessage] = useState(''); + + useEffect(() => { + const fetchData = async () => { + const token = localStorage.getItem('jwtToken'); + if (token) { + try { + const response = await axios.get('http://localhost:5000/admin', { + headers: { Authorization: `Bearer ${token}` }, + }); + setMessage(response.data.message); + } catch (error) { + console.error('Error fetching admin data', error); + } + } else { + setMessage('You are not authorized to view this page.'); + } + }; + + fetchData(); + }, []); + + return ( +
+

Admin Panel

+

{message}

+
+ ); +} + +export default Admin; diff --git a/src/components/Login.js b/src/components/Login.js index c73b59a..717a3ae 100644 --- a/src/components/Login.js +++ b/src/components/Login.js @@ -1,10 +1,12 @@ import React, { useState } from 'react'; import axios from 'axios'; +import { useNavigate } from 'react-router-dom'; function Login() { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [error, setError] = useState(''); + const navigate = useNavigate(); // Initialize useNavigate hook const handleSubmit = async (event) => { event.preventDefault(); @@ -17,7 +19,7 @@ function Login() { const { token } = response.data; console.log('Login successful, token:', token); localStorage.setItem('jwtToken', token); - // Redirect to admin panel or another page after successful login + navigate('/admin'); // Redirect to the admin page } catch (error) { setError('Invalid credentials'); console.error('Error logging in', error); diff --git a/src/components/MyComponent.js b/src/components/MyComponent.js deleted file mode 100644 index b9434ea..0000000 --- a/src/components/MyComponent.js +++ /dev/null @@ -1,7 +0,0 @@ -import React from 'react'; - -function MyComponent() { - return
This is my component!
; -} - -export default MyComponent; \ No newline at end of file -- cgit v1.2.3-59-g8ed1b