Я пытаюсь передать переменную промежуточного программного обеспечения и быть полезной после успешной аутентификации с использованием facebook-паспорта.Передать переменную от промежуточного ПО к паспорту в Express.js
// route for facebook authentication and login
router.get('/connect/facebook', rentalinfo, passport.authenticate('facebook', { scope : 'email' }));
// handle the callback after facebook has authenticated the user
router.get('/connect/facebook/callback', passport.authenticate('facebook', {
//successRedirect : '../../../protected',
failureRedirect : '/'
}),
function(req, res) {
// successful auth, user is set at req.user. redirect as necessary.
//Get the redirect location from the response
//console.log(req.body);
console.log(req.bookingarray);
res.redirect('/protected');
});
Мой Middleware:
//Rent item middleware - carries rental info through signup/login process
function rentalinfo(req, res, next){
//console.log('MIDDLEWARE VARIABLE: '+ bookingarray);
req.bookingarray = 'SOMETHING';
//return bookingarray;
//if (something(res)) redirect('http://google.com'); // no access to your site
next(); // go to routes
};
Я затем попытаться получить к нему доступ в моей facebook паспортный стратегии.
passport.use(new FacebookStrategy({
// pull in our app id and secret from our auth.js file
clientID : configAuth.facebookAuth.clientID,
clientSecret : configAuth.facebookAuth.clientSecret,
callbackURL : configAuth.facebookAuth.callbackURL,
profileFields : ["emails", "displayName"],
// this allows us to pass in the req from our route (lets us check if a user is logged in or not)
passReqToCallback : true
},
// facebook will send back the token and profile
function(req, token, refreshToken, profile, done) {
console.log(req.bookingarray);
// asynchronous
process.nextTick(function() {
// check if the user is already logged in
if (!req.user) {
...
}
}
}));
Однако он просто возвращается как неопределенный. Я пробовал, чего мне не хватает? Цель состоит в том, чтобы сохранить состояние пользователя до и после аутентификации.
Ваш код выглядит хорошо. Уверены ли вы, что у вас нет опечатки или определено значение, указанное вами * bookingarray *? Нет ли другого кода, который может повлиять на * req.bookingarray *? – Molda
обновлен с помощью обоих маршрутов facebook/connect/facebook/и/connect/facebook/callback. Я знаю, что авторизуется правильно без переменной. Большое спасибо за вашу помощь. Ив ударился головой о стену. – Cookiejest