-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
213 lines (191 loc) · 7.42 KB
/
server.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const express = require('express')
const passport = require('passport')
const FacebookStrategy = require('passport-facebook').Strategy
const GoogleStrategy = require('passport-google-oauth20').Strategy;
// const LinkedInStrategy = require('passport-linkedin-oauth2').Strategy;
// var GitHubStrategy = require('passport-github').Strategy;
var PinterestStrategy = require('passport-pinterest-oauth').OAuth2Strategy;
const session = require('express-session')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
const CONFIG = require('./APPCONFIG');
const mysql = require('mysql');
const app = express();
//Define MySQL parameter in Config.js file.
const pool = mysql.createPool({
host : CONFIG.MYSQL.host,
user : CONFIG.MYSQL.username,
password : CONFIG.MYSQL.password,
database : CONFIG.MYSQL.database
});
// Passport session setup.
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
passport.use(new PinterestStrategy({
clientID: CONFIG.PINTEREST.clientID,
clientSecret: CONFIG.PINTEREST.clientSecret,
scope: ['read_public', 'read_relationships'],
callbackURL: CONFIG.PINTEREST.callbackURL,
state: true
},
function(accessToken, refreshToken, profile, done) {
console.log('profile', profile);
if(CONFIG.MYSQL.use_database) {
// if sets to true
pool.query("SELECT * from userinfo where userid="+profile.id, (err,rows) => {
if(err) throw err;
if(rows && rows.length === 0) {
console.log("There is no such user, adding now");
pool.query("INSERT into userinfo(userid,username) VALUES('"+profile.id+"','"+profile.displayName+"')");
} else {
console.log("User already exists in database");
}
});
}
return done(null, profile);
// User.findOrCreate({ pinterestId: profile.id }, function (err, user) {
// return done(err, user);
// });
}
));
// Use the FacebookStrategy within Passport.
passport.use(new FacebookStrategy({
clientID: CONFIG.FACEBOOK.clientID,
clientSecret: CONFIG.FACEBOOK.clientSecret ,
callbackURL: CONFIG.FACEBOOK.callbackURL
},
function(accessToken, refreshToken, profile, done) {
process.nextTick(function () {
//Check whether the User exists or not using profile.id
if(CONFIG.MYSQL.use_database) {
// if sets to true
pool.query("SELECT * from userinfo where userid="+profile.id, (err,rows) => {
if(err) throw err;
if(rows && rows.length === 0) {
console.log("There is no such user, adding now");
pool.query("INSERT into userinfo(userid,username) VALUES('"+profile.id+"','"+profile.displayName+"')");
} else {
console.log("User already exists in database");
}
});
}
return done(null, profile);
});
}));
passport.use(new GoogleStrategy({
clientID: CONFIG.GOOGLEAUTH.clientID,
clientSecret: CONFIG.GOOGLEAUTH.clientSecret ,
callbackURL: CONFIG.GOOGLEAUTH.callbackURL
},
function(token, refreshToken, profile, done) {
console.log('prrofile', profile);
process.nextTick(function() {
if(CONFIG.MYSQL.use_database) {
// if sets to true
pool.query("SELECT * from userinfo where userid="+profile.id, (err,rows) => {
if(err) throw err;
if(rows && rows.length === 0) {
console.log("There is no such user, adding now");
pool.query("INSERT into userinfo(userid,username) VALUES('"+profile.id+"','"+profile.displayName+"')");
} else {
console.log("User already exists in database");
}
});
}
return done(null, profile);
});
}));
// passport.use(new LinkedInStrategy({
// clientID: CONFIG.LINKDIN.clientID,
// clientSecret: CONFIG.LINKDIN.clientSecret,
// callbackURL: CONFIG.LINKDIN.callbackURL,
// scope: ['r_emailaddress', 'r_basicprofile'],
// }, function(accessToken, refreshToken, profile, done) {
// // asynchronous verification, for effect...
// process.nextTick(function () {
// if(CONFIG.MYSQL.use_database) {
// // if sets to true
// pool.query("SELECT * from userinfo where userid="+profile.id, (err,rows) => {
// if(err) throw err;
// if(rows && rows.length === 0) {
// console.log("There is no such user, adding now");
// pool.query("INSERT into userinfo(userid,username) VALUES('"+profile.id+"','"+profile.displayName+"')");
// } else {
// console.log("User already exists in database");
// }
// });
// }
// return done(null, profile);
// });
// }));
// passport.use(new GitHubStrategy({
// clientID: CONFIG.GIT_HUB.clientID,
// clientSecret: CONFIG.GIT_HUB.clientSecret,
// callbackURL: CONFIG.GIT_HUB.callbackURL
// },
// function(accessToken, refreshToken, profile, cb) {
// process.nextTick(function () {
// //Check whether the User exists or not using profile.id
// if(CONFIG.MYSQL.use_database) {
// // if sets to true
// pool.query("SELECT * from userinfo where userid="+profile.id, (err,rows) => {
// if(err) throw err;
// if(rows && rows.length === 0) {
// console.log("There is no such user, adding now");
// pool.query("INSERT into userinfo(userid,username) VALUES('"+profile.id+"','"+profile.displayName+"')");
// } else {
// console.log("User already exists in database");
// }
// });
// }
// return done(null, profile);
// });
// }
// ));
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(session({ secret: 'krupalideveloper', key: 'sid'}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res){
res.render('index', { user: req.user });
});
app.get('/account', ensureAuthenticated, function(req, res){
res.render('account', { user: req.user });
});
app.get('/auth/facebook', passport.authenticate('facebook',{scope:'email'}));
app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect : '/', failureRedirect: '/login' }), function(req, res) {
res.redirect('/');
});
app.get('/auth/google', passport.authenticate('google', { scope: ['profile', 'email'] }));
app.get('/auth/google/callback', passport.authenticate('google', { successRedirect: '/', failureRedirect: '/login'}), function(req, res) {
res.redirect('/');
});
// app.get('/auth/linkedin',passport.authenticate('linkedin', { scope: 'email' }));
// app.get('/auth/linkedin/callback', passport.authenticate('linkedin', { successRedirect: '/', failureRedirect: '/login'}), function(req, res){
// res.redirect('/');
// });
// app.get('/auth/github', passport.authenticate('github'));
// app.get('/auth/github/callback', passport.authenticate('github', { failureRedirect: '/login', successRedirect: '/' }), function(req, res) {
// res.redirect('/');
// });
app.get('/auth/pinterest', passport.authenticate('pinterest'));
app.get('/auth/pinterest/callback', passport.authenticate('pinterest', { failureRedirect: '/login' }), function(req, res) {
res.redirect('/');
});
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login')
}
app.listen(8080);