-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth.js
72 lines (59 loc) · 1.84 KB
/
oauth.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module.exports = init;
function init(app) {
var pkginfo = require('./package');
var passport = require('passport');
app.use(passport.initialize());
app.use(passport.session());
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy
passport.serializeUser(function(user, done) {
//console.log("serializeUser : ");
//console.dir(user);
//console.log(done.toString());
done(null, user);
});
passport.deserializeUser(function(obj, done) {
//console.log("disserializeUser : ");
//console.dir(obj);
//console.log(done.toString());
done(null, obj);
});
passport.use(new GoogleStrategy({
clientID : pkginfo.oauth.google.GOOGLE_APP_ID,
clientSecret : pkginfo.oauth.google.GOOGLE_APP_SECRET,
callbackURL : pkginfo.oauth.google.callbackURL
}, function(accessToken, refreshToken, profile, done) {
//
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// req.session.passport 정보를 저장하는 단계이다.
// done 메소드에 전달된 정보가 세션에 저장된다.
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
console.log('callback : ');
console.log('accessToken : ');
console.dir(accessToken);
console.log('refreshToken : ');
console.dir(refreshToken);
console.log('profile : ');
console.dir(profile);
var userId = profile.id;
//
return done(null, userId);
}));
app.get('/auth/google', passport.authenticate('google', {
scope: [
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email'
]
}));
app.get('/auth/google/callback', passport.authenticate('google', {
successRedirect : '/',
failureRedirect : '/'
}));
app.get('/logout', function(req, res) {
//
// passport 에서 지원하는 logout 메소드이다.
// req.session.passport 의 정보를 삭제한다.
//
req.logout();
res.redirect('/');
});
}