From 07d7dacaa51f7656d60136470ccbd26932f428bc Mon Sep 17 00:00:00 2001 From: Anthony Climer Date: Mon, 27 Mar 2023 21:12:31 -0400 Subject: [PATCH] I have finished this project --- src/App.css | 8 +++++ src/App.js | 53 +++++++++++++++++++++++++++--- src/components/AddFriend.js | 60 ++++++++++++++++++++++++++++++++++ src/components/FriendList.js | 37 +++++++++++++++++++++ src/components/Login.js | 53 ++++++++++++++++++++++++++++++ src/components/Logout.js | 25 ++++++++++++++ src/components/PrivateRoute.js | 14 ++++++++ src/utils/axiosWithAuth.js | 14 ++++++++ 8 files changed, 260 insertions(+), 4 deletions(-) create mode 100644 src/components/AddFriend.js create mode 100644 src/components/FriendList.js create mode 100644 src/components/Login.js create mode 100644 src/components/Logout.js create mode 100644 src/components/PrivateRoute.js create mode 100644 src/utils/axiosWithAuth.js diff --git a/src/App.css b/src/App.css index 74b5e0534..956f0aba5 100644 --- a/src/App.css +++ b/src/App.css @@ -36,3 +36,11 @@ transform: rotate(360deg); } } + + +.link { + padding: 1em; + text-decoration: none; + color: black; + font-size: 17px; +} \ No newline at end of file diff --git a/src/App.js b/src/App.js index 1b971d1f6..7aec24fa9 100644 --- a/src/App.js +++ b/src/App.js @@ -1,12 +1,57 @@ import React from 'react'; import './App.css'; -import { BrowserRouter as Router, Route} from 'react-router-dom'; +import { BrowserRouter as Router, Route, Routes, Link} from 'react-router-dom'; +import Login from './components/login'; +import FriendsList from './components/FriendList'; +import AddFriend from './components/AddFriend'; +import Logout from './components/Logout'; + +import PrivateRoute from './components/PrivateRoute'; function App() { return ( -
-

Client Auth Project

-
+
+
+

Friends Database

+ Login + Friends + Add Friends + Logout +
+ {/* Home */} + + }> + + + + {/* Login */} + + }> + + + + {/* Friendlist */} + {/* */} + + }> + } /> + + + + {/* Addfriend */} + + }> + }/> + + + + {/* Logout */} + + }> + }/> + + +
); } diff --git a/src/components/AddFriend.js b/src/components/AddFriend.js new file mode 100644 index 000000000..3912ee839 --- /dev/null +++ b/src/components/AddFriend.js @@ -0,0 +1,60 @@ +import React, {useState} from "react"; +import { useNavigate } from "react-router-dom"; +import axios from "axios"; + +const AddFriend = () => { + let navigate = useNavigate(); + const [form, setForm] = useState({ + name:'', + age:'', + email:'', + }) + + const handleChange = e => { + setForm({ + ...form, + [e.target.name]: e.target.value + }) + } + + const handleSubmit = e => { + e.preventDefault() + const token = localStorage.getItem('token'); + axios.post('http://localhost:9000/api/friends', form, { + headers: { + authorization: token + } + }) + .then(res => { + navigate('/friends') + }) + .catch(err => {console.error(err)}) + } + + console.log(form) + return ( +
+

Add a friend

+
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+
+ ) + } + +export default AddFriend \ No newline at end of file diff --git a/src/components/FriendList.js b/src/components/FriendList.js new file mode 100644 index 000000000..41b573e14 --- /dev/null +++ b/src/components/FriendList.js @@ -0,0 +1,37 @@ +import React,{ useState , useEffect} from "react"; +import axios from 'axios' +import axiosWithAuth from "../utils/axiosWithAuth"; + +const FriendsList = () => { + const [friends, setFriends] = useState([]); + + useEffect(()=>{ + const token = localStorage.getItem('token') + + axiosWithAuth().get('/friends') + .then(res => { + console.log(res.data) + setFriends(res.data) + }) + .catch(err => { + console.error(err) + }) + }, []) + + return ( +
+

My friends

+ +
+ ) + } + + export default FriendsList \ No newline at end of file diff --git a/src/components/Login.js b/src/components/Login.js new file mode 100644 index 000000000..7dd95debf --- /dev/null +++ b/src/components/Login.js @@ -0,0 +1,53 @@ +import axios from 'axios'; +import React, {useState} from 'react' +import { useNavigate } from 'react-router-dom'; + + +const Login = () => { +let navigate = useNavigate() + + const [cred, setCred] = useState({ + username: '', + password:'', + }); + + const handleChange = e => { + setCred({ + ...cred, + [e.target.name]:e.target.value, + }) + } + + const handleSubmit = e => { + e.preventDefault(); + axios.post('http://localhost:9000/api/login', cred) + .then(res => { + console.log(res.data.token) + localStorage.setItem('token', res.data.token) + navigate('/friends') + }) + .catch(err => console.error(err)) + } + + + return ( +
+

Login

+
+ +
+ + +
+ +
+ + +
+ +
+
+ ) + } + +export default Login \ No newline at end of file diff --git a/src/components/Logout.js b/src/components/Logout.js new file mode 100644 index 000000000..7c163302e --- /dev/null +++ b/src/components/Logout.js @@ -0,0 +1,25 @@ +import React, {useEffect} from 'react'; +import axios from 'axios'; +import { useNavigate } from 'react-router-dom'; + +const Logout = () => { + let navigate = useNavigate(); + const token = localStorage.getItem('token') + useEffect(()=>{ + axios.post('http://localhost:9000/api/logout',{}, { + headers: { + authorization: token + } + }) + .then(res => { + localStorage.removeItem('token') + navigate('/login') + }) + .catch(err => { + console.error(err) + }) + },[]) + +} + +export default Logout \ No newline at end of file diff --git a/src/components/PrivateRoute.js b/src/components/PrivateRoute.js new file mode 100644 index 000000000..f85c3eda6 --- /dev/null +++ b/src/components/PrivateRoute.js @@ -0,0 +1,14 @@ +import React from 'react'; +import {Navigate, Outlet} from 'react-router-dom' + +const PrivateRoute = () => { + let userId = localStorage.getItem('token') == null ? false : true + console.log(userId) + return ( + <> + {userId ? : } + + ) +} + +export default PrivateRoute \ No newline at end of file diff --git a/src/utils/axiosWithAuth.js b/src/utils/axiosWithAuth.js new file mode 100644 index 000000000..0a9476d24 --- /dev/null +++ b/src/utils/axiosWithAuth.js @@ -0,0 +1,14 @@ +import axios from "axios"; + +const axiosWithAuth = () => { + const token = localStorage.getItem('token') + + return axios.create({ + headers:{ + authorization: token + }, + baseURL:'http://localhost:9000/api' + }) +} + +export default axiosWithAuth \ No newline at end of file