在express中进行身份验证后的passport.js错误

时间:2017-11-10 10:38:04

标签: javascript express oauth-2.0 google-oauth passport.js

我正在尝试使用this strategy (passport-google-oauth20)通过Google在我的网站上运行OAuth 2.0。

但是,在我被Google重定向回我自己的网站后,我的网站崩溃并显示以下错误消息:

Error
    at /Users/theonlygusti/Project/node_modules/passport-google-oauth20/lib/strategy.js:95:21
    at passBackControl (/Users/theonlygusti/Project/node_modules/oauth/lib/oauth2.js:132:9)
    at IncomingMessage.<anonymous> (/Users/theonlygusti/Project/node_modules/oauth/lib/oauth2.js:157:7)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

此错误实际上同时出现在网页和控制台中。

以下是我试图开始工作的代码:

const express=require('express');
const app = express();
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;

passport.use(new GoogleStrategy({
  clientID: process.env.GOOGLE_OAUTH_CLIENT_ID,
  clientSecret: process.env.GOOGLE_OAUTH_CLIENT_SECRET,
  callbackURL: '/sign-in/after'
},
function(accessToken, refreshToken, profile, cb) {
  User.findOrCreate({ googleId: profile.id }, function (err, user) {
    return cb(err, user);
  });
}));

app.get('/sign-in/go', passport.authenticate('google', { scope: ['profile'] }));

app.get('/sign-in/after',
  passport.authenticate('google', { failureRedirect: '/sign-in' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/');
  });

const httpServer = app.listen(process.env.PORT);

如何停止此错误并通过重定向登录? (我的应用程序已经可以使用JavaScript按钮登录。)

0 个答案:

没有答案