Saul Martinez
Saul Martinez

Reputation: 169

How to return a response from a pre config? (HapiJS)

I have the path:

{
    method: 'GET',
    path: '/filesystem',
    config: {pre:[validateJWT]},
    handler: readDir
}

And two functions, the pre:

const validateJWT = async (req, res) => {
     . . .
    //returns true or false
}

And the handler:

const readDir = async(req, res) => {
  return res.response('Handler response').code(200);
};

How can I link the return value of the validateJWT function to use it in the readDir handler function?. Each function and the route are in different files.

Thanks.

Upvotes: 0

Views: 293

Answers (1)

Joe
Joe

Reputation: 64

To use the validateJWT function, you must assign a name to the function like this:

const validateJWT = () {
  return {
      method: async (req, _res) => {
        . . .
        //returns true or false
      },
      assign: 'validateJWT',
    };
}

And after doing this to retrieve the value of the function in readDir:

const readDir = async(req, res) => {
  // const {validateJWT} = req.pre;
  return res.response('Handler response').code(200);
};

DOC: https://hapi.dev/api?v=20.2.0#-routeoptionspre

Upvotes: 1

Related Questions