Stormpath用户信息
问题描述:
好的,这是我的路由器代码,我试图访问用户帐户信息,当他们在主页上。到目前为止没有运气......我在这里做错了什么?Stormpath用户信息
// Render the dashboard page.
router.get('/home', function (req, res) {
if (!req.user || req.user.status !== 'ENABLED') {
return res.redirect('/login');
}
console.log('User:', req.user.email, 'just accessed the /home page!');
//user: req.user
res.render('home', {
title: 'Home',
user:req.user.username
}
);
});
答
你的例子看起来是正确的,但我会在渲染调用中分配整个用户对象(现在你只是传递用户名)。
所以,如果你放在这routes/index.js
// Render the 'home' page if logged in and account is not disabled
router.get('/home', function (req, res) {
if (!req.user || req.user.status !== 'ENABLED') {
return res.redirect('/login');
}
res.render('home', {
title: 'Home',
user: req.user
});
});
然后在views/home.jade
的观点是这样的:
html
head
title=title
body
p Hello, #{user.username}
答
问题(看来)是你没有loginRequired中间件设置在你的路线上。你应该修改你的函数签名:
router.get('/home', stormpath.loginRequired, function(req, res) {
...
});
这将迫使用户正确地评价=)
您好。您能告诉我们您正在使用哪个Stormpath存储库,例如[stormpath-express-sample](https://github.com/stormpath/stormpath-express-sample)或[stormpath-express](https:// github .com/stormpath/stormpath-express)?谢谢! – robertjd 2014-09-24 17:52:43
嗨,它的stormpath表示样本。 – user61026 2014-09-24 20:29:07