我正在尝试使用 the passport tutorial 复制此本地身份验证.
但是使用 angularJS 前端而不是 EJS,到目前为止一切正常,但是当我想在个人资料页面中显示用户数据时(成功登录后),我遇到了问题
我说的是这段代码
// =====================================
// PROFILE SECTION =====================
// =====================================
// we will want this protected so you have to be logged in to visit
// we will use route middleware to verify this (the isLoggedIn function)
app.get('/profile', isLoggedIn, function(req, res) {
res.render('profile.ejs', {
user : req.user // get the user out of session and pass to template
});
});
我知道 user 是我需要的数据,但在 EJS 的示例中是这样使用的:
<!-- LOCAL INFORMATION -->
<div class="col-sm-6">
<div class="well">
<h3><span class="fa fa-user"></span> Local</h3>
<p>
<strong>id</strong>: <%= user._id %><br>
<strong>email</strong>: <%= user.local.email %><br>
<strong>password</strong>: <%= user.local.password %>
</p>
</div>
</div>
我可以使用 AngularJS 访问相同的数据吗?我对此有点陌生,我不明白如何做到
最佳答案
是的,您也可以在 Angular 中获取数据。希望您已经启动了您的 Angular 应用程序。使用这个:
本地
<p>
<strong>id</strong>: {{user._id}}<br>
<strong>email</strong>: {{user.local.email}}<br>
<strong>password</strong>: {{user.local.password}}
</p>
</div>
</div>
此“用户”将使用您将在 Controller 中定义的范围变量
Controller 功能
function test($scope) {
$scope.user = user; //Need to get this user from the server call (by $http or a service)
}
关于javascript - 如何使用 Passport 和 nodeJS 在 angularJS 前端显示用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24957259/