我正在尝试使用Flow Router为我的Meteor应用程序实现自定义登录,授权和角色。我正在使用最新版本的Meteor。我也更新了所有包。我登录时收到错误。
注册和创建帐户非常有效。我的凭据已保存,用户已创建。
我在loggedIn.js
中设置会话变量,然后在点击登录按钮时在configure.js
中检索它,并且“应该”将我路由到我在登录前尝试去的所需路线,但我得到的是“会话未定义”错误。以下是我收到的两个错误:
Exception in onLogin callback: TypeError: Cannot read property 'replace' of undefined
at Router.path (http://localhost:3000/packages/kadira_flow-router.js?hash=09ea12875d3801955ee70797bf8e4a70feebc570:325:18)
at Router.go (http://localhost:3000/packages/kadira_flow-router.js?hash=09ea12875d3801955ee70797bf8e4a70feebc570:360:19)
at http://localhost:3000/app/lib/routes/configure.js?hash=1ca98e9145d8b9d63189b16a8d872866175709b0:15:25
at runAndHandleExceptions (http://localhost:3000/packages/callback-hook.js?hash=fff7fdef0707c85900b21f766a4b6c65bf278ff4:162:24)
at http://localhost:3000/packages/callback-hook.js?hash=fff7fdef0707c85900b21f766a4b6c65bf278ff4:169:12
at http://localhost:3000/packages/accounts-base.js?hash=db584b046b0a64d03bfcbf1cd84a8b38f83ddc0d:290:9
at Hook.each (http://localhost:3000/packages/callback-hook.js?hash=fff7fdef0707c85900b21f766a4b6c65bf278ff4:138:15)
at http://localhost:3000/packages/accounts-base.js?hash=db584b046b0a64d03bfcbf1cd84a8b38f83ddc0d:289:25
at http://localhost:3000/packages/underscore.js?hash=27b3d669b418de8577518760446467e6ff429b1e:794:19
at loggedInAndDataReadyCallback (http://localhost:3000/packages/accounts-base.js?hash=db584b046b0a64d03bfcbf1cd84a8b38f83ddc0d:411:5)
从cmd行:
I20160818-21:36:28.962(-7)? Exception in onLogin callback: ReferenceError: Session is not defined
I20160818-21:36:29.266(-7)? at app\lib\routes\configure.js:9:3
I20160818-21:36:29.267(-7)? at runAndHandleExceptions (packages/callback-hook/hook.js:133:1)
I20160818-21:36:29.267(-7)? at packages/callback-hook/hook.js:140:1
I20160818-21:36:29.267(-7)? at packages/accounts- base/accounts_server.js:167:5
I20160818-21:36:29.267(-7)? at [object Object]._.extend.each (packages/callback-hook/hook.js:109:1)
I20160818-21:36:29.267(-7)? at AccountsServer.Ap._successfulLogin (packages/accounts-base/accounts_server.js:166:21)
I20160818-21:36:29.267(-7)? at AccountsServer.Ap._attemptLogin (packages/accounts-base/accounts_server.js:356:10)
I20160818-21:36:29.267(-7)? at [object Object].methods.login (packages/accounts-base/accounts_server.js:533:21)
I20160818-21:36:29.267(-7)? at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1711:12)
I20160818-21:36:29.267(-7)? at packages/ddp-server/livedata_server.js:711:19
我已多次删除并重新添加Sessions包。以下是我目前使用的所有软件包的列表:
less@2.7.4
twbs:bootstrap@3.3.6
fortawesome:fontawesome
jquery@1.11.9
okgrow:router-autoscroll
meteor-base@1.0.4
mobile-experience@1.0.4
mongo@1.1.10
blaze-html-templates@1.0.4
tracker@1.1.0
logging@1.1.14
reload@1.1.10
random@1.0.10
ejson@1.0.12
spacebars@1.0.12
check@1.2.3
kadira:flow-router
meteorhacks:fast-render
kadira:blaze-layout
zimme:active-route
arillo:flow-router-helpers
accounts-password@1.2.12
alanning:roles
accounts-base@1.2.9
standard-minifier-css
standard-minifier-js
meteortoys:allthings@3.0.0
session
对于我到目前为止创建的不同路由,我有3个Flow Router文件。
在configure.js
中,这是Session is not defined
错误开始的地方:
Accounts.onLogin(function() { // This makes sure the user goes to the route that he wanted after he successfully logged in.
var redirect = Session.get('redirectAfterLogin');
console.log(redirect);
if (redirect !== null) { // added this check here because some async behaviour in either FlowRouter or onLogin hook can cause wrong redirect to the ‘ login’ page again. This explicit check solves that issue.
if (redirect !== '/login') {
return FlowRouter.go(redirect);
}
}
});
在loggedIn.js
中,我为登录的用户路线创建了一个组路由:
var loggedIn = FlowRouter.group({
name: loggedIn,
triggersEnter: [ function() { //whenever someone enters a route in this group, the trigger will run before the route runs.
var route;
if (!(Meteor.loggingIn() || Meteor.userId())) { // Checks if the user is logging in or if the user is logged in already
route = FlowRouter.current();
if (route.route.name !== 'login') {
Session.set('redirectAfterLogin', route.path); // we don’t use the route name, but the path. this way you can redirect the user while keeping the state in the url.
console.log("this is the route path", route.path); // we save the route that the user wanted to go in Session.set('redirectAfterLogin')
}
FlowRouter.go('login');
}
}]
});
loggedIn.route('/admin', {
name: 'mainLayout',
action: function() {
BlazeLayout.render( 'mainLayout' );
}
});
loggedIn.route('/pageOne', {
action: function() {
BlazeLayout.render( 'mainLayout', {content: 'pageOne'});
},
name: 'pageOne'
});
loggedIn.route('/pageTwo', {
action: function() {
BlazeLayout.render( 'mainLayout', {content: 'pageTwo'});
},
name: 'pageTwo'
});
loggedIn.route('/logout', {
name: 'logout',
action: function () {
Meteor.logout(function () {
FlowRouter.go(FlowRouter.path('login'));
});
}
});
以下是exposed.js
中我公开的路线:
var exposed = FlowRouter.group ({
name: exposed
});
exposed.route('/', {
action: function() {
BlazeLayout.render( 'landing' );
},
name: 'landing'
});
exposed.route('/login', {
action: function() {
BlazeLayout.render( 'login' );
},
name: 'login'
});
exposed.route('/register', {
action: function() {
BlazeLayout.render( 'register' );
},
name: 'register'
});
最后但并非最不重要的是,在我的启动文件夹中,我有一个名为default.js
的文件:
FlowRouter.wait();
//if the roles subscription is ready, start routing
//there are specific cases that this reruns, so we also check
// that FlowRouter hasn't initalized already
Tracker.autorun(function() {
if (Roles.subscription.ready() && !FlowRouter._initialized) {
return FlowRouter.initialize();
}
});
// Run this when the meteor app is started
Meteor.startup(function () {
});
有没有其他人有这个问题或类似的东西?任何帮助将不胜感激。为了澄清,已安装Sessions,我使用的是最新版本1.1.6
。感谢
答案 0 :(得分:0)
有两个不同的问题:
会话仅在客户端上定义,并且不使用在服务器上运行重定向代码(在服务器上,挂钩通常用于不同目的,例如分析)。这导致您在控制台中看到的错误。
您将undefined
传递给FlowRouter.go()
,因为您严格将返回的会话值与null
进行比较。 null
和undefined
不一样。因此,如果未设置'redirectAfterLogin'
会话变量,您将获得undefined
并将其传递给FlowRouter。
答案 1 :(得分:0)
有类似的问题。通过以下方式解决了它: 在AccountsTemplates.configure中,我设置了homeRoutePath:' / loggedin'路径。这样,登录后,用户将被重定向到'/ loggedin'路线。 然后在'/ loggedin'路径的FlowRouter中:
loggedInGroup.route('/loggedin', {
name: 'loggedin',
triggersEnter: [function(context, redirect) {
var redirectAfterLoginPath = Session.get('redirectAfterLogin');
if(redirectAfterLoginPath){
redirect(redirectAfterLoginPath);
Session.set('redirectAfterLogin', undefined);
} else {
redirect('home');
};
}]
});
loggedInGroup = FlowRouter.group({
triggersEnter: [
function(context, redirect, stop) {
if (Meteor.loggingIn() || Meteor.userId()) {
route = FlowRouter.current();
} else {
if(context.path != '/sign-in'){
Session.set('redirectAfterLogin', context.path);
};
redirect('/sign-in');
}
}
]
});
这样,'/ loggedin'路由就是处理无法访问Accounts.onLogin中的Session的问题,并将用户重定向到正确的路径。