[英]passportjs authentication using google apps email id
I am trying passport js with google app email id. 我正在尝试使用谷歌应用程序电子邮件ID的护照js。 I am able to authenticate using gmail.com email id. 我可以使用gmail.com电子邮件ID进行身份验证。 But how can I authenticate if the email id is a google app id (google.com/a/companyname.com). 但是,如果电子邮件ID是谷歌应用程序ID(google.com/a/companyname.com),我该如何进行身份验证。
This is my code 这是我的代码
var express = require('express');
var app = express();
var passport = require('passport');
var GoogleStrategy = require('passport-google').Strategy;
passport.use(new GoogleStrategy({
returnURL: 'http://10.3.0.52:3000/auth/google/return',
realm: 'http://10.3.0.52:3000/'
},
function(identifier, profile, done) {
User.findOrCreate({
openId: identifier
}, function(err, user) {
done(err, user);
});
}
));
app.get('/auth/google', passport.authenticate('google'));
app.get('/auth/google/return',
passport.authenticate('google', {
successRedirect: '/',
failureRedirect: '/login'
}));
app.get('/', function(req, res){
res.writeHead(200);
res.end("connected");
});
app.listen(process.env.PORT || 3000);
Your code is missing some vital parts: 您的代码缺少一些重要部分:
...
passport.use(...); // this you have
// these are required as well.
app.use(passport.initialize());
app.use(passport.session());
// please read docs for the following two calls
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
...
With those in place, I can log in using my Google App address just fine. 有了这些,我可以使用我的Google App地址登录。
EDIT: it only works with Node 0.8 though, Node 0.10 gives an error. 编辑:它只适用于Node 0.8,但节点0.10给出错误。 I think using passport-google-oauth
is a better solution anyway. 我认为使用passport-google-oauth
无论如何都是更好的解决方案。 For that, you have to register your application with Google ( here ); 为此,您必须向Google注册您的申请( 此处 ); after registration, you'll be supplied both the GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET codes which you can use. 注册后,您将获得可以使用的GOOGLE_CLIENT_ID和GOOGLE_CLIENT_SECRET代码。
I have created a method that verifies if the email domain is the one i want to authorize: 我创建了一个方法来验证电子邮件域是否是我想要授权的域:
UserSchema.method('checkFordomain', function(value) {
var parts = value.split('@');
return (parts[1] == 'companyname.com');
});
this is method I put in the model of the user model, using mongoose schema models 这是我在用户模型的模型中使用mongoose模式模型的方法
if (!user.checkForMMdomain(profile.emails[0].value)) {
return done();
}
in the callback of the passport google strategy https://github.com/jaredhanson/passport-google-oauth 在护照google策略的回调中https://github.com/jaredhanson/passport-google-oauth
In your passport.use
callback you can perform additional checking based on the domain of the primary email address (or whatever you are checking): 在您的passport.use
回调中,您可以根据主电子邮件地址的域(或您正在检查的任何内容)执行其他检查:
if (profile.emails[0].split('@')[1] !== authorizedDomain) {
return done(null, false);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.