我在 hapijs 中实现了 oauth2orize。但是当我调用 api 时,什么也没有发生。该函数进入 oauth2orize 模块的 code.js 文件并挂在两者之间。请建议我如何在 hapjs 中实现 oauth2orize。hapi-oauth2orize 也无法正常工作,因为移民和 hapi-oauth2orize 插件会引发选项错误。
const Hapi = require('hapi');
const server = new Hapi.Server();
const oauth2orize = require('oauth2orize');
var oauth = oauth2orize.createServer();
server.connection({
host: 'localhost',
port: 8000
});
server.register([{
register: require('hapi-mongodb'),
options: dbOpts
}], function (err) {
if (err) {
console.error(err);
throw err;
}
server.start();
server.route([
{
method: 'GET',
path: '/oauth/authorizegrant',
config: {
auth: false,
handler: function(request, reply) {
var clientId = request.query.client_id,
redirectUrl = request.query.redirect_uri,
resType = request.query.response_type,
state = request.query.state;
oauth.grant(oauth2orize.grant.code(function(clientId,redirectUrl,resType,state,callback) {
// Create a new authorization code
console.log('client', client);
var db = request.server.plugins['hapi-mongodb'].db;
var code = new Code({
value: uid(16),
clientId: client._id,
redirectUri: redirectUri,
userId: user._id
});
// Save the auth code and check for errors
db.collection('codes').insert(code, function(err) {
if (err) { console.log('err*********', err); return callback(err); }
callback(null, code.value);
});
}));
}
}
},
]);
});