From 4598a261212b25fefbdc1356598c9da26a24efab Mon Sep 17 00:00:00 2001 From: Joshua Hsueh Date: Sat, 10 Apr 2021 12:52:03 -0400 Subject: [PATCH] deleted protected routes --- src/App.js | 11 ++--- src/components/CreatePool.js | 11 +---- src/components/Pool.js | 20 ++------ src/components/Pools.js | 9 +--- src/components/Profile.js | 10 +--- src/components/ProtectedRoute.js | 82 -------------------------------- src/components/UpdatePool.js | 23 ++------- 7 files changed, 17 insertions(+), 149 deletions(-) delete mode 100644 src/components/ProtectedRoute.js diff --git a/src/App.js b/src/App.js index e7f2e16..3094f03 100644 --- a/src/App.js +++ b/src/App.js @@ -12,7 +12,6 @@ import Main from './components/Main'; import 'bootstrap/dist/css/bootstrap.min.css'; import 'bootstrap/dist/js/bootstrap.bundle.min'; import './App.css'; -import ProtectedRoute from './components/ProtectedRoute.js'; function App() { return ( @@ -23,11 +22,11 @@ function App() { - - - - - + + + + + diff --git a/src/components/CreatePool.js b/src/components/CreatePool.js index 9e210bb..2c06ddd 100644 --- a/src/components/CreatePool.js +++ b/src/components/CreatePool.js @@ -3,15 +3,8 @@ import React, { useState, useEffect } from 'react'; const CreatePool = (props) => { const onSubmit = (e) => { e.preventDefault(); - const requestOptions = { - method: 'Pool', - headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer ${localStorage.getItem('token')}`, - }, - body: JSON.stringify({ Poolid: props.id }), - }; - fetch(`${process.env.REACT_APP_API_ENDPOINT}/createPool`, requestOptions) + + fetch(`${process.env.REACT_APP_API_ENDPOINT}/createPool`) .then((response) => response.json()) .then((data) => { console.log(data); diff --git a/src/components/Pool.js b/src/components/Pool.js index ed95c0d..9f0bdee 100644 --- a/src/components/Pool.js +++ b/src/components/Pool.js @@ -13,15 +13,8 @@ const Pool = (props) => { comments: ['What is the covid vaccination status of all the participants?'], }); - const requestOptions = { - method: 'GET', - headers: { - Authorization: `Bearer ${localStorage.getItem('token')}`, - }, - }; - const callAPI = () => { - fetch(`${process.env.REACT_APP_API_ENDPOINT}/pool/${id}`, requestOptions) + fetch(`${process.env.REACT_APP_API_ENDPOINT}/pool/${id}`) .then((response) => response.json()) .then((data) => { if (data !== undefined) { @@ -31,15 +24,8 @@ const Pool = (props) => { }; const onComment = (e) => { e.preventDefault(); - const requestOptions = { - method: 'pool', - headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer ${localStorage.getItem('token')}`, - }, - body: JSON.stringify({ poolid: props.id }), - }; - fetch(`${process.env.REACT_APP_API_ENDPOINT}/pool/comments`, requestOptions) + + fetch(`${process.env.REACT_APP_API_ENDPOINT}/pool/comments`) .then((response) => response.json()) .then((data) => { console.log(data); diff --git a/src/components/Pools.js b/src/components/Pools.js index 62c28ba..924496b 100644 --- a/src/components/Pools.js +++ b/src/components/Pools.js @@ -54,15 +54,8 @@ const Pools = (props) => { ], }); - const requestOptions = { - method: 'GET', - headers: { - Authorization: `Bearer ${localStorage.getItem('token')}`, - }, - }; - const callAPI = () => { - fetch(`${process.env.REACT_APP_API_ENDPOINT}/Pools/`, requestOptions) + fetch(`${process.env.REACT_APP_API_ENDPOINT}/pools/`) .then((response) => response.json()) .then((data) => { if (data !== undefined) { diff --git a/src/components/Profile.js b/src/components/Profile.js index f085c07..335aa14 100644 --- a/src/components/Profile.js +++ b/src/components/Profile.js @@ -43,17 +43,9 @@ const Profile = (props) => { ], groups: [], }); - const [stocks, setStocks] = useState([]); - - const requestOptions = { - method: 'GET', - headers: { - Authorization: `Bearer ${localStorage.getItem('token')}`, - }, - }; const callAPI = () => { - fetch(`${process.env.REACT_APP_API_ENDPOINT}/profile/`, requestOptions) + fetch(`${process.env.REACT_APP_API_ENDPOINT}/profile/`) .then((response) => response.json()) .then((data) => { if (data !== undefined) { diff --git a/src/components/ProtectedRoute.js b/src/components/ProtectedRoute.js deleted file mode 100644 index 366e9de..0000000 --- a/src/components/ProtectedRoute.js +++ /dev/null @@ -1,82 +0,0 @@ -import React from "react"; -import { Route, Redirect } from "react-router-dom"; - -const ProtectedRoute = ({ component: Component, ...rest }) => { - const isAuthenticated = async () => { - try { - const requestOptions = { - method: "GET", - headers: { Authorization: `Bearer ${localStorage.getItem("token")}` }, - }; - const response = await fetch( - `${process.env.REACT_APP_API_ENDPOINT}/profile`, - requestOptions - ); - - console.log(`Checking status! ${response.status}`); - if (response.status === 200) { - return true; - } else { - const requestOptions = { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: { refresh: localStorage.getItem("refresh") }, - }; - const response = await fetch( - `${process.env.REACT_APP_API_ENDPOINT}/token/refresh/`, - requestOptions - ); - - const data = response.json(); - - localStorage.setItem("token", data.access); - - return false; - } - } catch (e) { - const requestOptions = { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: { refresh: localStorage.getItem("refresh") }, - }; - const response = await fetch( - `${process.env.REACT_APP_API_ENDPOINT}/token/refresh/`, - requestOptions - ); - - const data = response.json(); - - localStorage.setItem("token", data.access); - - return false; - } - }; - - return ( - { - if (isAuthenticated()) { - return ; - } else { - return ( - - ); - } - }} - /> - ); -}; - -export default ProtectedRoute; diff --git a/src/components/UpdatePool.js b/src/components/UpdatePool.js index a9e8b26..0346d64 100644 --- a/src/components/UpdatePool.js +++ b/src/components/UpdatePool.js @@ -3,6 +3,7 @@ import React, { useState, useEffect } from 'react'; const UpdatePool = (props) => { const id = props.match.params.id; const [state, setState] = useState({ + id: 1, pool_title: 'TJ Carpool', pool_text: 'Carpool from TJ track to homes', start_time: '4/10/2021 3:00 PM', @@ -11,15 +12,9 @@ const UpdatePool = (props) => { participants: [], comments: ['What is the covid vaccination status of all the participants?'], }); - const requestOptions = { - method: 'GET', - headers: { - Authorization: `Bearer ${localStorage.getItem('token')}`, - }, - }; const callAPI = () => { - fetch(`${process.env.REACT_APP_API_ENDPOINT}/pool/${id}`, requestOptions) + fetch(`${process.env.REACT_APP_API_ENDPOINT}/pool/${id}`) .then((response) => response.json()) .then((data) => { if (data !== undefined) { @@ -29,15 +24,7 @@ const UpdatePool = (props) => { }; const onSubmit = (e) => { e.preventDefault(); - const requestOptions = { - method: 'Pool', - headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer ${localStorage.getItem('token')}`, - }, - body: JSON.stringify({ Poolid: props.id }), - }; - fetch(`${process.env.REACT_APP_API_ENDPOINT}/createPool`, requestOptions) + fetch(`${process.env.REACT_APP_API_ENDPOINT}/create_pool`) .then((response) => response.json()) .then((data) => { console.log(data); @@ -58,7 +45,7 @@ const UpdatePool = (props) => {

- Create Pool + Update Pool