繁体   English   中英

如何使用Passport和NodeJS在angularJS前端中显示用户信息

[英]How to show user information in angularJS frontend with Passport and nodeJS

我试图用护照教程复制此本地身份验证。

但是使用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
        });
    });

我知道用户就是我需要的数据,但是在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中获取数据。 希望您已经引导了角度应用程序。 用这个:

本地

                <p>
                    <strong>id</strong>: {{user._id}}<br>
                    <strong>email</strong>: {{user.local.email}}<br>
                    <strong>password</strong>: {{user.local.password}}
                </p>

        </div>
    </div>

该“用户”将使用您将在控制器中定义的范围变量

控制器功能

function test($scope) {
    $scope.user = user; //Need to get this user from the server call (by $http or a service)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM