I have a problem with the management of the jwt token from Kuzzle javascript SDK's auth controller. I'm a complete beginner in JS or Kuzzle, sorry for any bad assumptions.
I use a simple admin account on my Express server with the rights to register and update users, or create indices/collections. Here is the setup so far.
const {Kuzzle, WebSocket} = require('kuzzle-sdk')
const config = require('../config/config')
const kuzzle = new Kuzzle(new WebSocket('localhost'))
// Add a listener to detect connection problems
kuzzle.on('networkError', (error) => console.error("Network Error: " + error))
// start admin session
if (!kuzzle.connected) {
kuzzle.connect()
.then(() => kuzzle.auth.login('local', {username: config.kuzzle, password: config.kuzzle.password}))
.catch((error) => {
console.error(error)
kuzzle.disconnect()
})
}
At token expiration, I get asecurity.token.invalid
error when I would expect from the security error codes a security.token.expired
. Then, all following requests fail with a security.rights.forbidden
id.
// token not expired
await kuzzle.auth.getCurrentUser()
.then((user) => console.log(user.content)) // { profileIds: [ 'admin' ], _kuzzle_info: {...} }
.catch((err) => console.error(err)) // X
// token just expired
await kuzzle.auth.getCurrentUser()
.then((user) => console.log(user.content)) // X
.catch((err) => console.error(err)) // [KuzzleError: Invalid token.] {status: 401, id: 'security.token.invalid', code: 117506049}
// following user
await kuzzle.auth.getCurrentUser()
.then((user) => console.log(user.content)) // { profileIds: [ 'anonymous' ], name: 'Anonymous' }
.catch((err) => console.error(err)) // X
// any following request (admin does have those permissions)
await kuzzle.index.exists("existing")
.catch((err) => console.error(err)) // [KuzzleError: Insufficient permissions to execute the action "index:exists".] {status: 403, id: 'security.rights.forbidden', code: 117637122}
Three questions:
Is "invalid" the expected behaviour? I think I successfully login, and Kuzzle does not throw before the expiration date, so I would expect an expired and not invalid token. I tried with 5 seconds or 2 hours tokens with same results.
Why is Kuzzle logging me out for an invalid token? Not that I am complaining, just curious.
And how do I recover from an expired token in Kuzzle? Is there a good practice to do so? Or better yet, can I automatically refresh the token for as long as the server lives, before being logged out?
My quick and dirty solution for now is to use an error handler Express middleware to intercept the error if it is an instance of kuzzle-sdk/src/KuzzleError.js, check for any of these two error ids, and re-login. I then leave my client side with the task of retrying the query, which is a bit of a pain to handle.
I am using kuzzle-sdk v7.1.4.
Many thanks!