Ich bin sehr neu in hapijs, ich mache eine Demo-App mit Authentifizierung mit hapi-auth-basic
. aber mein Code, (server.auth.strategy('simple', 'basic', { validateFunc: validate});
) die validate
Aufruf der Methode nicht, und das Ergebnis ist immer unberechtigten geben, wie unten:Fehlende Authentifizierung mit Statuscode 401, in Hapijs?
{
"statusCode": 401,
"error": "Unauthorized",
"message": "Missing authentication"
}
ich autorisiert möchte, kann mir jemand empfehlen, was und wo zu tun, ich tue, falsch ?
'use strict';
const Bcrypt = require('bcrypt');
const Hapi = require('hapi');
const Basic = require('hapi-auth-basic');
const server = new Hapi.Server();
server.connection({ port: 3000 });
const users = {
john: {
username: 'john',
password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm', // 'secret'
name: 'John Doe',
id: '2133d32a'
}
};
const validate = function (request, username, password, callback) {
const user = 'john';
console.log("1");
if (!user) {
return callback(null, false);
}
Bcrypt.compare(12345, 12345, (err, isValid) => {
console.log("2");
callback(err, isValid, { id: user.id, name: user.name });
});
};
server.register(Basic, (err) => {
console.log("3");
if (err) {
throw err;
}
server.auth.strategy('simple', 'basic', { validateFunc: validate});
server.route({
method: 'GET',
path: '/',
config: {
auth: 'simple',
handler: function (request, reply) {
console.log("4");
reply('hello, ' + request.auth.credentials.name);
}
}
});
server.start((err) => {
if (err) {
throw err;
}
console.log('server running at: ' + server.info.uri);
});
});
Waren u in der Lage, diese zu lösen ... ?? –