This repository has been archived by the owner on May 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
71 lines (66 loc) · 2.46 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const passport = require("passport");
const Strategy = require("passport-facebook").Strategy;
const config = require("./config");
const User = require('./models/user');
passport.use(new Strategy({
clientID: config.facebook.clientID,
clientSecret: config.facebook.clientSecret,
callbackURL: "https://olympics.nvision.org.in/auth/facebook/callback",
profileFields: ['id', 'displayName', 'name', 'email', 'picture']
}, function (accessToken, refreshToken, profile, cb) {
process.nextTick(function () {
var email;
if (!profile.emails || !profile.emails.length) {
email = profile.id;
} else {
email = profile.emails[0].value;
}
User.findOne({
email: email
}, function (err, user) {
if (err) return cb(err);
else if (user) return cb(null, user);
else {
console.log(profile);
const user = new User();
if (!profile.emails || !profile.emails.length) {
user.id = profile.id;
user.email = profile.id;
} else {
user.id = profile.emails[0].value;
user.email = profile.emails[0].value;
}
user.facebook.id = profile.id;
user.facebook.token = accessToken;
user.displayName = profile.displayName;
// user.email = profile.emails[0].value;
user.picture = profile.photos[0].value;
user.games = [];
user.questions = [];
for (var i=0; i<Questions.length; i++) {
user.questions.push(i);
}
for (var i=0; i<Questions.length; i++) {
var r = Math.floor(Math.random()*Questions.length);
var temp = user.questions[r];
user.questions[r] = user.questions[i];
user.questions[i] = temp;
}
user.save(function (err, user) {
if (err) return cb(err);
else return cb(null, user);
});
}
});
});
}));
passport.serializeUser(function (user, cb) {
return cb(null, user.id);
});
passport.deserializeUser(function (id, cb) {
User.findOne({id:id}, function (err, user) {
if (err) return cb(err);
else return cb(null, user);
});
});
module.exports = passport;