Then, in src/index.js, import the sign method from theĀ jsonwebtoken package and pass it down to the engine through the handlers. Then, update the engine function to return a signed JWT when a user is found with those credentials. Note that we are using the private key, stored at process.env.PRIVATE_KEY, to sign the token:
function loginUser(req, db, validator, ValidationError, sign) {
...
return client.search( ... )
.then((res) => {
if (res.hits.total > 0) {
const payload = { sub: res.hits.hits[0]._id };
const options = { algorithm: 'RS512' };
const token = sign(payload, process.env.PRIVATE_KEY, options);
return token;
}
return Promise.reject(new Error('Not Found'));
});
}
Now, run our tests again and they should all pass.