我有点沮丧,因为我不知道如何重构这个脚本。我写了authenticate函数,但是它太复杂了,在一个函数中可能太大了,看起来有点spagetti :(,我将如何让它变得更好?一些线索?谢谢。
"use strict";
var errorResponse = require('../errorResponse'),
paynopain = require('../../core/paynopain'),
User = require('../../core/User'),
cache = require('../../core/cache/cache'),
validateValues = require('../validateValues'),
API_PATH = 'v1/';
function authenticate(req, res, next) {
if (req.url.indexOf(API_PATH) !== -1){
var pnpUserId;
var validated = validateValues(req, ['pnp_access_token']);
if(validated.error){
return next(validated.error);
}
var pnpAccessToken = req.params.pnp_access_token;
cache.read('pnpToken:' + pnpAccessToken)
.then(function(userId){
if(userId){
storeUserIdInRequest(req, next, userId);
}else{
var user = new User(req.logger);
return paynopain.getIdFromAccessToken(pnpAccessToken)
.then(function(pnpUserIdResult){
pnpUserId = pnpUserIdResult;
return user.findByPnpID(pnpUserIdResult);
})
.then(function(userData){
if(userData._id){
return storeUserIdInRequest(req, next, userData._id.toString());
}else{
var newUser = {
pnpId: pnpUserId,
radius: 1000
};
return user.create(newUser)
.then(function(userData){
return storeUserIdInRequest(req, next, userData._id);
});
}
});
}
})
.fail(function(e){
if(e.message === 'invalid_grant'){
next(errorResponse.unauthorized());
}else{
req.logger.log('error', 'Error authenticating', {
error: {
message: e.message,
stack: e.stack
}
});
next(errorResponse.internalError());
}
});
}else{
next();
}
}
function storeUserIdInRequest(req, next, userId){
var oneHour = 1000 * 60 * 60;
cache.write('pnpToken:' + req.params.pnp_access_token, userId, {
expire: oneHour
})
.then(function(){
req.userId = userId;
req.logger.setExtra({
userId: userId
});
next();
});
}
function init(server){
server.use(authenticate);
require('./configuration/routes')(API_PATH, server);
require('./products/routes')(API_PATH, server);
require('./tracking/routes')(API_PATH, server);
require('./lists/routes')(API_PATH, server);
}
module.exports = init;
答案 0 :(得分:0)
这不是更好,因为我无法看到和更改User
,cache
和其他对象方法的行为,但我希望您可以使用其中的一些想法:
"use strict";
var errorResponse = require('../errorResponse'),
paynopain = require('../../core/paynopain'),
User = require('../../core/User'),
cache = require('../../core/cache/cache'),
validateValues = require('../validateValues'),
API_PATH = 'v1/';
function authenticate(req, res, next) {
if (req.url.indexOf(API_PATH) == -1) {
return next();
}
var validated = validateValues(req, ['pnp_access_token']);
if (validated.error){
return next(validated.error);
}
var pnpAccessToken = req.params.pnp_access_token;
cache.read('pnpToken:' + pnpAccessToken)
.then(function(userId){
if (userId) return userId;
var user = new User(req.logger);
return paynopain.getIdFromAccessToken(pnpAccessToken)
.then(function(pnpUserIdResult){
return [ user.findByPnpID(pnpUserIdResult), pnpUserIdResult ];
})
.spread(function(userData, pnpUserId){
if (userData._id){
return userData;
}
return user.create({
pnpId: pnpUserId,
radius: 1000
});
}).then(function(user) {
return user._id.toString();
});
})
.then(function(userID) {
return storeUserIdInRequest(req, userId);
})
.then(next)
.fail(function(e){
if(e.message === 'invalid_grant'){
next(errorResponse.unauthorized());
}else{
req.logger.log('error', 'Error authenticating', {
error: {
message: e.message,
stack: e.stack
}
});
next(errorResponse.internalError());
}
});
}
function storeUserIdInRequest(req, userId){
var oneHour = 1000 * 60 * 60;
return cache.write('pnpToken:' + req.params.pnp_access_token, userId, {
expire: oneHour
})
.then(function(){
req.userId = userId;
req.logger.setExtra({
userId: userId
});
});
}
function init(server){
server.use(authenticate);
require('./configuration/routes')(API_PATH, server);
require('./products/routes')(API_PATH, server);
require('./tracking/routes')(API_PATH, server);
require('./lists/routes')(API_PATH, server);
}
module.exports = init;
注意:我无法测试它,所以我不知道它是否真的很好用!
提示:
User
和其他核心'对象方法中)else