本地主机上的护照谷歌oauth

fri*_*ley 14 node.js oauth-2.0 express passport.js

我很擅长使用护照进行身份验证,因此需要大量的代码片段

我的服务器配置为:

var router = require('./app/config/routes');
var googleStrategy = require('./app/config/passport');
var session = require("express-session");

var passport = require('passport');
app.use(session({secret : '<secret-key>'}));
app.use(passport.initialize());
app.use(passport.session());
googleStrategy(passport); 
Run Code Online (Sandbox Code Playgroud)

我的路由配置为

module.exports = function(app, passport) {

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

    app.get('/auth/google/callback', function() {
        passport.authenticate('google', {
            successRedirect: '/profile',
            failureRedirect: '/fail'
        });
    });

    .... ALSO configured /profile and /fail
};
Run Code Online (Sandbox Code Playgroud)

我的护照配置为

passport.serializeUser(function(user, callback){
        console.log('serializing user.');
        callback(null, user);
    });

    passport.deserializeUser(function(user, callback){
       console.log('deserialize user.');
       callback(null, user);
    });

    var processRequest = function(token, refreshToken, profile, callback){
        process.nextTick(function(){
           console.log('id : '+ profile.id);
           console.log('name :'+ profile.displayName);
           console.log('email :' + profile.emails);
           console.log('token : '+ token);
        });
    };

    passport.use(new GoogleStrategy({
        clientID: 'client ID',
        clientSecret : 'client SECRET',
        callbackURL : 'http://127.0.0.1:8080/auth/google/callback',
        realm : 'http://127.0.0.1:8080'
    }, processRequest));
Run Code Online (Sandbox Code Playgroud)

问题:在去的时候/auth/google,我从未收到过确认画面.我应该看什么?

更新:

将路由更改为下面显示的配置使其工作.

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

    app.get('/auth/google/callback', 
        passport.authenticate('google', {
            successRedirect: '/profile',
            failureRedirect: '/fail'
        })
    );
Run Code Online (Sandbox Code Playgroud)

Kar*_*Rao 28

目前,用于身份验证和autherization的OAUTH2协议得到了google的很好支持.所以最好使用相同的方法.这是谷歌的文档.使用'passport-google-oauth'模块.这是实现.这应该是应用程序对象配置,也可以看看来自passport-google-oauth模块的oauth2strategy对象,也检查了app.get路由注册中的范围.

var googleStrategy = require('passport-google-oauth').OAuth2Strategy;
  app.configure(function() {

    app.set('views',  './views');
    app.set('view engine', 'jade');
    app.use(express.favicon());
    app.use(express.logger('dev'));
    app.use(express.cookieParser());
    app.use(express.bodyParser());
    app.use(express.session({secret:'MySecret'}));
    app.use(passport.initialize());
    app.use(passport.session());
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.static('./public'));
});

app.get('/auth/google', select.passport.authenticate('google',{scope: 'https://www.googleapis.com/auth/plus.me https://www.google.com/m8/feeds https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'}));

app.get('/auth/google/callback', function() {
    passport.authenticate('google', {
        successRedirect: '/profile',
        failureRedirect: '/fail'
    });
});
app.get('/logout', function (req, res) {
        req.logOut();
        res.redirect('/');
    });
Run Code Online (Sandbox Code Playgroud)

但在创建新策略之前,请访问googles开发人员控制台并获取clientID和secret.这是步骤

  1. 去这个链接并创建项目,这里是相同的快照在此输入图像描述
  2. 给出一个新的项目名称和ID,这里是快照 在此输入图像描述
  3. 创建新项目大约需要一分钟,创建新项目后,它会将您重定向到应用程序的应用程序配置.在重定向的页面中选择APIS AND AUTH - > API,在API的页面中启用GOogle + API,这里是它的快照在此输入图像描述
  4. 然后转到凭据(在API下面),然后单击Create New Client Id,并为您的应用注册域和回调(将域配置为localhost),这是它的快照!在此输入图像描述 然后你会得到你的新身份证和秘密.使用它们来创建新策略

    passport.use(new googleStrategy({
        clientID: '<TheNewclientID>',
        clientSecret: '<The New Secret>',
    
        callbackURL: "http://locahost:8080/auth/google/callback"
    },
    function (accessToken, refreshToken, profile, done) {
        console.log(profile); //profile contains all the personal data returned 
        done(null, profile)
    }
    ));
    
    Run Code Online (Sandbox Code Playgroud)

6.现在序列化和反序列化

passport.serializeUser(function(user, callback){
        console.log('serializing user.');
        callback(null, user.id);
    });

passport.deserializeUser(function(user, callback){
       console.log('deserialize user.');
       callback(null, user.id);
    });
Run Code Online (Sandbox Code Playgroud)

运行服务器并转到localhost:8080/auth/google(不要使用127.0.0.1:8080而不是locahost).这应该让它工作:)

[其他有用的链接:由kvcrawford在模块的回购退房的第一个评论页护照,谷歌是另一种流行的模块,它是用来提供使用谷歌登录,它的种类已经过时,现在,这里是链接相对于最近的问题]


Sei*_*uno 7

在Web上的大多数示例中,路由代码都是这样完成的:

app.get('/auth/google', passport.authenticate('google'));
Run Code Online (Sandbox Code Playgroud)

根据Express Reference,该app.get方法的回调有三个参数request,response和'next'.这意味着,在上述例子中的authenticate方法返回一个功能对象,并将其与三个参数执行request,response和"下一步".

所以,如果你想在方法的回调函数中进行身份验证,app.get如下所示:

app.get('/auth/google', function() {
    passport.authenticate('google', {scope: ['profile', 'email']});
});
Run Code Online (Sandbox Code Playgroud)

那你应该写:

app.get('/auth/google', function(request, response, next) {
    passport.authenticate('google', {scope: ['profile', 'email']})(request, response, next);
});
Run Code Online (Sandbox Code Playgroud)