我正在开发一个应用程序,其中我将使用两个角色admin和customer,它们基本上是没有角色的用户。
我写了以下代码,在其中指定所需的路由以及应验证哪些页面。但是,当我检查用户是否具有管理员角色时,即使MongoDB显示该用户具有管理员角色(示例位于代码下方),它始终返回false。
到目前为止我写的是
import { Meteor } from 'meteor/meteor';
import { Router, Route, browserHistory } from 'react-router';
import { Roles } from 'meteor/alanning:roles';
import React, { Component } from 'react';
import AuthenticationLayout from '../interface/layouts/authentication-layout';
import AdminLayout from '../interface/layouts/admin-layout';
import CustomerLayout from '../interface/layouts/customer-layout';
import NotFound from '../interface/pages/notfound';
import Login from '../interface/pages/login';
import SignUp from '../interface/pages/signup';
import AdminDashboard from '../interface/pages/admin/dashboard';
import CustomerOverview from '../interface/pages/admin/customers/customer-overview';
import ChatsOverview from '../interface/pages/admin/chats/chats-overview';
import LogsOverview from '../interface/pages/admin/logs/logs-overview';
import CustomerDashboard from '../interface/pages/customer/dashboard';
import SettingsOverview from '../interface/pages/admin/settings/settings-overview';
import RegistrationCardSettings from '../interface/pages/admin/settings/registration-cards';
const onEnterPublicPage = () => {
if ( Meteor.userId() ) {
browserHistory.replace('/klant');
}
};
const onEnterPrivatePage = () => {
if ( !Meteor.userId() ) {
browserHistory.replace('/');
}
};
const unauthenticatedPages = [
'/',
'/login',
'/signup'
];
const authenticatedPages = [
'/admin',
'/admin/klanten',
'/admin/chats',
'/admin/logboeken',
'/admin/instellingen',
'/admin/instellingen/strippenkaart',
'/klant'
];
export const onAuthChange = (isAuthenticated) => {
Tracker.autorun(() => {
const isAuthenticated = !!Meteor.userId();
const pathname = browserHistory.getCurrentLocation().pathname;
const isUnauthenticatedPage = unauthenticatedPages.includes(pathname);
const isAuthenticatedPage = authenticatedPages.includes(pathname);
const isAdmin = Roles.userIsInRole( Meteor.userId(), 'admin' );
console.log(isAdmin); // always returns false..
if (isUnauthenticatedPage && isAuthenticated) {
browserHistory.replace('/klant');
} else if (isAuthenticatedPage && isAuthenticated) {
if( isAdmin ) {
browserHistory.replace('/admin');
} else {
browserHistory.replace('/klant');
}
} else if (isAuthenticatedPage && !isAuthenticated) {
browserHistory.replace('/');
}
});
};
export const routes = (
<Router history={browserHistory}>
<Route component={AuthenticationLayout}>
<Route path="/login" component={Login} />
<Route path="/" component={Login} onEnter={onEnterPublicPage} />
</Route>
<Route component={AdminLayout}>
<Route path="/admin" component={AdminDashboard} onEnter={onEnterPrivatePage}/>
<Route path="/admin/klanten" component={CustomerOverview} onEnter={onEnterPrivatePage}/>
<Route path="/admin/chats" component={ChatsOverview} onEnter={onEnterPrivatePage}/>
<Route path="/admin/logboeken" component={LogsOverview} onEnter={onEnterPrivatePage}/>
<Route path="/admin/instellingen" component={SettingsOverview} onEnter={onEnterPrivatePage}/>
<Route path="/admin/instellingen/strippenkaart" component={RegistrationCardSettings} onEnter={onEnterPrivatePage}/>
</Route>
<Route component={CustomerLayout}>
<Route path="/klant" component={CustomerDashboard} onEnter={onEnterPrivatePage}/>
</Route>
<Route path="/signup" component={SignUp}/>
<Route path="*" component={NotFound} />
</Router>
);
具有管理员角色的用户: 我首先在跟踪器外部进行了管理员检查,但是不幸的是,将其放在跟踪器中并没有解决问题:
const isAdmin = Roles.userIsInRole( Meteor.userId(), 'admin' );
console.log(isAdmin); // always returns false..