From b92f3bfea63edd1c5243a71bb72efce4f8bcd4c2 Mon Sep 17 00:00:00 2001 From: Rvanderheyde Date: Tue, 3 Feb 2015 15:43:04 -0500 Subject: [PATCH 01/11] stuff --- classes/class4/hw/app.js | 35 ++++++++++++++ classes/class4/hw/models/ingredientModel.js | 9 ++++ classes/class4/hw/package.json | 18 +++++++ classes/class4/hw/public/javascripts/main.js | 38 +++++++++++++++ classes/class4/hw/routes/ingredients.js | 47 +++++++++++++++++++ classes/class4/hw/views/home.handlebars | 4 ++ .../class4/hw/views/ingredients.handlebars | 24 ++++++++++ .../class4/hw/views/layouts/main.handlebars | 11 +++++ 8 files changed, 186 insertions(+) create mode 100644 classes/class4/hw/app.js create mode 100644 classes/class4/hw/models/ingredientModel.js create mode 100644 classes/class4/hw/package.json create mode 100644 classes/class4/hw/public/javascripts/main.js create mode 100644 classes/class4/hw/routes/ingredients.js create mode 100644 classes/class4/hw/views/home.handlebars create mode 100644 classes/class4/hw/views/ingredients.handlebars create mode 100644 classes/class4/hw/views/layouts/main.handlebars diff --git a/classes/class4/hw/app.js b/classes/class4/hw/app.js new file mode 100644 index 00000000..27d72cf3 --- /dev/null +++ b/classes/class4/hw/app.js @@ -0,0 +1,35 @@ +var mongoose = require('mongoose'); +var path = require('path'); +var express = require('express'); +var logger = require('morgan'); +var exphbs = require('express-handlebars'); +var bodyParser = require('body-parser'); +var ingredients = require('./routes/ingredients') +// var ingredient = require('./models/ingredientModel') + +var app = express(); +app.engine('handlebars', exphbs({defaultLayout: 'main'})); +app.set('view engine', 'handlebars'); + +var mongoURI = process.env.MONGOURI || "mongodb://localhost/test"; +var PORT = process.env.PORT || 3000; +mongoose.connect(mongoURI); + + +app.use(logger('dev')); +app.use(bodyParser.json()); +app.use(bodyParser.urlencoded({ extended: false })); +app.use(express.static(path.join(__dirname, 'public'))); + +app.get('/', ingredients.addIngredients); +app.post('/newIngredient', ingredients.addIngredientsPOST); +app.get('/ingredients', ingredients.ingredientsRender); +app.post('/edit', ingredients.editIngredients); +app.post('/outOfStock', ingredients.outOfStock) +app.get('/order'); +app.get('/kitchen'); + + +app.listen(PORT, function() { + console.log("Application running on port:", PORT); +}); \ No newline at end of file diff --git a/classes/class4/hw/models/ingredientModel.js b/classes/class4/hw/models/ingredientModel.js new file mode 100644 index 00000000..f2042815 --- /dev/null +++ b/classes/class4/hw/models/ingredientModel.js @@ -0,0 +1,9 @@ +var mongoose = require('mongoose'); + +var ingredientSchema = mongoose.Schema({ + name: String, + price: Number, + available: String +}); + +module.exports = ingredientSchema; \ No newline at end of file diff --git a/classes/class4/hw/package.json b/classes/class4/hw/package.json new file mode 100644 index 00000000..5d2ff156 --- /dev/null +++ b/classes/class4/hw/package.json @@ -0,0 +1,18 @@ +{ + "name": "hw", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "body-parser": "^1.11.0", + "express": "^4.11.1", + "express-handlebars": "^1.1.0", + "mongoose": "^3.8.22", + "morgan": "^1.5.1" + } +} diff --git a/classes/class4/hw/public/javascripts/main.js b/classes/class4/hw/public/javascripts/main.js new file mode 100644 index 00000000..b666173d --- /dev/null +++ b/classes/class4/hw/public/javascripts/main.js @@ -0,0 +1,38 @@ +var $form = $("#ajax-form"); + +var onSuccess = function(data, status){ + console.log('success'); + console.log(data); + var out = "
  • "+data.name+"
  • " + + "" + + "
    " + + "" + + "
    " + + "
    " + + "Name:
    " + + "Price:
    " + + "" + + "
    "; + $("#result").html(out); +}; + +var onError = function(data, status){ + console.log("status",status); + console.log("error",data); +}; + +$form.submit(function(event){ + event.preventDefault(); + var name = $form.find("[name='name']").val(); + var price = $form.find("[name='price']").val(); + + $.post("newIngredient", { + name: name, + price: price, + available: "In Stock" + }) + .done(onSuccess) + .error(onError); +}); \ No newline at end of file diff --git a/classes/class4/hw/routes/ingredients.js b/classes/class4/hw/routes/ingredients.js new file mode 100644 index 00000000..48079ea9 --- /dev/null +++ b/classes/class4/hw/routes/ingredients.js @@ -0,0 +1,47 @@ +var routes = {}; +var mongoose = require('mongoose'); + +var ingredientSchema = mongoose.Schema({ + name: String, + price: Number, + available: String +}); +var Ingredient = mongoose.model('Ingredient', ingredientSchema); + +routes.ingredientsRender = function(req, res){ + Ingredient.find({available: 'In Stock'},function(err, ingredients){ + res.render('ingredients', {'ingredients': ingredients}); + console.log(ingredients); + }); +}; + +routes.addIngredients = function(req,res){ + res.render('home') +}; + +routes.addIngredientsPOST = function(req, res){ + console.log(req.body); + res.send(makeIngredient(req.body)); +}; + +var makeIngredient = function(params){ + var ingredient = new Ingredient({name: params.name, + price: params.price, + available: params.available + }); + ingredient.save(function(err){ + if (err){ + console.log("Problem adding Ingredient", err); + } + }); +} + +routes.editIngredients = function(req, res){ + +}; + +routes.outOfStock = function(req, res){ + +}; + +module.exports = routes; \ No newline at end of file diff --git a/classes/class4/hw/views/home.handlebars b/classes/class4/hw/views/home.handlebars new file mode 100644 index 00000000..4ed644ee --- /dev/null +++ b/classes/class4/hw/views/home.handlebars @@ -0,0 +1,4 @@ +

    Jessica's Burgers

    +Ingredients +Order +Kitchen diff --git a/classes/class4/hw/views/ingredients.handlebars b/classes/class4/hw/views/ingredients.handlebars new file mode 100644 index 00000000..a1e91ba6 --- /dev/null +++ b/classes/class4/hw/views/ingredients.handlebars @@ -0,0 +1,24 @@ +

    Ingredient List

    +
    + Name:
    + Price:
    + +
    + \ No newline at end of file diff --git a/classes/class4/hw/views/layouts/main.handlebars b/classes/class4/hw/views/layouts/main.handlebars new file mode 100644 index 00000000..fa99941b --- /dev/null +++ b/classes/class4/hw/views/layouts/main.handlebars @@ -0,0 +1,11 @@ + + + + + Jess's Burgers + + + + {{{body}}} + + \ No newline at end of file From 3b6c96cfe5af5edafa3237a78349d27f8fd7ac01 Mon Sep 17 00:00:00 2001 From: Rvanderheyde Date: Tue, 3 Feb 2015 16:55:57 -0500 Subject: [PATCH 02/11] adding redisgned ingredients page --- classes/class4/hw/app.js | 4 ++-- classes/class4/hw/public/javascripts/main.js | 14 ++++---------- classes/class4/hw/routes/ingredients.js | 11 +++++++---- classes/class4/hw/views/ingredients.handlebars | 14 ++++---------- 4 files changed, 17 insertions(+), 26 deletions(-) diff --git a/classes/class4/hw/app.js b/classes/class4/hw/app.js index 27d72cf3..82c03727 100644 --- a/classes/class4/hw/app.js +++ b/classes/class4/hw/app.js @@ -24,8 +24,8 @@ app.use(express.static(path.join(__dirname, 'public'))); app.get('/', ingredients.addIngredients); app.post('/newIngredient', ingredients.addIngredientsPOST); app.get('/ingredients', ingredients.ingredientsRender); -app.post('/edit', ingredients.editIngredients); -app.post('/outOfStock', ingredients.outOfStock) +app.post('/edit', ingredients.editIngredientsPOST); +app.post('/outOfStock', ingredients.outOfStockPOST) app.get('/order'); app.get('/kitchen'); diff --git a/classes/class4/hw/public/javascripts/main.js b/classes/class4/hw/public/javascripts/main.js index b666173d..98199eb3 100644 --- a/classes/class4/hw/public/javascripts/main.js +++ b/classes/class4/hw/public/javascripts/main.js @@ -3,17 +3,11 @@ var $form = $("#ajax-form"); var onSuccess = function(data, status){ console.log('success'); console.log(data); - var out = "
  • "+data.name+"
  • " + - "" + - "
    " + + var out = "" + + "
    " + + "
    "+ + ""+ "" + - "
    " + - "
    " + - "Name:
    " + - "Price:
    " + - "" + "
    "; $("#result").html(out); }; diff --git a/classes/class4/hw/routes/ingredients.js b/classes/class4/hw/routes/ingredients.js index 48079ea9..231212da 100644 --- a/classes/class4/hw/routes/ingredients.js +++ b/classes/class4/hw/routes/ingredients.js @@ -34,14 +34,17 @@ var makeIngredient = function(params){ console.log("Problem adding Ingredient", err); } }); + return ingredient } -routes.editIngredients = function(req, res){ - +routes.editIngredientsPOST = function(req, res){ + console.log(req.body); + res.end(); }; -routes.outOfStock = function(req, res){ - +routes.outOfStockPOST = function(req, res){ + console.log(req.body); + res.end(); }; module.exports = routes; \ No newline at end of file diff --git a/classes/class4/hw/views/ingredients.handlebars b/classes/class4/hw/views/ingredients.handlebars index a1e91ba6..74296bc5 100644 --- a/classes/class4/hw/views/ingredients.handlebars +++ b/classes/class4/hw/views/ingredients.handlebars @@ -6,18 +6,12 @@ diff --git a/classes/class6/hw/views/login.handlebars b/classes/class6/hw/views/login.handlebars index 9e600fab..35f803b1 100644 --- a/classes/class6/hw/views/login.handlebars +++ b/classes/class6/hw/views/login.handlebars @@ -1,8 +1,12 @@
    diff --git a/classes/class6/hw/views/profile.handlebars b/classes/class6/hw/views/profile.handlebars index 1b0145d3..ddb06a34 100644 --- a/classes/class6/hw/views/profile.handlebars +++ b/classes/class6/hw/views/profile.handlebars @@ -1,8 +1,9 @@
    @@ -17,7 +18,7 @@

    Profile

    - + @@ -25,10 +26,10 @@
      {{#each twit}} -
      +
    • {{this.text}}
    • - +
      {{/each}} From 4762b0d0504d224d696a8f91e960959617bb1f82 Mon Sep 17 00:00:00 2001 From: Rvanderheyde Date: Tue, 17 Feb 2015 13:55:10 -0500 Subject: [PATCH 10/11] some Oauth stuff, and fixing bugs because of the change and some styling --- classes/class6/hw/app.js | 50 ++++++++++++++++++- classes/class6/hw/package.json | 22 ++++++++ classes/class6/hw/public/css/main.css | 3 ++ .../class6/hw/public/javascripts/newTwit.js | 2 +- classes/class6/hw/routes/auth.js | 30 +++++++++++ classes/class6/hw/routes/home.js | 24 ++++++--- classes/class6/hw/routes/login.js | 8 +-- classes/class6/hw/routes/profile.js | 18 ++++--- classes/class6/hw/views/login.handlebars | 1 + classes/class6/hw/views/profile.handlebars | 2 +- 10 files changed, 139 insertions(+), 21 deletions(-) create mode 100644 classes/class6/hw/package.json create mode 100644 classes/class6/hw/routes/auth.js diff --git a/classes/class6/hw/app.js b/classes/class6/hw/app.js index 8515c5ca..80954c90 100644 --- a/classes/class6/hw/app.js +++ b/classes/class6/hw/app.js @@ -1,3 +1,4 @@ +var config = require('./oauth.js'); var mongoose = require('mongoose'); var path = require('path'); var express = require('express'); @@ -5,10 +6,13 @@ var logger = require('morgan'); var exphbs = require('express-handlebars'); var bodyParser = require('body-parser'); var cookieParser = require('cookie-parser'); -var session = require('express-session') +var session = require('express-session'); +var passport = require('passport'); +var FacebookStrategy = require('passport-facebook').Strategy; var home = require('./routes/home'); var login = require('./routes/login'); var profile = require('./routes/profile'); +var auth = require('./routes/auth'); var app = express(); app.engine('handlebars', exphbs({defaultLayout: 'main'})); @@ -18,6 +22,27 @@ var mongoURI = process.env.MONGOURI || "mongodb://localhost/test"; var PORT = process.env.PORT || 3000; mongoose.connect(mongoURI); +// serialize and deserialize +passport.serializeUser(function(user, done) { +done(null, user); +}); +passport.deserializeUser(function(obj, done) { +done(null, obj); +}); + +// config +passport.use(new FacebookStrategy({ + clientID: config.facebook.clientID, + clientSecret: config.facebook.clientSecret, + callbackURL: config.facebook.callbackURL +}, +function(accessToken, refreshToken, profile, done) { + process.nextTick(function () { + return done(null, profile); + }); +} +)); + app.use(logger('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); @@ -28,6 +53,8 @@ app.use(session({ resave: false, saveUninitialized: true })); +app.use(passport.initialize()); +app.use(passport.session()); app.get('/', home.homeRender); app.post('/homeTwit', home.postTwit); @@ -38,6 +65,25 @@ app.get('/profile', profile.profileRender); app.post('/deleteTwit', profile.deleteTwit); app.post('/profilePost', profile.profilePost); +app.get('/auth/facebook',passport.authenticate('facebook'), auth.fbAuth); +app.get('/auth/facebook/callback',passport.authenticate('facebook', { failureRedirect: '/login' }), auth.fbAuthCallback); + +// app.get('/auth/facebook', +// passport.authenticate('facebook'), +// function(req, res){ +// }); +// app.get('/auth/facebook/callback', +// passport.authenticate('facebook', { failureRedirect: '/' }), +// function(req, res) { +// res.redirect('/'); +// }); +// app.get('/auth/facebook', ) + app.listen(PORT, function() { console.log("Application running on port:", PORT); -}); \ No newline at end of file +}); + +function ensureAuthenticated(req, res, next) { +if (req.isAuthenticated()) { return next(); } +res.redirect('/') +} \ No newline at end of file diff --git a/classes/class6/hw/package.json b/classes/class6/hw/package.json new file mode 100644 index 00000000..8f776650 --- /dev/null +++ b/classes/class6/hw/package.json @@ -0,0 +1,22 @@ +{ + "name": "hw", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "body-parser": "^1.11.0", + "cookie-parser": "^1.3.3", + "express": "^4.11.2", + "express-handlebars": "^1.1.0", + "express-session": "^1.10.2", + "mongoose": "^3.8.23", + "morgan": "^1.5.1", + "passport": "^0.2.1", + "passport-facebook": "^1.0.3" + } +} diff --git a/classes/class6/hw/public/css/main.css b/classes/class6/hw/public/css/main.css index bbd84717..409262c7 100644 --- a/classes/class6/hw/public/css/main.css +++ b/classes/class6/hw/public/css/main.css @@ -103,3 +103,6 @@ } +ul{ + list-style-type: none; +} \ No newline at end of file diff --git a/classes/class6/hw/public/javascripts/newTwit.js b/classes/class6/hw/public/javascripts/newTwit.js index e0223f09..48de5958 100644 --- a/classes/class6/hw/public/javascripts/newTwit.js +++ b/classes/class6/hw/public/javascripts/newTwit.js @@ -11,8 +11,8 @@ $btn.click(function btnClick(event){ var onSuccess = function(data, status){ var out = "
    • "+ - "
      "+data.username+"
      "+ "
      "+data.text+"
      "+ + "
      "+data.username+"
      "+ "
    • "; $("#result").html(out); }; diff --git a/classes/class6/hw/routes/auth.js b/classes/class6/hw/routes/auth.js new file mode 100644 index 00000000..b4891a93 --- /dev/null +++ b/classes/class6/hw/routes/auth.js @@ -0,0 +1,30 @@ +var passport = require('passport'); +var FacebookStrategy = require('passport-facebook').Strategy; +var User = require('../models/user'); +var routes = {}; + +routes.fbAuth = function(req, res){ +}; + +routes.fbAuthCallback = function(req, res) { + var usersName = req.session.passport.user.displayName + User.findOne({username: usersName}, function(error, user){ + if(error){ + console.log(error); + }; + if(user){ + console.log('Returning User', user) + req.session.username = user.username; + res.redirect('/'); + } else { + console.log('New User') + var newUser = new User({username: usersName, twits: []}); + console.log(newUser) + newUser.save(); + req.session.username = usersName; + res.redirect('/'); + }; + }); +}; + +module.exports = routes; \ No newline at end of file diff --git a/classes/class6/hw/routes/home.js b/classes/class6/hw/routes/home.js index e271d19e..818cf224 100644 --- a/classes/class6/hw/routes/home.js +++ b/classes/class6/hw/routes/home.js @@ -26,9 +26,9 @@ routes.homeRender = function(req,res){ // var userList = users.map(function(val){ // return val.username; // }); - if(req.session.username){ - console.log(twits); - res.render('home', {'twit': twits, 'user': users, 'login': req.session.username}); + console.log(req.session.passport); + if(!emptyObjTest(req.session.passport)){ + res.render('home', {'twit': twits, 'user': users, 'login': req.session.passport.user.displayName}); } else { console.log(twits); res.render('home', {'twit': twits, 'user': users}); @@ -36,11 +36,21 @@ routes.homeRender = function(req,res){ }); }; +function emptyObjTest(obj){ + // for (var prop in obj){ + // if(obj.hasOwnProperty(prop)){ + // return false; + // } + // } + // return true; + + return Object.keys(obj).length === 0; +} routes.postTwit = function(req,res){ //Handles the new twit form - if(req.session.username){ - User.findOne({username: req.session.username}, function(err, user){ + if(!emptyObjTest(req.session.passport)){ + User.findOne({username: req.session.passport.user.displayName}, function(err, user){ var d = new Date(); var time = d.getTime(); var twitObj = { @@ -53,7 +63,7 @@ routes.postTwit = function(req,res){ console.log(user.twits); var out = ({ text: req.body.text, - username: req.session.username + username: req.session.passport.user.displayName }); res.send(out); }) @@ -64,7 +74,7 @@ routes.postTwit = function(req,res){ routes.logoutUser = function(req,res){ User.find({},function(err,users){ - req.session.username = ''; + req.session.passport = {}; var twits = []; console.log(users.length); for(var i=0;i + Login with Facebook
    \ No newline at end of file diff --git a/classes/class6/hw/views/profile.handlebars b/classes/class6/hw/views/profile.handlebars index ddb06a34..7302dee5 100644 --- a/classes/class6/hw/views/profile.handlebars +++ b/classes/class6/hw/views/profile.handlebars @@ -27,8 +27,8 @@
    {{#each twit}}
    -
  • {{this.text}}
  • +
    From 416d6f6ef5ba45f5de236da45b461113d78e4547 Mon Sep 17 00:00:00 2001 From: Rvanderheyde Date: Tue, 17 Feb 2015 20:00:04 -0500 Subject: [PATCH 11/11] Heroku stuff, and some styling --- classes/class6/hw/app.js | 10 +++--- classes/class6/hw/public/css/main.css | 41 +++++++++++++++++++++++-- classes/class6/hw/routes/home.js | 7 ----- classes/class6/hw/views/home.handlebars | 10 +++--- 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/classes/class6/hw/app.js b/classes/class6/hw/app.js index 80954c90..65fca9bd 100644 --- a/classes/class6/hw/app.js +++ b/classes/class6/hw/app.js @@ -1,4 +1,3 @@ -var config = require('./oauth.js'); var mongoose = require('mongoose'); var path = require('path'); var express = require('express'); @@ -20,6 +19,9 @@ app.set('view engine', 'handlebars'); var mongoURI = process.env.MONGOURI || "mongodb://localhost/test"; var PORT = process.env.PORT || 3000; +var CLIENTID= process.env.CLIENTID || require('./oauth.js').facebook.clientID; +var CLIENTSECRET = process.env.CLIENTSECRET || require('./oauth.js').facebook.clientSecret; +var CALLBACKURL = process.env.CALLBACKURL || require('./oauth.js').facebook.callbackURL; mongoose.connect(mongoURI); // serialize and deserialize @@ -32,9 +34,9 @@ done(null, obj); // config passport.use(new FacebookStrategy({ - clientID: config.facebook.clientID, - clientSecret: config.facebook.clientSecret, - callbackURL: config.facebook.callbackURL + clientID: CLIENTID, + clientSecret: CLIENTSECRET, + callbackURL: CALLBACKURL }, function(accessToken, refreshToken, profile, done) { process.nextTick(function () { diff --git a/classes/class6/hw/public/css/main.css b/classes/class6/hw/public/css/main.css index 409262c7..86e0da13 100644 --- a/classes/class6/hw/public/css/main.css +++ b/classes/class6/hw/public/css/main.css @@ -90,19 +90,56 @@ width: 33%; height: 100vh; } + .columnOneThird h3{ margin: 0; +} +.input-group{ + width: 100%; + background: hsl(0, 0%, 100%); } .btn{ - + display: inline; + background: -webkit-linear-gradient(top, hsl(100, 90%, 60%), hsl(100, 90%, 40%)); + box-shadow: none; + font-weight: bold; + cursor: pointer; + transition-duration: .5s; + transition-property: background-color,box-shadow; + height:30px; + width: 60px; + z-index: 1; + padding: 0; + outline: none; + margin: 0; + border: 0; + border-bottom-left-radius:0; + border-top-left-radius: 0; } .textWell{ + width: 75%; + height: 30px; + margin: 0; + /*border: 0;*/ + padding: 0; +} +.tweet{ + width: 100%; + background: -webkit-linear-gradient(top, hsl(100, 90%, 90%), hsl(100, 90%, 70%)); + padding-left: 0; + padding-bottom: 5px; +} + +.messages{ + list-style-type: none; + padding-left: 0; } -ul{ +.users{ list-style-type: none; + padding-left: 0; } \ No newline at end of file diff --git a/classes/class6/hw/routes/home.js b/classes/class6/hw/routes/home.js index 818cf224..44adf734 100644 --- a/classes/class6/hw/routes/home.js +++ b/classes/class6/hw/routes/home.js @@ -37,13 +37,6 @@ routes.homeRender = function(req,res){ }; function emptyObjTest(obj){ - // for (var prop in obj){ - // if(obj.hasOwnProperty(prop)){ - // return false; - // } - // } - // return true; - return Object.keys(obj).length === 0; } diff --git a/classes/class6/hw/views/home.handlebars b/classes/class6/hw/views/home.handlebars index dc87116e..b9230cd2 100644 --- a/classes/class6/hw/views/home.handlebars +++ b/classes/class6/hw/views/home.handlebars @@ -22,24 +22,26 @@
    + - - +
    -
      +
        {{#each twit}} +
      • {{this.text}}
        {{this.author}}
      • +
        {{/each}}

    Users

    -
      +
        {{#each user}}
      • {{this.username}}
      • {{/each}}