mirror of
https://github.com/rjNemo/MERN_sample_app
synced 2026-06-06 08:46:39 +00:00
admin role constants
This commit is contained in:
parent
1f1db7c0ac
commit
e7f756a0ea
2 changed files with 8 additions and 7 deletions
|
|
@ -1,2 +1,4 @@
|
|||
import * as ROLES from "./roles";
|
||||
|
||||
export const AUTHENTICATED = (authUser) => !!authUser;
|
||||
export const ADMIN = (authUser) => authUser && !!authUser.roles[ROLES.ADMIN];
|
||||
|
|
|
|||
|
|
@ -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) => <Component {...props} />
|
||||
// : null;
|
||||
setAuthUser(authUser);
|
||||
}
|
||||
});
|
||||
}, [firebase.auth, condition, history]);
|
||||
render = (props) => <Component {...props} />;
|
||||
|
||||
return <Route path={path} render={render} {...rest} />;
|
||||
const render = (props) => <Component {...props} />;
|
||||
return authUser ? <Route path={path} render={render} {...rest} /> : null;
|
||||
};
|
||||
|
||||
export default withRouter(PrivateRoute);
|
||||
|
|
|
|||
Loading…
Reference in a new issue