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
Javascript Passport和Node.JS结构_Javascript_Node.js_Passport.js - Fatal编程技术网

Javascript Passport和Node.JS结构

Javascript Passport和Node.JS结构,javascript,node.js,passport.js,Javascript,Node.js,Passport.js,我正在使用一个平均堆栈,并希望将Passport集成到其中。然而,我并不完全确定我应该如何构建它。我最终想包括几个策略,但让我们从谷歌开始 在我的routes.js文件中,我有: app.get('/auth/google', passport.authenticate('google')); app.get('/auth/google/return', passport.authenticate('google', { successRedirect: '/',

我正在使用一个平均堆栈,并希望将Passport集成到其中。然而,我并不完全确定我应该如何构建它。我最终想包括几个策略,但让我们从谷歌开始

在我的routes.js文件中,我有:

app.get('/auth/google', passport.authenticate('google'));

app.get('/auth/google/return', passport.authenticate('google', { successRedirect: '/',
                                failureRedirect: '/login' }));

这是有道理的。但是在我的顶层server.js中,我应该把GoogleStrategy构造函数/成功回调代码放在哪里呢?假设我有5种不同的策略,它们都应该放在那里吗?

对于我们的应用程序,我们创建了一个单独的passport.js文件,其中包含我们所有的社交注册策略以及一个单独的路由文件,下面是我们谷歌的外观:

app.js

passport.js位于根目录中

var passport = require('passport'),
    GoogleStrategy = require('passport-google-oauth').OAuth2Strategy,
    config = require('./config');
var socialAuth = config.socialAuth; //contains keys

module.exports = function(passport) {
    /*DEFINE LOGIN STRATS HERE*/
    //GOOGLE+
    passport.use(new GoogleStrategy({
        clientID: socialAuth.google.GOOGLE_CLIENT_ID,
        clientSecret: socialAuth.google.GOOGLE_CLIENT_SECRET,
        callbackURL: config.callbackDomain + "/auth/google/callback",
        scope: 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email'
    }, function(accessToken, refreshToken, profile, done) {
            //code for strat here
            return done(null, userDoc);
       }
    });
}
social.js路线


它可以很好地管理您想要的每种类型的战略,并且很容易添加。

谢谢!当然,我刚刚发现passport google不再适用于新的实现。必须转到Google+登录。
var passport = require('passport'),
    GoogleStrategy = require('passport-google-oauth').OAuth2Strategy,
    config = require('./config');
var socialAuth = config.socialAuth; //contains keys

module.exports = function(passport) {
    /*DEFINE LOGIN STRATS HERE*/
    //GOOGLE+
    passport.use(new GoogleStrategy({
        clientID: socialAuth.google.GOOGLE_CLIENT_ID,
        clientSecret: socialAuth.google.GOOGLE_CLIENT_SECRET,
        callbackURL: config.callbackDomain + "/auth/google/callback",
        scope: 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email'
    }, function(accessToken, refreshToken, profile, done) {
            //code for strat here
            return done(null, userDoc);
       }
    });
}
exports.googleCallback = function(req, res) {
    //do stuff with the return from passport
}