auth.js
1013 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Add following code to auth() function in
* ./config/auth.js
*
* It's better to keep 'returnURL' and 'realm'
* in settings.
*/
var GoogleStrategy = require('passport-google').Strategy
passport.use( new GoogleStrategy({
returnURL: 'http://localhost:3000/auth/google/callback',
realm: 'http://localhost:3000',
passReqToCallback: true
},
function(req, identifier, profile, done) {
var email = profile.emails[ 0 ].value
var User = lib.db.model('User')
// save user
User.findOne({
email: email
}, function( err, user ) {
if (!user) {
User.create({
email: email,
name: profile.displayName,
}, function(err, user) {
// Update lastToken
user.lastToken = identifier
user.save()
return done( err, user )
});
} else {
// Update lastToken
user.lastToken = identifier
user.save()
return done( err, user )
}
});
}
));