-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticate.js
89 lines (83 loc) · 3.23 KB
/
authenticate.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
const FacebookStrategy = require('passport-facebook').Strategy;
var GoogleStrategy = require('passport-google-oauth20').Strategy;
const passport = require('passport');
const add = require('date-fns/add');
const User = require('./models/user');
const config = require('./config');
const connect = require('./connect');
const { error } = require('firebase-functions/logger');
//configures a Facebook OAuth 2.0 strategy for Passport
passport.use(new FacebookStrategy({
clientID: config.fbClientId,
clientSecret: config.fbClientSecret,
callbackURL: 'https://grandma-8ed4c.web.app/api/users/auth/facebook/callback'
},
async function(accessToken, refreshToken, profile, cb) {
try {
//returns existing user or creates one
const user = await User.findOne({ facebookId: profile.id });
if (user) {
return cb(null, user);
}
const newUser = new User({ facebookId: profile.id, display_name: profile.displayName });
await newUser.save();
return cb(null, newUser);
} catch (err) { return cb(err) }
}
));
passport.use(new GoogleStrategy({
clientID: config.gClientId,
clientSecret: config.gClientSecret,
callbackURL: "https://grandma-8ed4c.web.app/api/users/auth/google/callback"
},
async function(accessToken, refreshToken, profile, cb) {
try {
//returns existing user or creates one
const user = await User.findOne({ googleId: profile.id });
if (user) {
return cb(null, user);
}
console.log("new google user creation in progres...")
const email = profile.emails && profile.emails.length > 0 ? profile.emails[0].value : null;
const newUser = new User({ googleId: profile.id, display_name: profile.displayName, email: email });
await newUser.save();
return cb(null, newUser);
} catch (err) { return cb(err) }
}
));
//when a session is initialized, passport stores the user id in the session doc
passport.serializeUser((user, done) => {
console.log('serializing user...')
return done(null, user._id);
});
//when any request is made during an active session, passport uses the stored user id to extract the associated user doc
passport.deserializeUser(async (id, done) => {
console.log('deserializing user...');
try {
let user = await User.findById(id);
return done(null, user)
} catch (err) {
return done(err)
}
});
//middleware function that checks if user exists
//passport.session() middleware in connect.js provides req.user object
exports.verifyUser = async (req, res, next) => {
console.log('verifying user...');
if (req.user) {
const currentTime = new Date();
const expirationDate = req.session.cookie.expires;
const inOneMonth = add(expirationDate, { months: 1 });
//if the session expires in 1 month or less, add 3 months to the session
if (inOneMonth >= currentTime) {
const inThreeMonths = add(currentTime, { months: 3 });
await connect.extendSession(req.sessionID, inThreeMonths, (err) => {
if (err) { res.status(500).send({ error: err }) }
});
req.session.cookie.expires = inThreeMonths;
req.session.save();
}
return next();
}
else { res.status(400).send({ error: "ERROR, unauthorized" }) }
}