我不熟悉护照,正在制作自己的模块(例如passport-local
和passport-facebook
的样子),但是当我致电passport.use
时,我的策略没有出现在Authenticator对象中。因此,当我打电话给passport.authenticate
时,什么也没发生。有任何想法吗?为了简洁起见,以下是我的简短代码。提前非常感谢!
应用程序app.js:
当我在需求之后放置断点时,当我检查RaindropStrategy
时,策略构造器会正确显示,但会排除原型函数。如果最后省略.Strategy
,我将得到相同的结果。
/* Package requirements */
const createError = require('http-errors'),
express = require('express'),
session = require('express-session'),
passport = require('passport'),
RaindropStrategy = require('./passport-raindrop/lib/index.js').Strategy,
path = require('path'),
cookieParser = require('cookie-parser'),
logger = require('morgan');
/* App definition */
let app = express();
/* App configuration */
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({ secret: 'Super Secret' }));
app.use(passport.initialize());
app.use(passport.session());
/* Passport information */
// This is the raindrop strategy initialization
passport.use(new RaindropStrategy({
clientId: "INSERT HERE",
clientSecret: "INSERT HERE",
applicationId: "INSERT HERE"
}), function(req, done) {
done(null);
}
);
passport.serializeUser(function(user, done) {
done(null, user.userID);
});
passport.deserializeUser(function(id, done) {
let user = userDB.find(u => u.userID === id);
user ? done(null, user) : done(new Error('User not found'), user);
});
/* Authenticate the user with a local strategy*/
app.get('/login', function(req, res) {
passport.authenticate('raindrop', { userId: req.userId },
function(err, user) {
console.log(err + ' - ' + user);
}
)
});
/* Specifies Node.js port */
app.listen(3000, function() {
console.log('Listening on port 3000')
});
module.exports = app;
模块index.js
// This passport module's structure is based off of 'passport-facebook' written by jaredhanson
/* Load modules */
let Strategy = require('./strategy');
/* Expose Strategy */
exports = module.exports = Strategy;
/* Exports */
exports.Strategy = Strategy;
module strategy.js
let Raindrop = require('@hydrogenplatform/raindrop');
/* Strategy Constructor */
function Strategy(options) {
let strat = this;
strat.name = 'raindrop';
options = options || {};
options.environment = "Sandbox";
options.verbose = true;
strat.partner = new Raindrop.client.RaindropPartner(options);
}
/* Raindrop Authentication */
Strategy.prototype.authenticate = function(req) {
let strat = this;
return strat.partner.verifySignature(req.userId, req.message);
};
Strategy.prototype.generateMessage = function() {
let strat = this;
return strat.partner.generateMessage();
};
module.exports = Strategy;