简体   繁体   English

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

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

Im trying to replicate this local authentication with the passport tutorial . 我试图用护照教程复制此本地身份验证。

but with an angularJS frontend instead of EJS, until now everything works, but I have a problem when I want to show the user data in the profile page (after a successful login) 但是使用angularJS前端而不是EJS,直到现在一切正常,但是当我想在个人资料页面中显示用户数据(成功登录后)时遇到问题

I'm talking about this piece of code 我在说这段代码

 // =====================================
    // 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
        });
    });

I understand that user is the data that I need, but in the example with EJS is used like this: 我知道用户就是我需要的数据,但是在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>

Can I access this same data with AngularJS? 我可以使用AngularJS访问相同的数据吗? I'm a bit new in this and I don't understand how 我在这方面有点新,我不知道如何

Yes, you can get the data in Angular as well. 是的,您也可以在Angular中获取数据。 Hope you have bootstraped your angular application. 希望您已经引导了角度应用程序。 Use this: 用这个:

Local 本地

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

        </div>
    </div>

This 'user' will use the scope variable which you will define in your controller 该“用户”将使用您将在控制器中定义的范围变量

Controller Function 控制器功能

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