Google oAuth重定向挂起NodeJS

时间:2018-04-11 19:35:25

标签: node.js

目前正在创建使用google oAuth-20的应用程序,在重定向回到我的网站期间,进程挂起最终给我一个代码被兑换,由于谷歌的实例登录你,因为超时错误预先激活。 我将假设以下承诺存在问题

let passport = require('passport'),
    GoogleStrategy = require('passport-google-oauth20').Strategy,
    keys = require('./keys')
    User = require('../models/user-model');


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

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

passport.use(
    new GoogleStrategy({
        // options for google strategy
        clientID: keys.google.clientID,
        clientSecret: keys.google.clientSecret,
        callbackURL: '/auth/google/redirect'
    }, (accessToken, refreshToken, profile, done) => {
        // check if user already exists in our own db
        User.findOne({googleId: profile.id}).then((currentUser) => {
            if(currentUser){
                // already have this user
                console.log('user is: ', currentUser);
                done(null, currentUser);
            } else {
                // if not, create user in our db
                new User({
                    googleId: profile.id,
                    username: profile.displayName,
                }).save().then((newUser) => {
                    console.log('created new user: ', newUser);
                    done(null, newUser);
                });
            }
        });
    })
);

我认为错误发生在此代码块中 -

}).save().then((newUser) => {
                        console.log('created new user: ', newUser);
                        done(null, newUser);
                    });

因为从谷歌请求的用户详细信息被打印到屏幕上,我怎么会得到一个“未处理的促销拒绝警告:未处理的承诺拒绝”。此错误源于通过在catch块中抛出异步函数,或者拒绝未使用.catch()处理的promise。 (拒绝ID:1)'

Passport是init'在我的sever.js文件中 -

let express = require('express'),
    mongoose = require('mongoose'),
    passportConfig = require('./config/passport-config'),
    bodyParser = require('body-parser'),
    keys = require('./config/keys'),
    cookieSession = require('cookie-session'),
    passport = require('passport'),
    User = require('./models/user-model'),
app = express();


//Define routes for configuration - refactor into dedicated file
let landingRoute = require('./routes/landingpage'),
    authenticationRoute = require('./routes/authentication'),
    profileRoute = require('./routes/profile');

app.use(landingRoute);
app.use('/profile', profileRoute);
app.use('/auth', authenticationRoute);

app.use(bodyParser.urlencoded({ extended: true }));

app.use(cookieSession({
    //sets the max age of the cookie to 1 day
    maxAge: 24 * 60 * 60 * 1000,
    keys: [
        keys.sessionKey.cookieKey
    ]
}));


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

//mongodb connection configuration 
mongoose.connect(keys.mongodb.dbURI, () => {
    console.log('db connected');
});

//General app configuration
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public/css'));


let port = 8080

//Listen ports
app.listen(port, () => {
    console.log("Sever started");
});

非常感谢任何帮助,谢谢

0 个答案:

没有答案