-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.js
143 lines (136 loc) · 5.34 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
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
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var FacebookStrategy = require('passport-facebook').Strategy;
var GoogleStrategy = require('passport-google-oauth20').Strategy;
var TwitterStrategy = require('passport-twitter').Strategy;
var Customer = require('./models/customer');
var Organizer = require('./models/organizer');
passport.use('cust-local', new LocalStrategy(Customer.authenticate()));
passport.use('org-local', new LocalStrategy(Organizer.authenticate()));
passport.use('cust-face', new FacebookStrategy({
clientID: process.env.FB_KEY,
clientSecret: process.env.FB_SECRET,
callbackURL: process.env.SERVER_URL + "/auth/facebook/callback",
profileFields: ['id', 'displayName', 'photos', 'email'],
},
function(accessToken, refreshToken, profile, done) {
//check Customer table for anyone with a facebook ID of profile.id
Customer.findOne({
'facebookId': profile.id
}, function(err, customer) {
if (err) {
return done(err);
}
//No Customer was found... so create a new Customer with values from Facebook (all the profile. stuff)
if (!customer) {
customer = new Customer({
facebookId: profile.id,
username: profile.id,
display_name: profile.displayName,
email: profile.email,
imageUrl: profile.photos[0].value //needs testing
});
customer.save(function(err) {
return done(err, customer);
});
} else {
//found Customer. Return
return done(err, customer);
}
});
}
));
// Use the GoogleStrategy within Passport.
// Strategies in passport require a `verify` function, which accept
// credentials (in this case, a token, tokenSecret, and Google profile), and
// invoke a callback with a user object.
passport.use('cust-google', new GoogleStrategy({
clientID: process.env.GOOGLE_KEY,
clientSecret: process.env.GOOGLE_SECRET,
callbackURL: process.env.SERVER_URL + "/auth/google/callback"
},
function(token, tokenSecret, profile, done) {
//check Customer table for anyone with a google ID of profile.id
Customer.findOne({
'googleId': profile.id
}, function(err, customer) {
if (err) {
return done(err);
}
//No Customer was found... so create a new Customer with values from google (all the profile. stuff)
if (!customer) {
customer = new Customer({
googleId: profile.id,
username: profile.id,
display_name: profile.displayName,
email: profile.email,
imageUrl: profile.photos[0].value //needs testing
});
customer.save(function(err) {
return done(err, customer);
});
} else {
//found Customer. Return
return done(err, customer);
}
});
}
));
passport.use('cust-twitter', new TwitterStrategy({
consumerKey: process.env.TWITTER_KEY,
consumerSecret: process.env.TWITTER_SECRET,
callbackURL: process.env.SERVER_URL + "/auth/twitter/callback"
},
function(token, tokenSecret, profile, done) {
//check Customer table for anyone with a twitter ID of profile.id
Customer.findOne({
'twitterId': profile.id
}, function(err, customer) {
if (err) {
return done(err);
}
//No Customer was found... so create a new Customer with values from twitter (all the profile. stuff)
if (!customer) {
customer = new Customer({
twitterId: profile.id,
username: profile.id,
display_name: profile.displayName,
email: profile.email,
imageUrl: profile.photos[0].value //needs testing
});
customer.save(function(err) {
return done(err, customer);
});
} else {
//found Customer. Return
return done(err, customer);
}
});
}
));
function SessionConstructor(userId, userGroup) {
this.userId = userId;
this.userGroup = userGroup;
}
passport.serializeUser(function(userObject, done) {
let userGroup = "cust";
let userPrototype = Object.getPrototypeOf(userObject);
if (userPrototype === Customer.prototype) {
userGroup = "cust";
} else if (userPrototype === Organizer.prototype) {
userGroup = "org";
}
let sessionConstructor = new SessionConstructor(userObject.id, userGroup);
done(null, sessionConstructor);
});
passport.deserializeUser(function(sessionConstructor, done) {
if (sessionConstructor.userGroup == 'cust') {
Customer.findOne({ _id: sessionConstructor.userId }, function(err, user) {
done(err, user);
});
} else if (sessionConstructor.userGroup == 'org') {
Organizer.findOne({ _id: sessionConstructor.userId }, function(err, user) {
done(err, user);
});
}
});