我是新手做出反应并正在开发一个应用程序,
背景:
它有管理员,教师,学生,仪表板和静态登录页面,其中包含/ admin / login,/ faculty / login,/ student / login的按钮。它打开各自的仪表板。
问题:
*当我使用学生登录登录时,我能够访问所有仪表板,反之亦然,我的firebase节点中有一个名为role的字段,
*登录时我检查用户的角色并且不允许其他具有其他角色的用户登录,但是一旦我登录后我能够访问所有仪表板(不应该发生),包括我应该打开的仪表板,我正在使用react和routes.js,我的公共和私人路线。
我试图使用isstudent,isfaculty,isadmin标志来获取角色并限制访问但不能弄清楚如何遍历所有节点。
任何建议都会有所帮助,在此先感谢。
ROUTES.js
const Routes = props => {
if (props.user) {
let isStudent=false;
let isFaculty=false;
let isAdmin=false;
const uid = props.user.uid;
const request = firebase
.database()
.ref(`student/${uid}`)
.once("value")
.then(snapshot => {
if (snapshot.val().role === "student") {
// isStudent=true
console.log(snapshot.val());
}
});
firebase
.database()
.ref(`faculty/${uid}`)
.once("value")
.then(snapshot => {
if (snapshot.val().role === "faculty") {
console.log(snapshot.val());
//isFaculty=true
}
});
firebase
.database()
.ref(`admin/${uid}`)
.once("value")
.then(snapshot => {
if (snapshot.val().role === "admin") {
console.log(snapshot.val());
//isAdmin=true
}
});
return (
<MainLayout>
<Switch>
<AdminPublicRoute
{...props}
exact
restricted={true}
path="/admin/login"
component={AdminLogin}
/>
{isAdmin&&<AdminPrivateRoute
{...props}
path="/admin/admindashboard"
exact
component={AdminDash}
/>}
<FacultyPublicRoute
{...props}
exact
restricted={true}
path="/faculty/login"
component={FacultyLogin}
/>
{isFaculty && <FacultyPrivateRoute
{...props}
path="/faculty/facultydashboard"
exact
component={FacultyDash}
/>}
<StudentPublicRoute
{...props}
exact
restricted={true}
path="/student/login"
component={StudentLogin}
/>
{isStudent&& <StudentPrivateRoute
{...props}
path="/student/studentdashboard"
exact
component={StudentDash}
/>}
</Switch>
</MainLayout>
尼基特 - 加载地图:
const roles = _.map(obj.admin, item => item.role)
最简单 - Object.keys
const roles = Object.keys(obj.admin).map(key => key.role)