Skip to content

Commit

Permalink
actually added everything
Browse files Browse the repository at this point in the history
  • Loading branch information
avarun42 committed Jan 18, 2015
1 parent 16c36ed commit 03ca65b
Show file tree
Hide file tree
Showing 67 changed files with 96 additions and 6,114 deletions.
51 changes: 34 additions & 17 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ var passportConf = require('./config/passport');
* Connect to MongoDB.
*/

// mongoose.connect(secrets.db);
// mongoose.connection.on('error', function() {
// console.error('MongoDB Connection Error. Make sure MongoDB is running.');
// });
// var User = require('./models/User');
mongoose.connect(secrets.db);
mongoose.connection.on('error', function() {
console.error('MongoDB Connection Error. Make sure MongoDB is running.');
});
var User = require('./models/User');

app.set("ipaddr", "127.0.0.1");
app.set("port", 8080);
Expand All @@ -36,33 +36,44 @@ app.use(express.static("public", __dirname + "/public"));

app.use(bodyParser.json());

// app.use(session({secret: secrets.sessionSecret, resave: true, saveUninitialized: true}));
app.use(session({secret: secrets.sessionSecret, resave: true, saveUninitialized: true}));
// Initialize Passport! Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(passport.initialize());
app.use(passport.session());

//route homepage
app.get("/", function(req, res) {
res.render("index");
app.get("/", ensureAuthenticated, function(req, res) {
res.render("index", { user: req.user });
});

app.get("/login", function(req, res) {
res.render("login");
});

app.get("/register", function(req, res) {
app.get('/logout', function(req, res){
req.logout();
res.redirect('/login');
});

app.get("/register", ensureAuthenticated, function(req, res) {
res.render("register");
});

app.post("/register", ensureAuthenticated, function(req, res) {
console.log(req.body);
// req.user.patient = req.body.patient;
res.redirect('/');
});

// GET /auth/facebook
// Use passport.authenticate() as route middleware to authenticate the
// request. The first step in Facebook authentication will involve
// redirecting the user to facebook.com. After authorization, Facebook
// will redirect the user back to this application at
// /auth/facebook/callback
app.get('/auth/facebook',
passport.authenticate('facebook', { scope: ['email'] }),
passport.authenticate('facebook', { scope: ['email', 'public_profile'] }),
function(req, res){
// The request will be redirected to Facebook for authentication, so
// this function will not be called.
Expand All @@ -81,23 +92,19 @@ app.get('/auth/facebook/callback',

//Same thing for Google
app.get('/auth/google',
passport.authenticate('google'),
passport.authenticate('google', { scope: ['https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email'] }),
function(req, res){
// The request will be redirected to Google for authentication, so
// this function will not be called.
});

app.get('/auth/google/return',
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});

app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});

// //route boards
// app.get("/board", function(req, res) {
// res.render("board", { user: req.user });
Expand All @@ -112,6 +119,16 @@ app.get('/logout', function(req, res){
// res.render("hub", { user: req.user });
// });

// Simple route middleware to ensure user is authenticated.
// Use this route middleware on any resource that needs to be protected. If
// the request is authenticated (typically via a persistent login session),
// the request will proceed. Otherwise, the user will be redirected to the
// login page.
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login');
}

http.listen(app.get("port"), app.get("ipaddr"), function() {
console.log("Server up and running. Go to http://" + app.get("ipaddr") + ":" + app.get("port"));
});
115 changes: 57 additions & 58 deletions config/passport.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy; //Facebook
var GoogleStrategy = require('passport-google').Strategy; //Google
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy; //Google
var User = require('../models/User');
var secrets = require('./secrets');
var domain = "http://pillbox.ngrok.com";

// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
Expand All @@ -26,71 +27,69 @@ passport.deserializeUser(function(id, done) {
passport.use(new FacebookStrategy({
clientID: secrets.facebook.clientID,
clientSecret: secrets.facebook.clientSecret,
callbackURL: "localhost:8080/auth/facebook/callback"
callbackURL: domain + "/auth/facebook/callback"
},
function(accessToken, refreshToken, profile, done) {
User.findOrCreate({ facebookId: profile.id }, function(err, user) {
if (err) {
return done(err);
}

done(null, user);
User.findOne({
id: profile.id
}, function(err, user) {
if (err) {
return done(err);
}
//No user was found... so create a new user with values from Facebook (all the profile. stuff)
if (!user) {
// console.log(profile);
user = new User({
id: profile.id,
name: profile.displayName,
email: profile.emails[0].value,
provider: profile.provider,
//now in the future searching on User.findOne({'facebook.id': profile.id } will match because of this next line
facebook: profile._json
});
user.save(function(err) {
if (err) console.log(err);
return done(err, user);
});
} else {
//found user. Return
return done(err, user);
}
});
}
));

passport.use(new GoogleStrategy({
returnURL: 'localhost:8080/auth/google/return',
realm: 'localhost:8080/'
clientID: secrets.google.clientID,
clientSecret: secrets.google.clientSecret,
callbackURL: domain + "/auth/google/callback"
},
function(identifier, profile, done) {
User.findOrCreate({ openId: identifier }, function(err, user) {
done(err, user);
function(accessToken, refreshToken, profile, done) {
User.findOne({
id: profile.id
}, function(err, user) {
if (err) {
return done(err);
}
//No user was found... so create a new user with values from Google (all the profile. stuff)
if (!user) {
console.log(profile);
user = new User({
id: profile.id,
name: profile.displayName,
email: profile.emails[0].value,
provider: profile.provider,
//now in the future searching on User.findOne({'google.id': profile.id } will match because of this next line
google: profile._json
});
user.save(function(err) {
if (err) console.log(err);
return done(err, user);
});
} else {
//found user. Return
return done(err, user);
}
});
}
));

// passport.use(new WindowsLiveStrategy({
// clientID: secrets.microsoft.clientID,
// clientSecret: secrets.microsoft.clientSecret,
// callbackURL: "http://winger.ngrok.com/auth/windowslive/callback"
// },
// function(accessToken, refreshToken, profile, done) {
// User.findOne({
// id: profile.id
// }, function(err, user) {
// if (err) {
// return done(err);
// }
// //No user was found... so create a new user with values from Facebook (all the profile. stuff)
// if (!user) {
// console.log(profile.photos[0].value);
// user = new User({
// id: profile.id,
// name: profile.displayName,
// photo: profile.photos[0].value,
// provider: 'microsoft',
// //now in the future searching on User.findOne({'facebook.id': profile.id } will match because of this next line
// microsoft: profile._json
// });
// user.save(function(err) {
// if (err) console.log(err);
// return done(err, user);
// });
// } else {
// //found user. Return
// return done(err, user);
// }
// });
// }
// ));

// Simple route middleware to ensure user is authenticated.
// Use this route middleware on any resource that needs to be protected. If
// the request is authenticated (typically via a persistent login session),
// the request will proceed. Otherwise, the user will be redirected to the
// login page.
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/');
}
6 changes: 4 additions & 2 deletions models/User.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
var mongoose = require('mongoose');

var userSchema = new mongoose.Schema({
photo: String,
// photo: String,
id: String,
email: { type: String, default: '' },

provider: String,
facebook: String,
google: String,
tokens: Array,
name: { type: String, default: '' }
name: { type: String, default: '' },
patient: String,
pillbox: { type: Array, default: [false, false, false, false, false, false, false] }

// profile: {
// gender: { type: String, default: '' },
Expand Down
4 changes: 0 additions & 4 deletions node_modules/passport-google/.travis.yml

This file was deleted.

20 changes: 0 additions & 20 deletions node_modules/passport-google/LICENSE

This file was deleted.

94 changes: 0 additions & 94 deletions node_modules/passport-google/README.md

This file was deleted.

Loading

0 comments on commit 03ca65b

Please sign in to comment.