Passport认证请求路由仅在生产模式下不适用于Node app

时间:2018-09-30 21:24:34

标签: javascript node.js reactjs passport.js

目前,我有带有身份验证流程护照的MERN应用。在开发模式下,运行流畅且效果出色。在生产模式下...我似乎无法解决的主要问题。我陷入困境,试图找出我的错误。

前端:React(create-react-app软件包) 后端:Express +节点

在我配置的开发模式下,护照流程可以按预期正常工作。它创建一个会话并与MongoDB通信以毫无问题地检索或创建用户。

但是...当我切换到生产模式时,我的客户端无法与服务器端进行/ auth / google护照GET路由的通信。

我可能认为我可能是错误的:

1)客户端相对链接路径未访问正确的路由(我切换到生产环境并在端口4000上运行以进行测试)我的所有重定向和回调路由均已正确创建以在此路由上进行处理。

2)index.js页面上的中间件排序存在问题

下面是我的主服务器文件

#!/usr/bin/env nodejs
require('dotenv/config')
const compression = require('compression')
const express = require('express')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
const router = express.Router()
const cookieSession = require('cookie-session')
const passport = require('passport')
const cors = require('cors')
const path = require('path')
const keys = require('./config/keys')


require('./services')
require('./models/User')
require('./models/Course')
require('./models/Curriculum')
require('./models/Module')
require('./models/Section')
require('./models/Tool')
require('./models/Quiz')
require('./models/BlackboardMemo')
require('./models/LiveClass')
require('./models/Response')
require('./models/StudentWork')



const app = express()


app.use(compression())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieSession({maxAge: 30*24*60*60*1000,keys: [keys.cookieKey]}))

app.use(passport.initialize())
app.use(passport.session())


mongoose.Promise=global.Promise

if(process.env.NODE_ENV==='production'){
  mongoose.connect(keys.mLabUrl)
} else {
  mongoose.connect(keys.mongoDB)
}


require('./services/Mailer')(app)
require('./routes/toolRoutes')(app)
require('./routes/authRoutes')(app)
require('./routes/courseRoutes')(app)
require('./routes/curriculumRoutes')(app)
require('./routes/userRoutes')(app)
require('./routes/fileRoutes')(app)
require('./routes/moduleRoutes')(app)
require('./routes/sectionRoutes')(app)
require('./routes/quizRoutes')(app)
require('./routes/liveClassRoutes')(app)
require('./routes/paymentRoutes')(app)
require('./routes/generalRoutes')(app)



if(process.env.NODE_ENV==='production'){
  app.use(express.static('client/build'))
  app.get('/', (req, res)=>res.sendFile(path.join(__dirname,'client/build/index.html')))
}



const PORT = process.env.PORT || 4000
const server = app.listen(PORT)

require('./sockets')(server)


console.log('Listening on port: '+PORT)

下面是我的客户端连接尝试

<div>
  <a href="/auth/google" className="altLogin"><i className="fab fa-google padding-sides-10 margin-sides-10 font-40"></i></a>
  <a href="/auth/linkedin" className="altLogin"><i className="fab fa-linkedin-in padding-sides-10 margin-sides-10 font-40"></i></a>
  <a href="/auth/facebook" className="altLogin"><i className="fab fa-facebook-f padding-sides-10 margin-sides-10 font-40"></i></a>
</div>

我的路线摘要

app.get('/auth/google',passport.authenticate('google',{scope: ['profile','email','https://www.googleapis.com/auth/drive.readonly'],accessType: 'offline', approvalPrompt: 'force'}))
      app.get('/auth/google/callback',passport.authenticate('google'),(req,res)=>res.redirect('/'))

非常感谢您的帮助,见解或指导。

1 个答案:

答案 0 :(得分:0)

碰巧,这是一个范围界定问题。当前参数与我要请求的信息不符。解决了