1

Following this tutorial, I am trying to learn to be able to create a simple back-end authentication with node.js, passport and angular.

I would like to get logged in user name with angular but how can I do that?

I am not sure if my question makes sense..so If it is not clear please tell me, I would like to explain it.

I have created a service to get all users..

service.factory('Users', ['$http', function($http){
        return {
            get: function(){
                return $http.get('/api/users');
            }
        }

and on node

function getUsers(res){
    Users.find(function(err, users) {
        if (err)
                res.send(err)
            res.json(users);
    });
};

So I can register users on database and I can get them. What I need to compare the users on database with the logged in user so that logged in user can post or read..

Thanks in advance!

1
  • can you show some code
    – harishr
    Jan 1, 2015 at 14:30

2 Answers 2

0

It can be done trough an API that you must build it first if not already exist. Something like /user/data endpoint that returns a JSON object about your user credential. This approach is more suitable with SPA (Single Page Application) architecture.

Also it can be done by put a global object in your document that can be accessed by Angular later. Something like (if use EJS)

<script>
  <% var globalUser = req.user %>
</script>

So Angular able to access globalUser variable.

2
  • I have indeed created the /user/data api but is it possible to check logged user without using EJS.?
    – Mar
    Jan 1, 2015 at 14:41
  • Above I gave you option. Using API or template (EJS or whatever). If you using an API, so just call your API like any other AJAX then store your user credential in $rootScope maybe or something depends on your application's architecture. Jan 1, 2015 at 20:35
0

You should read the tutorial more carefully.

According to it, you should first - create a middleware callback that would handle the authentication:

function isLoggedIn(req, res, next) {

    // if user is authenticated in the session, carry on 
    if (req.isAuthenticated())
        return next();

    // if they aren't redirect them to the home page
    res.redirect('/');
}

Explanation of the middleware:

isLoggedIn: Using route middleware, we can protect the profile section route. A user has to be logged in to access that route. Using the isLoggedIn function, we will kick a user back to the home page if they try to access http://localhost:8080/profile and they are not logged in.

And lastly - use it in the route which you'd like to secure:

app.get('/profile', isLoggedIn, function(req, res) {
    res.render('profile.ejs', {
        user : req.user // get the user out of session and pass to template
    });
});
1
  • Thanks for the answer but still; how do I pass logged in user name or email to angular controller?
    – Mar
    Jan 3, 2015 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.