diff --git a/friends/src/App.js b/friends/src/App.js
index ed22f9c9c..77506d631 100644
--- a/friends/src/App.js
+++ b/friends/src/App.js
@@ -1,17 +1,35 @@
import React from 'react';
import './App.css';
-import { BrowserRouter as Router, Route} from 'react-router-dom';
+import { BrowserRouter as Router, Route, Link, Switch} from 'react-router-dom';
+import PrivateRoute from './components/PrivateRoute';
+import Login from './components/login';
+import Logout from './components/logout';
+import Friends from './components/friends';
-const Login = ()=> {
- return (
Login
)
-}
-
function App() {
return (
+
Client Auth Project
+
+ -
+ Login
+
+ -
+ Logout
+
+ -
+ Friends
+
+
+
+
+
+
+
+
);
}
diff --git a/friends/src/components/PrivateRoute.js b/friends/src/components/PrivateRoute.js
new file mode 100644
index 000000000..9b6126c77
--- /dev/null
+++ b/friends/src/components/PrivateRoute.js
@@ -0,0 +1,14 @@
+import React from 'react';
+import { Route, Redirect } from 'react-router-dom';
+
+const PrivateRoute = ({component:Component, ...rest}) => {
+ return {
+ if (localStorage.getItem("token")) {
+ return
+ } else {
+ return
+ }
+ }}/>
+}
+
+export default PrivateRoute;
\ No newline at end of file
diff --git a/friends/src/components/addFriend.js b/friends/src/components/addFriend.js
new file mode 100644
index 000000000..4c83f68f9
--- /dev/null
+++ b/friends/src/components/addFriend.js
@@ -0,0 +1,60 @@
+import React, {useHistory} from "react";
+import axios from "axios";
+import axiosWithAuth from "../utils/axiosWithAuth";
+
+
+class AddFriend extends React.Component {
+ state = {
+ credentials:
+ {
+ id: '',
+ name: '',
+ age: '',
+ email: '',
+ }
+ }
+ handleChange = e => {
+ this.setState({
+ credentials: {
+ ...this.state.credentials,
+ [e.target.name]: e.target.value
+ }
+ });
+ };
+
+ handleOnSubmit = e => {
+ axiosWithAuth.post('/friends', this.state.credentials)
+ .then(res => {
+ this.props.history.push('/friends')
+ })
+ }
+
+ render(){
+ return (
+
+
+
+ )
+ }
+}
+export default AddFriend
diff --git a/friends/src/components/friends.js b/friends/src/components/friends.js
new file mode 100644
index 000000000..e64bd2546
--- /dev/null
+++ b/friends/src/components/friends.js
@@ -0,0 +1,55 @@
+import React from "react";
+import axiosWithAuth from "../utils/axiosWithAuth";
+import AddFriend from "./addFriend";
+
+class Friends extends React.Component {
+
+ state = {
+ list: []
+ };
+
+ componentDidMount(){
+ axiosWithAuth()
+ .get('/friends')
+ .then(res=>{
+ this.setState({
+ ...this.state,
+ list: res.data
+ });
+ })
+ .catch(err=>{
+ console.log(err)
+ })
+ }
+ componentDidUpdate(){
+ console.log(this.state.list)
+ }
+ render()
+
+ {
+ return(
+
+ {this.state.list.map(friend => {
+ return (
+
+
{friend.name}
+
{friend.email}
+
+ )
+ })}
+ {this.state.list.length > 0 &&
+ (
+
+
{this.state.list.length} Friends
+
+ )}
+
+
+
+ )
+ }
+}
+
+export default Friends;
\ No newline at end of file
diff --git a/friends/src/components/login.js b/friends/src/components/login.js
new file mode 100644
index 000000000..eb4b2e61f
--- /dev/null
+++ b/friends/src/components/login.js
@@ -0,0 +1,65 @@
+import React, { useHistory } from "react";
+import axios from "axios";
+
+class Login extends React.Component {
+ state={
+ credentials: {
+ username: '',
+ password: ''
+ },
+ isLoading: false
+ }
+
+ handleChange = e => {
+ this.setState({
+ credentials: {
+ ...this.state.credentials,
+ [e.target.name]: e.target.value
+ }
+ })
+ }
+
+ login = e => {
+ e.preventDefault();
+
+ axios.post('http://localhost:5000/api/login', this.state.credentials)
+ .then(res => {
+ localStorage.setItem('token', res.data.payload);
+ this.props.history.push('/friends');
+ })
+ .catch(err => {
+ console.log(err);
+ })
+ };
+
+ componentDidMount(){
+ setTimeout(() => { this.setState({
+ isLoading: !this.state.isLoading
+ }) }, 1000)
+ }
+
+ render() {
+ return (
+
+ {this.state.isLoading === false &&
Loading User Login
}
+ {this.state.isLoading && }
+
+ );
+ }
+}
+
+export default Login;
\ No newline at end of file
diff --git a/friends/src/components/logout.js b/friends/src/components/logout.js
new file mode 100644
index 000000000..ecebac489
--- /dev/null
+++ b/friends/src/components/logout.js
@@ -0,0 +1,20 @@
+import React, { useEffect } from "react";
+import { useHistory } from "react-router";
+
+import axiosWithAuth from "../utils/axiosWithAuth";
+
+const Logout = (props) => {
+ const {push} = useHistory();
+
+ useEffect(()=>{
+ axiosWithAuth()
+ .post('/logout')
+ .then(res => {
+ localStorage.removeItem('token')
+ push('/login')
+ })
+ }, []);
+ return ()
+}
+
+export default Logout
\ No newline at end of file
diff --git a/friends/src/utils/axiosWithAuth.js b/friends/src/utils/axiosWithAuth.js
new file mode 100644
index 000000000..1f349b40a
--- /dev/null
+++ b/friends/src/utils/axiosWithAuth.js
@@ -0,0 +1,14 @@
+import axios from 'axios';
+
+const axiosWithAuth = ()=> {
+ const payload = localStorage.getItem('token');
+
+ return axios.create({
+ headers: {
+ authorization: payload
+ },
+ baseURL: 'http://localhost:5000/api'
+ });
+}
+
+export default axiosWithAuth;
\ No newline at end of file
diff --git a/package.json b/package.json
index 954bad52e..39d3dc076 100644
--- a/package.json
+++ b/package.json
@@ -18,4 +18,3 @@
"author": "Lambda School",
"license": "ISC"
}
-