mirror of
https://github.com/rjNemo/MERN_sample_app
synced 2026-06-12 03:26:40 +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 AUTHENTICATED = (authUser) => !!authUser;
|
||||||
export const ADMIN = (authUser) => authUser && !!authUser.roles[ROLES.ADMIN];
|
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 { withRouter, Route } from "react-router-dom";
|
||||||
import { useFirebase } from "../services/auth";
|
import { useFirebase } from "../services/auth";
|
||||||
import * as ROUTES from "../constants/routes";
|
import * as ROUTES from "../constants/routes";
|
||||||
|
|
@ -10,22 +10,21 @@ const PrivateRoute = ({
|
||||||
history,
|
history,
|
||||||
...rest
|
...rest
|
||||||
}) => {
|
}) => {
|
||||||
|
const [authUser, setAuthUser] = useState(null);
|
||||||
const firebase = useFirebase();
|
const firebase = useFirebase();
|
||||||
let render = null;
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
firebase.auth.onAuthStateChanged((authUser) => {
|
firebase.auth.onAuthStateChanged((authUser) => {
|
||||||
if (!condition(authUser)) {
|
if (!condition(authUser)) {
|
||||||
history.push(ROUTES.SIGN_IN);
|
history.push(ROUTES.SIGN_IN);
|
||||||
} else {
|
} else {
|
||||||
// render = condition(authUser)
|
setAuthUser(authUser);
|
||||||
// ? (props) => <Component {...props} />
|
|
||||||
// : null;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, [firebase.auth, condition, history]);
|
}, [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);
|
export default withRouter(PrivateRoute);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue