2017-03-02 48 views
1

我有一個愚蠢的問題與feathersjs auth鉤子(或其他)。這是我的代碼與評論:Feathersjs管理員角色(或與auth檢查羽毛中間件)

app.get('/admin', function (req, res) { 

    // i'd like to check here if (req.connection.isAdmin) then render page 

    res.render('admin/admin.html', { 
     title: 'admin' 
    }) 
    }); 

我找不到在哪裏我可以實現用戶身份驗證鉤爲chek用戶管理角色。我怎樣才能做到這一點?

+0

一些谷歌搜索後,我發現這個相同的問題https://github.com/feathersjs/feathers/issues/357但沒有解決方法和頁面上的鏈接被打破。仍然誤解如何在中間件上實現鉤子,或者只是使用自定義認證邏輯....如果我可以訪問req,請使用連接憑證res – Victor

回答

2

您應該能夠使用我張貼在這個其他問題例如:Feathers Js Restrict Access To Page on Server Side

在你的情況,你需要做一些邏輯來看看用戶是管理員。默認情況下,羽毛爲您提供的JWT令牌將僅包含userId。您可以檢索用戶記錄以檢查用戶是否是管理員。

這裏是你會怎樣把jwt.verify塊中的例子:

jwt.verify(token, secret, function(err, decoded) { 
    if (err) { 
    return res.status(401).send('You are not authorized to view that page.'); 
    } 
    app.service('users') 
    .get(decoded.id) // Or _id if you're using MongoDB 
    .then(user => { 
     if (user.isAdmin) { 
     res.render('admin/admin.html', { 
      title: 'admin' 
     }) 
     } else { 
     return res.status(401).send('You are not authorized to view that page.'); 
     } 
    }) 
}); 

它將在feathers-authentication下一個版本值添加到JWT在服務器上是可能的,所以,對於管理員,您可以將isAdmin屬性添加到JWT有效負載。一旦預發佈版本發佈,這將是相當微不足道的。在此之前,上述可能是最好的選擇。

+1

非常感謝Marshall!我接受你的答案。 – Victor