September 2018
Intermediate to advanced
764 pages
20h 6m
English
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. ...
Read now
Unlock full access