-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
55 lines (47 loc) · 1.57 KB
/
auth.js
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
49
50
51
52
53
54
/*
Read the docs:
http://www.passportjs.org/docs/
https://github.com/jaredhanson/passport-google-oauth2
*/
var passport = require('passport');
var GoogleStrategy = require('passport-google-oauth20').Strategy;
var config = require('./config.js');
var passport_initialize_middleware = passport.initialize();
var passport_session_middleware = passport.session();
// Use the GoogleStrategy within Passport.
// Strategies in Passport require a `verify` function, which accept
// credentials (in this case, an accessToken, refreshToken, and Google
// profile), and invoke a callback with a user object.
passport.use(new GoogleStrategy({
clientID: config.google.clientID,
clientSecret: config.google.clientSecret,
callbackURL: config.google.callbackURL
},
function(accessToken, refreshToken, profile, done) {
// see http://www.passportjs.org/docs/profile/
if (config.debug === true) {
done(null, profile); // bypass when debugging
} else {
User.findOrCreate({ googleId: profile.id }, function (err, user) {
return done(err, user);
});
}
}
));
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
// export the auth method and the middleware
module.exports = {
middleware: function( req, res, next){
// pass the call to the middlewares
// well, this is dirty, i think there's a better way to call more middleware
passport_initialize_middleware( req, res, function(){
passport_session_middleware( req, res, next);
});
},
authenticate: passport.authenticate.bind(passport)
}