From e7f756a0ea18f9df74774ccd4c463a8c35b3b9b3 Mon Sep 17 00:00:00 2001 From: Ruidy Nemausat Date: Fri, 24 Apr 2020 14:25:25 +0200 Subject: [PATCH] admin role constants --- client/src/constants/authConditions.js | 2 ++ client/src/routes/PrivateRoute.jsx | 13 ++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/client/src/constants/authConditions.js b/client/src/constants/authConditions.js index e931333..66ed9d5 100644 --- a/client/src/constants/authConditions.js +++ b/client/src/constants/authConditions.js @@ -1,2 +1,4 @@ +import * as ROLES from "./roles"; + export const AUTHENTICATED = (authUser) => !!authUser; export const ADMIN = (authUser) => authUser && !!authUser.roles[ROLES.ADMIN]; diff --git a/client/src/routes/PrivateRoute.jsx b/client/src/routes/PrivateRoute.jsx index 2b278c8..03c59ec 100644 --- a/client/src/routes/PrivateRoute.jsx +++ b/client/src/routes/PrivateRoute.jsx @@ -1,4 +1,4 @@ -import React, { useEffect } from "react"; +import React, { useEffect, useState } from "react"; import { withRouter, Route } from "react-router-dom"; import { useFirebase } from "../services/auth"; import * as ROUTES from "../constants/routes"; @@ -10,22 +10,21 @@ const PrivateRoute = ({ history, ...rest }) => { + const [authUser, setAuthUser] = useState(null); const firebase = useFirebase(); - let render = null; + useEffect(() => { firebase.auth.onAuthStateChanged((authUser) => { if (!condition(authUser)) { history.push(ROUTES.SIGN_IN); } else { - // render = condition(authUser) - // ? (props) => - // : null; + setAuthUser(authUser); } }); }, [firebase.auth, condition, history]); - render = (props) => ; - return ; + const render = (props) => ; + return authUser ? : null; }; export default withRouter(PrivateRoute);