From 934b4dfb38f74f5df523966f10a1445c3e8bb755 Mon Sep 17 00:00:00 2001 From: Michael Fatemi Date: Sat, 10 Apr 2021 19:47:29 -0400 Subject: [PATCH] fix session tokens --- .gitignore | 2 +- src/components/AuthenticationWrapper.tsx | 6 +++--- src/components/Authenticator.tsx | 5 ++++- src/components/Profile.tsx | 23 +++++++++++------------ 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 4d29575..04c5395 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. # dependencies -/node_modules +node_modules /.pnp .pnp.js diff --git a/src/components/AuthenticationWrapper.tsx b/src/components/AuthenticationWrapper.tsx index fdcc6a7..d3df9c6 100644 --- a/src/components/AuthenticationWrapper.tsx +++ b/src/components/AuthenticationWrapper.tsx @@ -7,7 +7,7 @@ export default function AuthenticationWrapper({ }: { children: React.ReactNode; }) { - const sessionId = localStorage.getItem('session_id'); + const sessionToken = localStorage.getItem('session_token'); // Prevent race conditions const [authState, setAuthState] = useState({ isLoggedIn: null, @@ -16,7 +16,7 @@ export default function AuthenticationWrapper({ }); const refreshAuthState = useCallback(() => { - if (sessionId) { + if (sessionToken) { getMe().then((user) => { if (user) { setAuthState({ isLoggedIn: true, user, refreshAuthState }); @@ -27,7 +27,7 @@ export default function AuthenticationWrapper({ } else { setAuthState({ isLoggedIn: false, user: null, refreshAuthState }); } - }, [sessionId]); + }, [sessionToken]); useEffect(() => { refreshAuthState(); diff --git a/src/components/Authenticator.tsx b/src/components/Authenticator.tsx index 2002aa3..e0d3d59 100644 --- a/src/components/Authenticator.tsx +++ b/src/components/Authenticator.tsx @@ -1,11 +1,13 @@ -import { useEffect, useState } from 'react'; +import { useContext, useEffect, useState } from 'react'; import { Redirect, useLocation, useParams } from 'react-router-dom'; import { API_ENDPOINT } from '../api/api'; +import AuthenticationContext from './AuthenticationContext'; export default function Authenticator() { const { provider } = useParams<{ provider: string }>(); const query = new URLSearchParams(useLocation().search); const code = query.get('code'); + const { refreshAuthState } = useContext(AuthenticationContext); const [status, setStatus] = useState<'pending' | 'errored' | 'authenticated'>( 'pending' ); @@ -25,6 +27,7 @@ export default function Authenticator() { response.json().then((json) => { if (json.status === 'success') { localStorage.setItem('session_token', json.token); + refreshAuthState && refreshAuthState(); setStatus('authenticated'); } else { setStatus('errored'); diff --git a/src/components/Profile.tsx b/src/components/Profile.tsx index 973c036..2f68f03 100644 --- a/src/components/Profile.tsx +++ b/src/components/Profile.tsx @@ -6,7 +6,7 @@ import CardContent from '@material-ui/core/CardContent'; import { makeStyles } from '@material-ui/core/styles'; import Typography from '@material-ui/core/Typography'; import { useContext, useEffect, useState } from 'react'; -import { API_ENDPOINT } from '../api/api'; +import { makeAPIGetCall } from '../api/utils'; import AuthenticationContext from './AuthenticationContext'; const useStyles = makeStyles({ @@ -21,7 +21,8 @@ const useStyles = makeStyles({ const Profile = () => { const { user, isLoggedIn } = useContext(AuthenticationContext); const [groups, setGroups] = useState([]); - const [pools, setPools] = useState([ + const [pools, setPools] = useState([ + /* { id: 1, pool_title: 'TJ Carpool', @@ -69,19 +70,17 @@ const Profile = () => { comments: [ 'What is the covid vaccination status of all the participants?', ], - }, + },*/ ]); const classes = useStyles(); useEffect(() => { console.log(process.env); - fetch(`${API_ENDPOINT}/my_pools`) - .then((response) => response.json()) - .then((json) => { - if (json) { - setPools(json.data); - } - }); + makeAPIGetCall('/my_pools').then((response) => { + if (response.data.data) { + setPools(response.data.data); + } + }); }, []); if (!user) { @@ -113,14 +112,14 @@ const Profile = () => { - {pool.pool_title} + {pool.title} - {pool.pool_text} + {pool.description}