我正在用咒语在流星中开发应用程序。这是一个 routes.jsx 的users
模块。我希望能够在其他模块的操作和路由中使用requireLogin
和。redirectUsers
或者一般来说,我如何在不违反咒语架构的情况下处理横切关注点?
import React from 'react';
import {mount} from 'react-mounter';
import Login from './containers/login';
import Register from './containers/register';
import App from '/client/modules/core/components/app.jsx';
export default function (injectDeps,{FlowRouter,Meteor,LocalState}) {
let userRoutes = FlowRouter.group({
prefix:'/user',
name:'user'
});
const AppCtx = injectDeps(App);
const redirectUsers = () => {
if(Meteor.userId()){
const path = LocalState.get('INTERRUPTED_REQUEST_PATH')
? LocalState.get('INTERRUPTED_REQUEST_PATH') : '/joke/';
FlowRouter.go(path);
return;
}
}
const requireLogin = (path) => {
if(!Meteor.userId()){
LocalState.set('INTERRUPTED_REQUEST_PATH',path);
FlowRouter.go('/user/login');
return;
}
}
userRoutes.route('/', {
triggersEnter: [(context,redirect) => {
if(!Meteor.userId()){
requireLogin('/user/login');
}
}],
action() {
}
});
userRoutes.route('/login',{
triggersEnter: [(context,redirect) => {
redirectUsers();
}],
action() {
mount(AppCtx, {
content: () => (<Login />)
});
}
});
}