2016-08-12 54 views
0

當用戶創建他們給出了兩個用戶選擇之間選擇一個配置文件,這意味着他們的用戶配置文件對象看起來像:路由用戶基於配置文件參數登錄後

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

我想什麼要做的是設置一個重定向,其中,一旦用戶登錄,它將檢查他們是什麼類型的用戶並將他們重定向到一個頁面。

如果USERTYPE =用戶A,他們會去爲localhost:3000 /用戶-A
如果USERTYPE =用戶B,他們會去爲localhost:3000 /用戶-B

我目前使用鐵路由器爲我的路線和賬戶,密碼賬戶

我試圖在客戶端以下,但它打印出空

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

編輯1

試圖從下面的代碼,以及在服務器端做到這一點,但我正在逐漸鐵路由器錯誤:

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 
+0

我不熟悉你使用的框架,但在客戶端,你可以設置'locatio n「對象,而在服務器端,您可以發送帶有302 HTTP響應(臨時重定向)的Location標頭 – rvighne

+0

如果您得到空值,則表示沒有用戶登錄。[Meter.userId()]( https://docs.meteor.com/api/accounts.html#Meteor-userId) – Zesky

+0

@澤斯基權利,多數民衆贊成我想弄清楚。我想要做的是,一旦用戶登錄,找出他們的用戶類型並將其重定向到特定頁面。 –

回答

1

您可以AccountsTemplates.configure()

使用 onSubmitHook
//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'); 
      } 
     } 
    } 
}