Search code examples
javascriptreactjskuzzle

Kuzzle getUser by JWT Token generated early


I have Integrated kuzzle in ReactJS app using Kuzzle-SDK

Everything works Fine first. I Login using kuzzle.auth.login('local', credentials) and save the JWT Token in localStorage and kuzzle.auth.getCurrentUser() return

{
  "profileIds": [
    "admin"
  ],
  "_kuzzle_info": {
    "author": "usamaashraf82",
    "createdAt": 1573960408796,
    "updatedAt": null,
    "updater": null
  }
}

After refreshing the Browser

I try to kuzzle.auth.checkToken();and it returns {valid: true, expiresAt: 1540824822897 } and i try to kuzzle.auth.getCurrentUser() but then its return

{
  "profileIds": [
    "anonymous"
  ],
  "name": "Anonymous"
}

I want to Ask what I am doing Wrong or How I can solve it


Solution

  • The kuzzle.auth.checkToken method is only meant to check a token validity.

    If you want to use a previously acquired token, you can set the kuzzle.jwt property with the corresponding token. (See Kuzzle properties documentation).

    const token = await kuzzle.auth.login('local', credentials);
    // [...]
    kuzzle.jwt = token;
    await kuzzle.auth.getCurrentUser(); // now loggued with the user corresponding to the token
    

    Please consider that storing authentication token in the browser localstorage is usually a bad idea because almost every script included in you webpage will be able to read them and connect without your user knowledge.