Implementing a Retrieve Salt engine

Create a new Retrieve Salt engine at src/engines/auth/salt/retrieve/index.js. In it, we need to use the Elasticsearch client's search method to find the user's document by email, extract the digest from the document, and then extract the salt from the digest:

const NO_RESULTS_ERROR_MESSAGE = 'no-results';function retrieveSalt(req, db, getSalt) {  if (!req.query.email) {    return Promise.reject(new Error('Email not specified'));  }  return db.search({    index: process.env.ELASTICSEARCH_INDEX,    type: 'user',    body: {      query: {        match: {          email: req.query.email,        },      },    },    _sourceInclude: 'digest',  }).then((res) => {    const user = res.hits.hits[0];    return user      ? user._source.digest : Promise.reject(new Error(NO_RESULTS_ERROR_MESSAGE)); ...

Get Building Enterprise JavaScript Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.