/ / cb не є функцією в hapi-auth-jwt2 - Node.js - javascript, node.js, jwt, restful-authentication, hapijs

cb не є функцією hapi-auth-jwt2 - Node.js - javascript, node.js, jwt, restful-authentication, hapijs

Я наступний це підручник для реалізації jwt authentication в hapijs v17.2.

Я зробив все згідно з підручником, але наступна помилка зводить мене з розуму, навіть налагодження не зробило жодних змін.

помилка

Debug: internal, implementation, error
TypeError: cb is not a function
at Object.secretProvider [as key] (C:UsersuserWebstormProjectshapi-blognode_modulesjwks-rsalibintegrationshapi.js:30:14)
at Object.authenticate (C:UsersuserWebstormProjectshapi-blognode_moduleshapi-auth-jwt2libindex.js:123:87)
at module.exports.internals.Manager.execute (C:UsersuserWebstormProjectshapi-blognode_moduleshapilibtoolkit.js:35:106)
at module.exports.internals.Auth._authenticate (C:UsersuserWebstormProjectshapi-blognode_moduleshapilibauth.js:242:58)
at authenticate (C:UsersuserWebstormProjectshapi-blognode_moduleshapilibauth.js:217:21)
at module.exports.internals.Request._lifecycle (C:UsersuserWebstormProjectshapi-blognode_moduleshapilibrequest.js:261:62)
at <anonymous>

app.js

const hapi = require("hapi");
const mongoose = require("./db");
const hapi_auth_jwt = require("hapi-auth-jwt2");
const jwksa_rsa = require("jwks-rsa");
const dog_controller = require("./controllers/dog");

const server = new hapi.Server({
host: "localhost",
port: 4200
});

const validate_user = (decoded, request, callback) => {
console.log("Decoded", decoded);
if (decoded && decoded.sub) {
return callback(null, true, {});
}

return callback(null, true, {});
};

const register_routes = () => {
server.route({
method: "GET",
path: "/dogs",
options: {
handler: dog_controller.list,
auth: false
}
});

// Test
server.route({
method: "POST",
path: "/a",
options: {
handler: (req, h) => {
return h.response({message: req.params.a});
},
auth: false
}
});

server.route({
method: "GET",
path: "/dogs/{id}",
options: {
handler: dog_controller.get
}
});

server.route({
method: "POST",
path: "/dogs",
options: {
handler: dog_controller.create
}
});

server.route({
method: "PUT",
path: "/dogs/{id}",
handler: dog_controller.update
});

server.route({
method: "DELETE",
path: "/dogs/{id}",
handler: dog_controller.remove
});
};

const init = async () => {
await server.register(hapi_auth_jwt);

server.auth.strategy("jwt", "jwt", {
key: jwksa_rsa.hapiJwt2Key({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
// YOUR-AUTH0-DOMAIN name e.g https://prosper.auth0.com
jwksUri: "https://mrvar.auth0.com/.well-known/jwks.json"
}),
verifyOptions: {
audience: "https://mrvar.auth0.com/api/v2/",
issuer: "https://mrvar.auth0.com",
algorithm: ["RS256"]
},
validate: validate_user
});

server.auth.default("jwt");

// Register routes
register_routes();

// Start server
await server.start();

return server;
};

init().then(server => {
console.log("Server running at: ", server.info.uri);
}).catch(err => {
console.log(err);
});

Коли я роблю запит на маршрути з auth: false, обробник працює належним чином, тоді я отримую очікуваний результат, але запити до маршрутів без auth повернути наступний json:

{
"statusCode": 500,
"error": "Internal Server Error",
"message": "An internal server error occurred"
}

Більше інформації:

версія вузла: 8.9.4

версія npm: 5.6.0

версія hapi: 17.2.0

hapi-auth-jwt2: github: salzhrani / hapi-auth-jwt2 # v-17

jwks-rsa: 1.2.1

мангуст: 5.0.6

nodemon: 1.15.0

Відповіді:

1 для відповіді № 1

Функція перевірки змінена у hapi @ 17, щоб не мати функції зворотного виклику. На основі вашого прикладу тепер це має виглядати приблизно так:

const validate = async (decoded, request) => {
if (decoded && decoded.sub) {
return { isValid: true };
}
return { isValid: false );
};

Частина поверненого об'єкта також може включати credentials який буде представляти користувача, який аутентифікується, і ви також можете зробити область дії як частину облікових даних.

Тоді, якщо ви хочете, ви можете отримати доступ до облікових даних як частина request об'єкт подібний request.auth.credentials