简体   繁体   English

google-oath登录后挂起的node.js / express / passport

[英]node.js/express/passport hanging after google-oath login

i am trying to learn node.js and having a bit of a headache doing the express login storing cookies with google oauth. 我正在尝试学习node.js,并在使用Google oauth进行快速登录存储cookie时有些头疼。 I am using mongodb, user gets saved without a problem. 我正在使用mongodb,用户可以毫无问题地得到保存。

I can log in just fine, but when i set the cookies baaaam. 我可以很好的登录,但是当我设置饼干baaaam时。 But there is no error on console. 但是控制台上没有错误。

If i try to set cookies using cookies-session and express, the app hangs and i can't access any route '/' even the '/logout'. 如果我尝试使用cookies-session和express设置cookie,则该应用程序会挂起,并且我无法访问任何路由“ /”,甚至无法访问“ / logout”。 I only able to access them again if i clear the cookies on browser. 如果清除浏览器上的cookie,我只能再次访问它们。 I am stuck at this point. 我被困在这一点上。

this is my app.js 这是我的app.js

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

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

my routes 我的路线

app.get('/auth/google',
  passport.authenticate('google', { 
    scope: ['profile', 'email'] 
  })
);

app.get('/auth/google/callback', passport.authenticate('google'));
app.get('/api/logout', (req, res) => {
    req.logout();
    res.send(req.user);
 });

and my passport.js 和我的passport.js

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

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

});

passport.use( new GoogleStrategy({
    clientID: keys.googleClientID,
    clientSecret: keys.googleClientSecret,
    callbackURL: '/auth/google/callback'
    },
    (accessToken, refreshToken, profile, done) => {
        User.findOne({ googleId: profile.id }).then((existingUser) => {
                if (existingUser) {
                // don't create a new user
                done(null, existingUser);

                } else {
                // create a new user    
                new User ({ googleId: profile.id }).save()
                .then(user => (null, user));

                }
            })

    }   

));

So i was able to get this working, but I want to know what was did wrong. 所以我能够使它工作,但我想知道做错了什么。

I changed this and it worked, anyone has a clue? 我改变了它,它奏效了,有人知道吗?

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

});

to

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

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

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