2016-08-19 105 views
0

我试图使用流量路由器实现自定义登录,授权和角色到我的Meteor应用程序。我正在使用Meteor的最新版本。我也更新了所有软件包。登录时出现错误。会话未定义 - 流星1.4和流量路由器

注册和创建帐户完美地工作。我的凭证已保存并且用户已创建。

我在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 

我已删除并重新加入会话包好几次。这里是我目前使用的所有软件包的列表:

[email protected] 
twbs:[email protected] 
fortawesome:fontawesome 
[email protected] 
okgrow:router-autoscroll 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
kadira:flow-router 
meteorhacks:fast-render 
kadira:blaze-layout 
zimme:active-route 
arillo:flow-router-helpers 
[email protected] 
alanning:roles 
[email protected] 
standard-minifier-css 
standard-minifier-js 
meteortoys:[email protected] 
session 

我有3个文件流量路由器为我创建了这个不同的路由。

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

你解决了这个问题吗?如果不是,现状是什么? – MasterAM

回答

0

有2个不同的问题:

  1. 会话仅在客户端上所定义,并且没有使用在服务器上运行的重定向代码的(在服务器上的钩子通常用于不同目的,如分析)。这会导致您在控制台中看到的错误。

  2. 您将undefined更改为FlowRouter.go(),因为您严格比较返回的会话值与nullnullundefined是不一样的。因此,如果未设置'redirectAfterLogin'会话变量,您将获得undefined并将其传递给FlowRouter。

0

有类似的问题。通过以下方式解决它: 在AccountsTemplates.configure中,我设置了homeRoutePath:'/ loggedin'路径。这样,登录后,用户将被重定向到'/ loggedin'路线。 然后在FlowRouter为“/的loggedIn”路径:

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的问题,只是方式它将用户重定向到正确的路线。