none
[Node.js][Mobile Apps] Easy API get current user id RRS feed

  • Question

  • Hi all - been banging my head on my desk most of the day regarding this. I have an Easy API end point. I've set all methods to authenticated:

    {
      "get": {
        "access": "authenticated"
      },
      "post": {
        "access": "authenticated"
      },
      "put": {
        "access": "disabled"
      },
      "patch": {
        "access": "disabled"
      },
      "delete": {
        "access": "disabled"
      }
    }

    Based on this I should be able to access my user id like:

    "get": function (req, res, next) {
            console.log("req: "+req.azureMobile.user.id);
            ...
        }

    but the id is undefined in the response:

    u { id: undefined,
      token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0NjI5NDUzNDksImV4cCI6MTQ2MzAzMTc0OSwiYXVkIjoidXJuOm1pY3Jvc29mdDp3aW5kb3dzLWF6dXJlOnp1bW8iLCJpc3MiOiJ1cm46bWljcm9zb2Z0OndpbmRvd3MtYXp1cmU6enVtbyJ9.gat8BX_IEza7hmn9HcmhiuyhRXztPhoBJ1-S12HQP28',
      claims:
       { iat: 1462945349,
         exp: 1463031749,
         aud: 'urn:microsoft:windows-azure:zumo',
         iss: 'urn:microsoft:windows-azure:zumo' },
      getIdentity: [Function: getIdentity] }

    and here is my simple code:

    var azureMobileApps = require('azure-mobile-apps');
    
    module.exports = {
    	get: function (req, res, next) {
            console.log("u", req.azureMobile.user);
            return;
    	}
    };

    And this is how I sign the user when I log them on:

    if(users.length === 1 && validatePassword(req.body.password, users[0].password))
         res.json(createResponse(sign, users[0]));
    
    ...
    
    function createResponse(sign, user) {
        return {
            // this JWT token must be applied on the Mobile Apps client using the appropriate client APIs
            token: sign({
                // sub is the only required property. this becomes context.user.id
                // you can add other claims here. they become available as context.user.claims
                sub: user.username
            })
        };
    }

    This is based on this sample. Any ideas what I'm doing wrong? I want to grab the userId so I can send it to a 3rd party API.

    Cheers,

    Kolchy



    Wednesday, May 11, 2016 5:48 AM

Answers

  • OK, if you get into this scenario, the issue is the name of the 'username' property on the user object. Mine is actually called 'email' :) After reading through the code here, it became clear! So my function changed to:

    function createResponse(sign, user) {
        return {
            // this JWT token must be applied on the Mobile Apps client using the appropriate client APIs
            token: sign({
                // sub is the only required property. this becomes context.user.id
                // you can add other claims here. they become available as context.user.claims
                sub: user.email
            })
        };
    }

    Cheers,

    Kolchy

    • Marked as answer by Kolchy Wednesday, May 11, 2016 7:59 AM
    Wednesday, May 11, 2016 7:59 AM