0

I'm building a web app with the MEAN Stack. What I am trying to achieve is that when the user logs in his user information get fetched by Angular from my REST API. I set up the API route http://localhost:3000/api/user/profile which should respond with json including the user object.

router.get('/user/profile', function(req, res, next){
    //console.log(req);
    if(req.user === undefined){
        res.json({
            success: false,
            msg: 'Unautorized'
        });
    } else {
        res.json({
            success: true,
            user: {
                id: req.user.steam.id,
                name: req.user.steam.name,
                avatar: req.user.steam.avatar,
                avatarmedium: req.user.steam.avatarmedium,
                avatarfull: req.user.steam.avatarfull
            }
        });
    }
});

When the user logs in Angular start a GET-Request:

ngOnInit() {
    this.authService.getProfile().subscribe(profile => {
        this.user = profile.user;
        console.log(profile);
    },
    err => {
        console.log(err);
        return false;
    });

}

getProfile():

getProfile(){
    return this.http.get('http://localhost:3000/api/user/profile')
    .map(res => res.json());
}

When I load up my site, log in, and go to the profile page the returned object contains success: false and the message 'Unauthorized' instead of the user object. Why is this happening?

wprzechodzen
  • 619
  • 4
  • 13
User3434343443
  • 170
  • 3
  • 17
  • you are not sending any params in the this.http.get call. And the way to accessing the params is also wrong. Refer to this question for the proper format for sending params and accesing get params. https://stackoverflow.com/questions/17007997/how-to-access-the-get-parameters-after-in-express – Shyam Babu Sep 04 '17 at 16:57
  • I don't think you understood my question properly, sorry if I didn't say it well. The get request is working completely, I just get the response for the case that req.user is undefined even though I am logged in. I console.logged the req when accessing the route /api/user/profile, and the req.user object somehow doesn't exist when making the call from angular. So somehow the cookie doesn't get set when logging in – User3434343443 Sep 04 '17 at 17:34
  • Usually the user info is passed along to the server via the auth token in the authorization headers. After the user authenticates you should receive a token back from the auth provider. Most likely this token is a jwt, if you pass that back to the server via a header you can then decode the jwt token and get the information on it. If you do have a toek use the debugger on https://jwt.io/ to see what all information is in it. If you have this in github it would be easier to troubleshoot :) – zmanc Sep 04 '17 at 18:04

1 Answers1

0

I completely redesigned my approach. I implemented json web token which now sends a token (containing all user data) to the user through a url parameter once he signs in.

User3434343443
  • 170
  • 3
  • 17