From 79e4f41d4e53d171e5cab5b86c307c4951628283 Mon Sep 17 00:00:00 2001 From: Alberto Defendi <1369-ahl-berto@users.noreply.gitlab.inf.unibz.it> Date: Thu, 20 May 2021 14:27:32 +0200 Subject: [PATCH] Made promise return only response. The passage of the react state component is now not necessary since the returned data is already usable. --- src/App.tsx | 11 ++--------- src/api/isAuthenticated.ts | 8 ++------ .../AuthUser/SignInForm/SignInForm.tsx | 13 ++++++++++++- .../RestrictedRoute/RestrictedRoute.tsx | 16 ++++++++++++++-- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 9c2a453..9b56843 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -22,7 +22,6 @@ import { getRole } from 'api/getRole'; * Entry point of the app. */ export const App: FC = () => { - let isMounted = true; configDjangoCookieName(); const [role, setRole] = useState(''); const [isAuth, setIsAuth] = useState<boolean>(false); @@ -31,16 +30,10 @@ export const App: FC = () => { const value = { role, setRole, isAuth, setIsAuth }; useEffect(() => { - if (!isCookieFetched && isMounted) fetchCookie(setIsCookieFetched); - // Initialize asking the server if this session is already logged in. - isAuthenticated(setIsAuth); + isAuthenticated().then((state) => setIsAuth(state)); getRole(setRole); - - return () => { - isMounted = false; - }; - }, [isCookieFetched]); + }, [isAuth, role]); return ( <ThemeProvider theme={muiTheme}> diff --git a/src/api/isAuthenticated.ts b/src/api/isAuthenticated.ts index 656a7cc..1195e01 100644 --- a/src/api/isAuthenticated.ts +++ b/src/api/isAuthenticated.ts @@ -1,10 +1,6 @@ import axios from 'axios'; -export const isAuthenticated = async ( - setIsAuth: React.Dispatch<React.SetStateAction<boolean>>, -): Promise<void> => { - const response = await axios('/api/web/login/is_authenticated').then( +export const isAuthenticated = async (): Promise<boolean> => + axios('/api/web/login/is_authenticated').then( (res) => res.data.is_authenticated, ); - setIsAuth(response); -}; diff --git a/src/components/AuthUser/SignInForm/SignInForm.tsx b/src/components/AuthUser/SignInForm/SignInForm.tsx index de15be6..2ac92e9 100644 --- a/src/components/AuthUser/SignInForm/SignInForm.tsx +++ b/src/components/AuthUser/SignInForm/SignInForm.tsx @@ -6,6 +6,7 @@ import { InputField } from 'components/AuthUser/InputField/InputField'; import { useHistory } from 'react-router-dom'; import { AuthRoutes, NonAuthRoutes } from 'api/routes'; import { AuthContext } from 'components/AuthUser/AuthContext'; +import { fetchCookie } from 'api/fetchCookie'; import { useStyles } from './useStyles'; import { CredentialsType } from './CredentialsType'; @@ -17,6 +18,16 @@ const defaultValues = { export const SignInForm: FC = () => { const history = useHistory(); const { setRole, setIsAuth } = useContext(AuthContext); + const [cookie, setCookie] = useState<string>(''); + + useEffect(() => { + let isMounted = true; + if (isMounted) fetchCookie(setCookie); + + return () => { + isMounted = false; + }; + }, [fetchCookie]); const { control, errors, setError, handleSubmit } = useForm<CredentialsType>({ defaultValues, @@ -31,7 +42,7 @@ export const SignInForm: FC = () => { { username: values.username, password: values.password, - csrfmiddlewaretoken: localStorage.getItem('COOKIE'), + csrfmiddlewaretoken: cookie, }, { headers: { diff --git a/src/components/RestrictedRoute/RestrictedRoute.tsx b/src/components/RestrictedRoute/RestrictedRoute.tsx index 3e3b5ee..24fbaa4 100644 --- a/src/components/RestrictedRoute/RestrictedRoute.tsx +++ b/src/components/RestrictedRoute/RestrictedRoute.tsx @@ -23,9 +23,21 @@ type Props = { export const RestrictedRoute = ({ Component, path }: Props): JSX.Element => { const [authUser, setAuthUser] = useState<boolean>(false); const [isLoading, setLoading] = useState<boolean>(false); + useEffect(() => { - isAuthenticated(setAuthUser).then(() => setLoading(true)); - }); + let isMounted = true; + + isAuthenticated().then((state) => { + if (isMounted) { + setAuthUser(state); + setLoading(true); + } + }); + + return () => { + isMounted = false; + }; + }, [isLoading]); return !isLoading ? ( <CircularProgress /> -- GitLab