2013-09-23 21 views

回答

4

由於護照進程的驗證,客戶端在與服務器進行的所有通信中都會發送用戶會話及其請求。如果你希望你的手把模板上的用戶的存在條件的,我的做法是建立在服務器上的以下要求處理:

app.get("/user", function (req,res) { 
    if (req.isAuthenticated()) { 
     res.json({ 
      authenticated: true, 
      user: req.user 
     }) 
    } else { 
     res.json({ 
      authenticated: false, 
      user: null 
     }) 
    } 
}) 

而在我的灰燼路線我做了以下要求:

App.ApplicationRoute = Ember.Route.extend({ 
    model: function() { 
     return $.get("/user").then(function (response) { 
      return {user: response.user}; 
     }) 
    } 
}); 

所以,在我的手把我的模板可以做到以下幾點:

{{#if user}} 
    <p>Hello, {{user.name}}</p> 
{{else}} 
    <p>You must authenticate</p> 
{{/if}} 
+0

謝謝主席先生。正在試圖做這樣的事情,但沒有得到完全正確的結果,並且正在讓我的頭撞在牆上。 – Jaime