Udit Kumawat
Udit Kumawat

Reputation: 684

validate function is not working in Hapi.js

I am new to hapi.js framework , I have made different files for routes and plugins . I have registered the hapi-auth-jwt plugin in a file named hapi-auth.js

Hapi-auth-jwt has functionality of validating the token with some function.

I have added my function named "validate" to my code ... this function is not working...please help me ...

'use strict';
const User = require('../Models/user.js');
const config = require('../Config');


let validate = function(request,decodedToken,callback){

var user = User.findOne({},function(err,user){

        if(err)
            throw err;
        return user;    
    });

let error,credentials = user.username || {};

  if(!credentials)
     return callback(null,false,credentials);
  return callback(null,true,credentials);
};

exports.register = function(server, options, next){


server.register(require('hapi-auth-jwt'),(err)=>{

server.auth.strategy('token','jwt',{

    key : config.jwtSecret.key,
    validateFunc : validate,
    verifyOptions:{ algorithms:['HS256'] }
});

});

 next();
};

exports.register.attributes = {
  name: 'hapi-auth-plugin'
 };

Upvotes: 0

Views: 1464

Answers (1)

dev0x10
dev0x10

Reputation: 105

@uditkurmawat

It happens because the request is rejected before entering the validate function. The request must be ends up here https://github.com/dwyl/hapi-auth-jwt2/blob/master/lib/index.js#L32

You need to assign authorization token before making the request. Check this: https://github.com/dwyl/learn-json-web-tokens

Upvotes: 2

Related Questions