Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 部署到Heroku时无法使用Google OAuth登录_Node.js_Reactjs_Express_Heroku - Fatal编程技术网

Node.js 部署到Heroku时无法使用Google OAuth登录

Node.js 部署到Heroku时无法使用Google OAuth登录,node.js,reactjs,express,heroku,Node.js,Reactjs,Express,Heroku,部署到Heroku时,我无法登录应用程序。我有一个成功的构建和部署,但当我启动应用程序时,它会启动我注销,在尝试登录时,它会挂起并抛出一个应用程序错误。 Express/Passport/MongoDB+创建应用程序 我检查了几次钥匙,每次都很完美。在我的本地服务器上,一切正常。服务器上的“https”响应是否有问题 应用程序链接 吉特回购 heroku错误日志: 2019-08-20T09:44:08.211815+00:00 heroku[路由器]:at=错误代码=H12 desc=“请求超

部署到Heroku时,我无法登录应用程序。我有一个成功的构建和部署,但当我启动应用程序时,它会启动我注销,在尝试登录时,它会挂起并抛出一个应用程序错误。 Express/Passport/MongoDB+创建应用程序

我检查了几次钥匙,每次都很完美。在我的本地服务器上,一切正常。服务器上的“https”响应是否有问题

应用程序链接

吉特回购

heroku错误日志:

2019-08-20T09:44:08.211815+00:00 heroku[路由器]:at=错误代码=H12 desc=“请求超时”方法=获取路径=“/auth/google/callback?code=4%2FpAFASjHeS\u JuqGKXbEIDY4Jh5zHBJZk-zyg19q6dhwgvtuipmckvt1hlpwkltjuhww9xb3eblcpgorequi68uze&scope=email+profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.com%2Fauth%2Fwww.googleapis.com%2Fuserinfo.profile&authuser=0&session=330af188789437fd21ab099fb679fb775fb675=提示”“主机=神秘-thicket-20426.herokuapp.com请求_id=59b62556-d07c-42ae-8f7f-e89b3ab5b735 fwd=“31.134.127.229”dyno=web.1 connect=1ms服务=30000ms状态=503字节=0协议=https 2019-08-20T09:52:03.838240+00:00 heroku[路由器]:at=info method=HEAD path=“/”host=神秘的灌木丛-20426.herokuapp.com请求\u id=81a31581-1aa6-478d-98af-68b6c479e32a fwd=“217.182.175.162”dyno=web.1 connect=0ms服务=1ms状态=200字节=289协议=https

//inex.js
const express = require('express');
const mongoose = require('mongoose');
const cookieSession = require('cookie-session');
const passport = require('passport');
const bodyParser = require('body-parser');
const keys = require('./config/keys');
require('./models/User');
require('./services/passport');

mongoose.connect(keys.mongoURI);

const app = express();

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


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

require('./routes/authRoutes')(app);
require('./routes/billingRoutes')(app);


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

const PORT = process.env.PORT || 5000;
app.listen(PORT);


// passport.js
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const mongoose = require('mongoose');
const keys = require('../config/keys');

const User = mongoose.model('users');

passport.serializeUser((user, done) => {
    done(null, user.id);
});

passport.deserializeUser((id, done) => {
    User.findById(id).then(user => {
        done(null, user);
    });
});

passport.use(
    new GoogleStrategy(
        {
            clientID: keys.googleClientID,
            clientSecret: keys.googleClientSecret,
            callbackURL: '/auth/google/callback',
            proxy: true
        },
        async (accessToken, refreshToken, profile, done) => {
            const existingUser = await User.findOne({ googleId: profile.id });

            if (existingUser) {
                return done(null, existingUser);
            }

            const user = await User({ googleID: profile.id }).save()
            done(null, user);
        }
    )
);

尝试将服务器的完整url添加为回调url, 检查文档 ,那么您的回电是什么


URL将是
https://mysterious-thicket-20426.herokuapp.com/auth/google/callback

解决了这个问题,以下是对我有效的方法:

请检查您的Mongodb Atlas prod数据库的用户/密码是否与您在Heroku中设置的MONGO_URI变量正确匹配,这是导致我出现此问题的原因。我认为很容易在这里输入错误,因为在创建数据库用户的密码后,您无法看到它的密码。还要注意的是,用户不应该是“只读”的,并且这与您登录Mongodb Atlas时使用的用户/密码组合不同


我发现身份验证回调失败并出现超时错误的原因是文件passport.js中定义的Google策略回调从未调用方法done()。这是因为Mongoose无法连接到mlab.com数据库,因此在MongoDB中查找用户的查询从未完成。我尝试在mlab中为prod数据库创建一个新用户,并在Heroku环境变量中更新它,结果成功了

与您的问题完全相同,设置和一切都相同

我检查了所有内容,发现MongoDB上的my集群没有活动,因此被暂停。当一段时间后没有与群集建立连接时,群集将暂停。 这意味着后端无法正确访问数据库,因此超时到
/auth/google/callback

发生的事情如下: 在您通过
/auth/google
页面上的OAuth凭证后,google会将您重定向回
/auth/google/callback

/auth/google/callback
端点的路由处理程序拾取请求并使用
passport.authenticate('google')
中间件(验证身份验证)

您的
passport.authenticate('google')
中间件使用google策略及其在
passport.js中设置的配置

回到没有连接到数据库的问题上,这意味着在谷歌的策略中会有一个身份验证问题。具体而言,在其回调函数中:

async(accessToken、refreshtToken、profile、done)=>{
const existingUser=await User.findOne({googleId:profile.id});
如果(现有用户){
返回完成(null,existingUser);
}
const user=await user({googleID:profile.id}).save()
完成(空,用户);
}
Express app之前无法连接到MongoDB,现在它尝试执行一个承诺:
等待User.findOne({googleId:profile.id})
,没有错误处理。 这意味着您的控制台中会出现
未处理的承诺拒绝错误
,并且您的应用程序会崩溃


就像@Agasthian Rathinavel提到的,仔细检查您的MongoDB凭据和环境变量,以确保后端能够连接到数据库。

您在google dashboard中更新了回调URL吗?是的,您在Heroku dashboard中添加了环境变量吗?是的,我在
keys.js
NODE\u ENV=='production'
但在heroku节点环境中,未设置变量。请检查heroku中的默认节点_ENV是否为生产节点