簡體   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