Search code examples
node.jsloopbackjs

Get custom response from loopback login API


I'm currently using Loopback3 to experiment and build a test application.

I've extended the default user model. When I use the login API on my custom user model I get a response like this -

{
  "id": "typIikmedhMiAoLketgdT4sOOTlSPGwZOFmol6wR6jF9RiZA4fAoKLYStKwyhbDA",
  "ttl": 1209600,
  "created": "2018-06-01T15:22:05.812Z",
  "userId": "5b0f8e00d831480db4670cbb"
}

But, I would also like to return other fields from my custom user model such as username along with the response from the login API. Is this something I can modify or do I have to resort to using userModel.find() to get other details that I would like the API to return?

What I would ideally like

{
  "id": "typIikmedhMiAoLketgdT4sOOTlSPGwZOFmol6wR6jF9RiZA4fAoKLYStKwyhbDA",
  "ttl": 1209600,
  "created": "2018-06-01T15:22:05.812Z",
  "userId": "5b0f8e00d831480db4670cbb",
  "username":"testAccount",
  "forcePasswordReset":true
}

I haven't attached any of my code since the question is related to just the default options on loopback framework and not specific to my code. If my code will help I will be happy to provide a sample. Thanks for your time.


Solution

  • It is possible. Add include=user query param to the URL, like this:

    POST /users/login?include=user
    

    You should see:

    {
       "id":"TOKEN",
       "ttl":1209600,
       "created":"2018-06-04T07:28:15.831Z",
       "userId":1,
       "user":{
          "realm":null,
          "username":"Test User",
          "email":"test@test.com",
          "emailVerified":true,
          "id":1
       }
    }