Search code examples
angularjsnode.jsmongoosepassport.jsng-view

session username not showing inside angularjs ngView template but does outside of ngView


So I am able to show the username via mongoose passport-local within my successful redirect:

app.get('/profile', isLoggedIn, function (req, res) {
    res.render('profile.html', {
        user: req.user
    });
    console.log(req.user.local.username) //correct username for the session
});

...and then use it to display within profile.html:

<p><%= user.local.username %></p>

...yet when trying to display the same within my ng-view:

<div ng-view ng-class="slide" class="slider"></div>

...via my route example.html template:

<p>welcome <%= user.local.username %></p>

..it is displayed as seen: not the correct username...

Is it not possible to include this information within an ngView? Is there a solution to include this info within my template? I have attempted to setup a factory with $http:

angular.module('userService', [])

.factory('Users', function($http) {
    return {
        get : function() {
            return $http.get('/profile');
        }
    }
});

...but it was returning the entire html, so not a correct solution.

Any guidance is as always welcomed and appreciated, so thanks in advance!

Edit in response: Getting the value and routing it isnt really the issue. The issue is just getting it to display correctly within the ngView.

heres my updated code:

Rules.get()
        .success(function (data) {
            $scope.rules = data;
            console.log('Rules.get()');
            console.log(data);

            Users.get()
                .success(function (username) {
                    Users.username = username;
                    console.log('Users.get()')
                    console.log(username)
                });
        });

...and...

angular.module('userService', [])

.factory('Users', function ($http) {
    return {
        get: function () {
            console.log('userService')
            return $http.get('/profile/user');
        }
    }
});

...which returns from:

app.get('/profile/user', function (req, res) {
    console.log('profile/user')
    console.log(req.user.local.username)
    res.json(req.user.local.username);

});

This gives me the same and correct username, but alas what do I call it to get it to display in the ngView? If placed outside of the actual ngView div it displays fine.

From the above {{username}} {{rules.username}} = nothing. What should be the name of the service param within the ngView template (which I am assuming is an entirely new $scope)?


Solution

  • So here was my solution for anyone else that may be searching:

    users.js:

    angular.module('userService', [])
    
    .factory('Users', function ($http) {
        return {
            get: function () {
                console.log('userService')
                return $http.get('/profile/user');
            }
        }
    });
    

    route.js

    $scope.username = {};    
    .controller('cntrl', function ($scope, Users) {
            getCurrentUser(Users, $scope);
        })
    
            function getCurrentUser(Users, $scope) {
            Users.get()
                .success(function (username) {
                    $scope.username = username.user;
                });
        }
    

    service.js:

    app.get('/profile/user', function (req, res) {
        console.log('profile/user')
        console.log(req.user.local.username)
        res.jsonp({ user:req.user.local.username});
    
    });
    

    then within html (within any ngView template):

    <span class="username">{{username}}</span>
    

    the key being:

    $scope.username = {};