简体   繁体   English

Passport身份验证请求路由仅在Node App的生产模式下不起作用

[英]Passport authentication request route not working only in production mode for Node app

Currently I have MERN app with passport included for auth flow. 目前,我有带有护照的MERN应用,用于身份验证流程。 In dev mode, runs smoothe and great. 在开发模式下,运行流畅且效果出色。 In production mode... major problems that I can't seem to figure out. 在生产模式下...我似乎无法解决的主要问题。 Im drowning in misery trying to figure out my bug. 我淹没在痛苦中,试图找出我的错误。

Front end: React (create-react-app package) Back end: Express + node 前端:React(create-react-app程序包)后端:Express +节点

In my configured development mode, the passport flow works fine as expected. 在我配置的开发模式下,护照流程可以按预期正常工作。 It creates a session and communicates with MongoDB to retrieve or create a user without any problems. 它创建一个会话并与MongoDB通信以检索或创建用户而没有任何问题。

However... when I switch to production mode, my client side has trouble communicating with the server side for the /auth/google passport GET route. 但是...当我切换到生产模式时,我的客户端无法与服务器端进行/ auth / google护照GET路由的通信。

Possible thoughts I had that I am probably wrong on: 我可能认为我可能是错误的:

1) The client relative link path wasn't accessing the proper route (I switched to production and ran on port 4000 to test) All my redirects and callback routes are created properly to handle on this route. 1)客户端相对链接路径未访问正确的路由(我切换到生产模式并在端口4000上运行以进行测试)我的所有重定向和回调路由均已正确创建以在此路由上进行处理。

2) Something wrong with the ordering of middleware on index.js page 2)index.js页面上的中间件排序有问题

Below is my main server file 下面是我的主服务器文件

#!/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)

Below is my client connect attempt 以下是我的客户端连接尝试

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

And my route snippet 还有我的路线片段

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

Any help, insight or guidance is greatly appreciated. 任何帮助,见解或指导,我们将不胜感激。

As it happens, it was a scoping issue. 碰巧的是,这是一个范围界定的问题。 The current parameters did not line up with what information I was looking to request. 当前参数与我要请求的信息不符。 Solved 解决了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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