根据配置文件参数登录后路由用户

时间:2016-08-12 02:11:41

标签: javascript meteor

当用户创建个人资料时,他们可以在两个用户选择之间进行选择,这意味着他们的用户个人资料对象将类似于:

{
    name: "Jane Doe",
    userType: "user-A"
}

我想要做的是设置重定向,一旦用户登录,它将检查他们是什么类型的用户并将他们重定向到页面。

如果userType = user-A,他们将转到localhost:3000 / user-A
如果userType = user-B,他们将转到localhost:3000 / user-B

我目前正在使用Iron Router作为我的路线和账户密码

我在客户端尝试了以下内容,但它打印出null

Template.login.events({
    'submit #at-pwd-form': function() {
        console.log(Meteor.userId());
    }
});

修改1

尝试使用以下代码从服务器端执行此操作,但我收到Iron Router错误:

Accounts.onLogin(function () {
  var pathToProfile = "/" + Meteor.user().profile.userType;
  console.log(pathToProfile); // Proof that variable is being saved properly
  Router.go(pathToProfile);
});

错误:

I20160811-22:58:28.560(-4)? Exception in onLogin callback: TypeError: Object function router(req, res, next) {
I20160811-22:58:28.560(-4)?     //XXX this assumes no other routers on the parent stack which we should probably fix
I20160811-22:58:28.560(-4)?     router.dispatch(req.url, {
I20160811-22:58:28.560(-4)?       request: req,
I20160811-22:58:28.561(-4)?       response: res
I20160811-22:58:28.561(-4)?     }, next);
I20160811-22:58:28.561(-4)?   } has no method 'go'
I20160811-22:58:28.561(-4)?     at server/main.js:13:10
I20160811-22:58:28.561(-4)?     at runAndHandleExceptions (packages/callback-hook/hook.js:133:1)
I20160811-22:58:28.561(-4)?     at packages/callback-hook/hook.js:140:1
I20160811-22:58:28.562(-4)?     at packages/accounts-base/accounts_server.js:167:5
I20160811-22:58:28.562(-4)?     at [object Object]._.extend.each (packages/callback-hook/hook.js:109:1)
I20160811-22:58:28.562(-4)?     at AccountsServer.Ap._successfulLogin (packages/accounts-base/accounts_server.js:166:21)
I20160811-22:58:28.562(-4)?     at AccountsServer.Ap._attemptLogin (packages/accounts-base/accounts_server.js:355:10)
I20160811-22:58:28.562(-4)?     at [object Object].methods.login (packages/accounts-base/accounts_server.js:532:21)
I20160811-22:58:28.562(-4)?     at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1704:12)
I20160811-22:58:28.563(-4)?     at packages/ddp-server/livedata_server.js:711:19

1 个答案:

答案 0 :(得分:1)

您可以在onSubmitHook

中使用AccountsTemplates.configure()
//code goes to lib folder, shared code between client & server
onSubmitHook: function onSubmitHook(error, state) {
    if (!error) {
        if (state === 'signIn') {
            var user = Meteor.user();
            if (user.userType == "user-A") {
                Router.go('/user-A');
            } else {
                Router.go('/user-B');
            }
        }
    }
}