护照会议明确req.session

时间:2018-05-18 00:25:16

标签: javascript node.js session passport.js

我正在使用NodeJS编写会话,当我得到用户连接时,首先使用MAC ADDRESS创建一个session.client,到目前为止一直很好,但后来我向客户询问是否要继续并登录应用社交网络,如Facebook,Instagram,Tweeter或Google+,然后当用户被重定向到社交登录时,它与来自passportjs的其他会话一起返回并清除会话的初始数据,我丢失了客户端信息。所以,我试图在session,session.data,session.test,session.whatever中改变数据的名称,但总是发生相同的情况,当我测试并且护照重定向我并返回我的域时,会话是干净的,它随着来自passportjs的新数据而变化,任何人都知道这里发生了什么?任何想法如何解决这个问题?

代码运行完美,问题是当回到//社交登录时的会话,它清除我的初始数据并返回护照数据。 //我需要我的初始数据才能继续工作!

这只是代码的摘录。它的工作原理

'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 ////////////////

1 个答案:

答案 0 :(得分:0)

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