简体   繁体   中英

How do I use the node.js module passport-google

I am trying to make a node.js web application that tells the user to sign in using their gmail.

So I tried to use the instructions over here: http://passportjs.org/guide/google/ . I changed the url www.example.com to localhost, then ran the application. It tells me that it can't find User. Here is the whole log: User.findOrCreate({openID: identifier }, function(err, user) {(and then on the next line) ReferenceError: User is not defined.

You need to define "User" by calling it from a model. Create a User model (if you haven't already) and import it as a variable. Eg

var User = require('/path/to/User'); 

Sometimes I find it helpful for debugging to log the callback to the console, to see if the desired output is being spit out.

I just implemented one maybe this will help , I'm using Express the routes section is on the bottom.. Remember to set your host in the Google Key, my App has de full url of the AWS Server

var passport = require('passport');
// ====== Passport and OAuth2 API 
var GoogleStretegy = require('passport-google-oauth').OAuth2Strategy;

passport.serializeUser(function (user, done) {
    done(null, user);});
passport.deserializeUser(function (obj, done){
    done(null, obj);});

// Set Passport Initialize and Sessions
app.use(passport.initialize());
app.use(passport.session());

passport.use(new GoogleStretegy({
clientID: CREDENTIALS.google.GOOGLE_CLIENT_ID,
clientSecret: CREDENTIALS.google.GOOGLE_CLIENT_SECRET,
callbackURL:"<host>/oauth2callback"    
},
function (req, accessToken, refreshToken, profile, done) {
    process.nextTick(function () {
        console.log(JSON.stringify(profile));
        console.log(req);
        var username= profile.emails[0].value.split('@');
        User.findOne({email: profile.emails[0].value,username:username[0]}).exec(function (err,user) {
            if(!user){        
                var user = new User({username: username[0]});
                user.set('email',profile.emails[0].value);
                user.set('FullName',profile.DisplayName);
                user.save(function (err) {
                    if(err){
                        console.log(err);
                        profile=null;
                        return done(null,profile);
                    }else {
                        return done(null, profile);        
                    }
                });
            }else {
                return done(null, profile);
            }
        });
        // return done(null, profile);
    });
}
));

/// ROUTES !

router.get('/logout', function (req, res) {

req.session.destroy(function () {
// Google log out
    req.logout();
    res.redirect('/login');
});
});
//Google OAuth2
router.get('/auth/google',passport.authenticate('google', { scope: ['https://www.googleapis.com/auth/userinfo.profile','https://www.googleapis.com/auth/userinfo.email'] }));
router.get('/oauth2callback', passport.authenticate('google', { failureRedirect: '/login' }), function (req, res) {
    res.redirect('/');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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