简体   繁体   中英

Passport session clear req.session

I'm coding a session with NodeJS, when I get the user connection first create a session.client with the MAC ADDRESS, so far so good, but then I ask to the client if he want to continue and login on the app with social -network like Facebook, Instagram, Tweeter or Google+, and then when the user is redirected to the social login it back with other session from passportjs and clear al my init data of session and I lost the client information. So, I tried to change the name of the data in session, session.data, session.test, session.whatever but always happen the same, when I test and the passport redirect me and back to my domain, the session is clean and it change with new data from passportjs, any one know what's happen here? any idea how to solve this?

the code run perfectly, the problem is the session when go and back to // the social login, it clear my init data and back with the passport data. // I need my init data to continue working!

this is just an extract of code. It works

 'use sctrict' const https = require('https'), fs = require('fs'), path = require('path'), morgan = require('morgan'), logger = require('express-logger'), express = require('express'), favicon = require('serve-favicon'), bodyParser = require('body-parser'), methodOverride = require('method-override'), passport = require('passport'), // config files port = 443, mongodbConfig = require('./config/mongodb-config'), session = require('express-session'), keys = require('./config/keys'), options = { key: fs.readFileSync('./config/ssl/server.key'), cert: fs.readFileSync('./config/ssl/server.crt') }, cookieParser = require('cookie-parser'), loginAPRoutes = require('./routes/loginAPRoutes'), passportSetup = require('./config/passport-setup'), app = express() // MongoDB - Mongoose connection const mongoose = require('mongoose') mongoose.Promise - global.Promise mongoose.connect('mongodb://' + mongodbConfig.mongodb.route + '/' + mongodbConfig.mongodb.db, {}) .then(() => console.log('db connected')) .catch(err => console.log(err)) // config app.set('view engine', 'ejs') // middlewares app.use(morgan('dev')) app.use(favicon(path.join(__dirname, 'public/img/', 'favicon.ico'))) app.use(bodyParser.urlencoded({ extended: false })) app.use(bodyParser.json()) app.use(methodOverride('X-HTTP-Method-Override')) app.use(express.static(path.join(__dirname, 'public'))) app.use(session({ secret: 'cybor-cat', resave: false, saveUninitialized: true })) // initilize passport app.use(passport.initialize()) app.use(passport.session()) // Main routes app.use('/guest', loginAPRoutes) app.use('/auth', loginAPRoutes) // Run the server https https.createServer(options, app).listen(port, () => { console.log('NodeJS Server Started... splice.pro is running!') }) router.get('/s/:site', (req, res) => { data = req.query data.site = req.params.site req.session.data = data console.log('===== session ========') console.log(req.session) console.log('====== session END =======') res.render('login') }) /////////////// GOOGLE AUTH //////////////// // route for google login router.get('/google', passport.authenticate('google', { scope: ['profile', 'email'] })) // route for google and redirect router.get('/google/callback', passport.authenticate('google'), (req, res) => { if (!req.user) { res.redirect('/guest/s/site') } else { /////////// here comes the new session from passport :( ////// ////////// and lost the first data of my session ///// console.log(req.session.data) //////////////// this show the session with info of user /////// /////////////// but req.session.data is lost /////////// res.redirect('/guest/startconnection') } } ) /////////////// GOOGLE AUTH END //////////////// 

好吧,好吧....我发现了我的问题,我的外部站点使用ip将我重定向到了我的服务器,当护照登录请求重定向时,它又回到了域名,这就是为什么它会生成一个新的会话ID的原因。 ..漫长的一天但是最后我找到了!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM