我是hapi.js框架的新手,我为路由和插件制作了不同的文件。我在一个名为hapi-auth.js的文件中注册了hapi-auth-jwt插件
Hapi-auth-jwt具有通过某种功能验证令牌的功能。
我已将名为“validate”的函数添加到我的代码中...此函数无效...请帮助我...
'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'
};
答案 0 :(得分:1)
@uditkurmawat
发生这种情况是因为在进入验证功能之前请求被拒绝。请求必须在此处结束 https://github.com/dwyl/hapi-auth-jwt2/blob/master/lib/index.js#L32
您需要在发出请求之前分配授权令牌。 检查一下:https://github.com/dwyl/learn-json-web-tokens