简体   繁体   中英

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)

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:

<!-- 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? I'm a bit new in this and I don't understand how

Yes, you can get the data in Angular as well. 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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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