javascript - 如何使用 Passport 和 nodeJS 在 angularJS 前端显示用户信息

标签 javascript angularjs node.js passport.js

我正在尝试使用 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/

相关文章:

node.js - 如何使用 NodeJS 通过套接字发出 HTTP GET 请求?

javascript从URL创建文件

javascript - JavaScript 中的WITH 语句有什么问题?

angularjs - 模块没有导出成员,导出接口(interface)

javascript - AngularJS - $scope 变量未在工厂中更新

node.js - 找不到 NodeJS nodegui 模块

javascript - 我如何让我的 JavaScript 函数返回图像

javascript - 在 js/node.js 中检查一行是否只包含空格和\n

javascript - ng 风格间歇性工作

javascript - NodeJS 等待异步函数完成 foreach