app.js
2.28 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var express = require('express')
, passport = require('passport')
, session = require('express-session')
, NaverStrategy = require('../lib/index.js').Strategy;
// @todo Use single `var` keyword?
var client_id = '************ your app client id ************';
var client_secret = '************ your app client secret ************';
var callback_url = '************ your app callback url ************';
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
passport.use(new NaverStrategy({
clientID: client_id,
clientSecret: client_secret,
callbackURL: callback_url
// @todo Suggest to use `state` parameter?
}, function(accessToken, refreshToken, profile, done) {
process.nextTick(function () {
// @todo Remove necessary comment
//console.log("profile=");
//console.log(profile);
// data to be saved in DB
user = {
name: profile.displayName,
email: profile.emails[0].value,
username: profile.displayName,
provider: 'naver',
naver: profile._json
};
//console.log("user=");
//console.log(user);
return done(null, profile);
});
}));
var app = express();
app.use(session({secret: 'keyboard cat'}));
app.use(passport.initialize());
app.use(passport.session());
app.set('view engine', 'jade');
app.set('views', __dirname + '/views/');
app.get('/', function(req, res){
res.render('index', { user: req.user });
});
app.get('/account', ensureAuthenticated, function(req, res) {
console.log(req.user);
res.render('account', { user: req.user });
});
app.get('/login', function(req, res){
res.render('login', { user: req.user });
});
// Setting the naver oauth routes
app.get('/auth/naver',
passport.authenticate('naver', null), function(req, res) { // @todo Additional handler is necessary. Remove?
console.log('/auth/naver failed, stopped');
});
// creates an account if no account of the new user
app.get('/auth/naver/callback',
passport.authenticate('naver', {
failureRedirect: '#!/auth/login'
}), function(req, res) {
res.redirect('/');
});
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
app.listen(3000);
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login');
}