├── .gitignore ├── config ├── database.js └── passport.js ├── README.md ├── package.json ├── views ├── login.ejs ├── cashlogin.ejs ├── connect-local.ejs ├── signup.ejs ├── cashsignup.ejs ├── index.ejs ├── cashier.ejs └── profile.ejs ├── app ├── models │ └── user.js └── routes.js ├── public ├── cashier.css ├── style.css └── main.js ├── npm-debug.log └── server.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | npm-debug.log 3 | .DS_Store 4 | /*.env -------------------------------------------------------------------------------- /config/database.js: -------------------------------------------------------------------------------- 1 | // config/database.js 2 | module.exports = { 3 | 4 | 'url' : 'mongodb://barista:barista1@ds231961.mlab.com:31961/coffee' // looks like mongodb://:@mongo.onmodulus.net:27017/Mikha4ot 5 | 6 | }; 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Barista-Fullstack 2 | This is a full-stack website that tracks live orders from the cashier screen to the display on the barista screen, and can check these orders for completeness. 3 | 4 | # How It's Made: 5 | Tech used: HTML, CSS, JavaScript, Node-JS, MongoDB, Express and Passport 6 | 7 | The objective of this project was to create a coffee app that cashiers can send live pending orders to baristas. The baristas can see the live orders, and after completing the order they can click on the check icon to send the live order to the completed section of orders on the same screen. 8 | 9 | On the barista page is the email and user id of the barista logged, and a list of pending orders and complete orders. The barista will receive the pending orders. Upon completion the barista can click on the check icon, setting the boolean value from false to true and send the order to the complete order list on their page and sent back to the cashiers completed order list. 10 | 11 | # Installation 12 | Clone repo 13 | run npm install 14 | # Usage 15 | run node server.js 16 | Navigate to localhost:8080 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "coffee", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "server.js", 6 | "author": "", 7 | "license": "ISC", 8 | "dependencies": { 9 | "express": "~4.14.0", 10 | "ejs": "~2.5.2", 11 | "mongoose": "~4.13.1", 12 | "passport": "~0.3.2", 13 | "passport-local": "~1.0.0", 14 | "passport-facebook": "~2.1.1", 15 | "passport-twitter": "~1.0.4", 16 | "passport-google-oauth": "~1.0.0", 17 | "connect-flash": "~0.1.1", 18 | "bcrypt-nodejs": "latest", 19 | "morgan": "~1.7.0", 20 | "body-parser": "~1.15.2", 21 | "cookie-parser": "~1.4.3", 22 | "method-override": "~2.3.6", 23 | "express-session": "~1.14.1" 24 | }, 25 | "scripts": { 26 | "start": "node server.js" 27 | }, 28 | "devDependencies": {}, 29 | "repository": { 30 | "type": "git", 31 | "url": "git+https://github.com/joshlobaptista/Barista-Fullstack.git" 32 | }, 33 | "bugs": { 34 | "url": "https://github.com/joshlobaptista/Barista-Fullstack/issues" 35 | }, 36 | "homepage": "https://github.com/joshlobaptista/Barista-Fullstack#readme" 37 | } 38 | -------------------------------------------------------------------------------- /views/login.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Barista 5 | 6 | 7 | 8 | 9 | 10 |
11 |
12 | 13 |

Barista Login

14 | 15 | <% if (message.length > 0) { %> 16 |
<%= message %>
17 | <% } %> 18 | 19 | 20 |
21 |
22 | 23 | 24 |
25 |
26 | 27 | 28 |
29 | 30 | 31 |
32 | 33 |
34 | 35 |

Need an account? Signup

36 |

Or go home.

37 | 38 | 39 |
40 |
41 | 42 | 43 | -------------------------------------------------------------------------------- /app/models/user.js: -------------------------------------------------------------------------------- 1 | // load the things we need 2 | var mongoose = require('mongoose'); 3 | var bcrypt = require('bcrypt-nodejs'); 4 | 5 | // define the schema for our user model 6 | var userSchema = mongoose.Schema({ 7 | 8 | local : { 9 | email : String, 10 | password : String 11 | }, 12 | facebook : { 13 | id : String, 14 | token : String, 15 | name : String, 16 | email : String 17 | }, 18 | twitter : { 19 | id : String, 20 | token : String, 21 | displayName : String, 22 | username : String 23 | }, 24 | google : { 25 | id : String, 26 | token : String, 27 | email : String, 28 | name : String 29 | } 30 | 31 | }); 32 | 33 | // generating a hash 34 | userSchema.methods.generateHash = function(password) { 35 | return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null); 36 | }; 37 | 38 | // checking if password is valid 39 | userSchema.methods.validPassword = function(password) { 40 | return bcrypt.compareSync(password, this.local.password); 41 | }; 42 | 43 | // create the model for users and expose it to our app 44 | module.exports = mongoose.model('User', userSchema); 45 | -------------------------------------------------------------------------------- /views/cashlogin.ejs: -------------------------------------------------------------------------------- 1 | 43 | -------------------------------------------------------------------------------- /public/cashier.css: -------------------------------------------------------------------------------- 1 | body{ 2 | padding-top:80px; 3 | word-wrap:break-word; 4 | background-image: url("http://www.photoshopfreebrushes.com/wp-content/uploads/2015/08/coffee-background-pattern-9.jpg"); 5 | background-repeat: no-repeat; 6 | background-size: cover; 7 | } 8 | 9 | input{ 10 | background-color: transparent; 11 | width: 20%; 12 | } 13 | 14 | select{ 15 | background-color: transparent; 16 | width: 20%; 17 | } 18 | 19 | button{ 20 | width: auto; 21 | background-color: transparent; 22 | } 23 | 24 | .container .jumbotron{ 25 | background-image: url("https://orig00.deviantart.net/32ba/f/2012/343/3/9/pokemon_background_by_iwildblood-d5nhuuq.png"); 26 | } 27 | 28 | .well{ 29 | background-color: transparent; 30 | border: none; 31 | } 32 | 33 | .col-sm-offset-3{ 34 | border: 3px solid black; 35 | background-color: blanchedalmond; 36 | border-radius: 30px; 37 | } 38 | 39 | .col-sm-4{ 40 | border: 3px solid black; 41 | background-color: blanchedalmond; 42 | border-radius: 30px; 43 | text-align: center; 44 | width: 75%; 45 | } 46 | 47 | .col-sm-5{ 48 | border: 3px solid black; 49 | background-color: blanchedalmond; 50 | border-radius: 30px; 51 | text-align: center; 52 | width: 25%; 53 | } 54 | 55 | #logo{ 56 | width: 100%; 57 | height: 200px; 58 | } 59 | .page-header{ 60 | border: 3px solid black; 61 | background-color: blanchedalmond; 62 | border-radius: 30px; 63 | } 64 | -------------------------------------------------------------------------------- /public/style.css: -------------------------------------------------------------------------------- 1 | body{ 2 | padding-top:80px; 3 | word-wrap:break-word; 4 | background-image: url("http://www.photoshopfreebrushes.com/wp-content/uploads/2015/08/coffee-background-pattern-9.jpg"); 5 | background-repeat: no-repeat; 6 | background-size: cover; 7 | } 8 | 9 | input{ 10 | background-color: transparent; 11 | width: 30%; 12 | } 13 | 14 | select{ 15 | background-color: transparent; 16 | width: 20%; 17 | } 18 | 19 | button{ 20 | width: auto; 21 | background-color: transparent; 22 | } 23 | 24 | .container .jumbotron{ 25 | background-image: url("https://orig00.deviantart.net/32ba/f/2012/343/3/9/pokemon_background_by_iwildblood-d5nhuuq.png"); 26 | } 27 | 28 | .well{ 29 | background-color: transparent; 30 | border: none; 31 | } 32 | 33 | .col-sm-offset-3{ 34 | border: 3px solid black; 35 | background-color: blanchedalmond; 36 | border-radius: 30px; 37 | } 38 | 39 | 40 | .col-sm-5{ 41 | border: 3px solid black; 42 | background-color: blanchedalmond; 43 | border-radius: 30px; 44 | text-align: center; 45 | width: 50%; 46 | } 47 | 48 | .col-sm-6{ 49 | border: 3px solid black; 50 | background-color: blanchedalmond; 51 | border-radius: 30px; 52 | text-align: center; 53 | width: 25%; 54 | } 55 | 56 | #logo{ 57 | width: 100%; 58 | height: 200px; 59 | } 60 | .page-header{ 61 | border: 3px solid black; 62 | background-color: blanchedalmond; 63 | border-radius: 30px; 64 | } 65 | -------------------------------------------------------------------------------- /npm-debug.log: -------------------------------------------------------------------------------- 1 | 0 info it worked if it ends with ok 2 | 1 verbose cli [ '/usr/bin/node', '/usr/bin/npm', 'run', 'dev' ] 3 | 2 info using npm@3.5.2 4 | 3 info using node@v8.10.0 5 | 4 verbose stack Error: missing script: dev 6 | 4 verbose stack at run (/usr/share/npm/lib/run-script.js:147:19) 7 | 4 verbose stack at /usr/share/npm/lib/run-script.js:57:5 8 | 4 verbose stack at /usr/share/npm/node_modules/read-package-json/read-json.js:345:5 9 | 4 verbose stack at checkBinReferences_ (/usr/share/npm/node_modules/read-package-json/read-json.js:309:45) 10 | 4 verbose stack at final (/usr/share/npm/node_modules/read-package-json/read-json.js:343:3) 11 | 4 verbose stack at then (/usr/share/npm/node_modules/read-package-json/read-json.js:113:5) 12 | 4 verbose stack at ReadFileContext. (/usr/share/npm/node_modules/read-package-json/read-json.js:284:20) 13 | 4 verbose stack at ReadFileContext.callback (/usr/share/npm/node_modules/graceful-fs/graceful-fs.js:76:16) 14 | 4 verbose stack at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:420:13) 15 | 5 verbose cwd /home/joshuabaptista/Documents/week10/morningChallenges/coffee 16 | 6 error Linux 4.15.0-23-generic 17 | 7 error argv "/usr/bin/node" "/usr/bin/npm" "run" "dev" 18 | 8 error node v8.10.0 19 | 9 error npm v3.5.2 20 | 10 error missing script: dev 21 | 11 error If you need help, you may report this error at: 22 | 11 error 23 | 12 verbose exit [ 1, true ] 24 | -------------------------------------------------------------------------------- /views/connect-local.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Orders 5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 |
13 | 14 |

Add Local Account

15 | 16 | <% if (message.length > 0) { %> 17 |
<%= message %>
18 | <% } %> 19 | 20 | 21 |
22 |
23 | 24 | 25 |
26 |
27 | 28 | 29 |
30 | 31 | 32 |
33 | 34 |
35 | 36 |

Go back to profile

37 | 38 | 39 |
40 |
41 | 42 | 43 | -------------------------------------------------------------------------------- /views/signup.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Barista Sign Up 5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 |
13 | 14 |

Barista Signup

15 | 16 | <% if (message.length > 0) { %> 17 |
<%= message %>
18 | <% } %> 19 | 20 | 21 |
22 |
23 | 24 | 25 |
26 |
27 | 28 | 29 |
30 | 31 | 32 |
33 | 34 |
35 | 36 |

Already have an account? Login

37 |

Or go home.

38 | 39 |
40 |
41 | 42 | 43 | -------------------------------------------------------------------------------- /views/cashsignup.ejs: -------------------------------------------------------------------------------- 1 | 21 | 43 | -------------------------------------------------------------------------------- /views/index.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Coffee Orders 5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 |
14 |

Welcome Model Employee!

15 | Heres a quote to get your day off to a good start 16 |

"Would I rather be feared or loved? Easy. Both. I want people to be afraid of how much they love me."

17 | 18 | 19 |

Login or Signup:

20 | 21 |

Barista

22 | Login 23 | Signup 24 |

Cashier

25 | Cashier 26 | 27 |
28 | 29 |
30 | 31 | 32 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | // server.js 2 | 3 | // set up ====================================================================== 4 | // get all the tools we need 5 | var express = require('express'); 6 | var app = express(); 7 | var port = process.env.PORT || 8080; 8 | var mongoose = require('mongoose'); 9 | var passport = require('passport'); 10 | var flash = require('connect-flash'); 11 | 12 | var morgan = require('morgan'); 13 | var cookieParser = require('cookie-parser'); 14 | var bodyParser = require('body-parser'); 15 | var session = require('express-session'); 16 | 17 | var configDB = require('./config/database.js'); 18 | 19 | var db 20 | 21 | // configuration =============================================================== 22 | mongoose.connect(configDB.url, (err, database) => { 23 | if (err) return console.log(err) 24 | db = database 25 | require('./app/routes.js')(app, passport, db); 26 | }); // connect to our database 27 | 28 | 29 | 30 | require('./config/passport')(passport); // pass passport for configuration 31 | 32 | // set up our express application 33 | app.use(morgan('dev')); // log every request to the console 34 | app.use(cookieParser()); // read cookies (needed for auth) 35 | app.use(bodyParser.json()); // get information from html forms 36 | app.use(bodyParser.urlencoded({ extended: true })); 37 | app.use(express.static('public')) 38 | 39 | app.set('view engine', 'ejs'); // set up ejs for templating 40 | 41 | // required for passport 42 | app.use(session({ 43 | secret: 'rcbootcamp2018a', // session secret 44 | resave: true, 45 | saveUninitialized: true 46 | })); 47 | app.use(passport.initialize()); 48 | app.use(passport.session()); // persistent login sessions 49 | app.use(flash()); // use connect-flash for flash messages stored in session 50 | 51 | 52 | // routes ====================================================================== 53 | //require('./app/routes.js')(app, passport, db); // load our routes and pass in our app and fully configured passport 54 | 55 | // launch ====================================================================== 56 | app.listen(port); 57 | console.log('Follow us to ' + port); 58 | -------------------------------------------------------------------------------- /views/cashier.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Cashier 5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 17 | 18 |
19 | 20 | 21 | 22 |
23 | 24 |

Take Their Order

25 |
26 | 27 | 36 | 42 | 43 | 44 |
45 |
46 | 47 |
48 |

Completed Order

49 |
    50 | <% for(var i=0; i 51 | <% if(orders[i].complete === true) { %> 52 |
  1. 53 | <%= orders[i].name %> 54 | <%= orders[i].customer %> 55 | <%= orders[i].coffee %> 56 | <%= orders[i].size %> 57 | <%= orders[i].other %> 58 | <%= orders[i].check %> 59 | <%= orders[i].thumbDown %> 60 | <%= orders[i].trash %> 61 | 62 | 63 | 64 |
  2. 65 | <% } %> 66 | <% } %> 67 |
68 |
69 | 70 |
71 | 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /public/main.js: -------------------------------------------------------------------------------- 1 | var check = document.getElementsByClassName("fa-check"); 2 | // var thumbDown = document.getElementsByClassName("fa-thumbs-down"); 3 | var ban = document.getElementsByClassName("fa-ban"); 4 | 5 | Array.from(check).forEach(function(element) { 6 | element.addEventListener('click', function(){ 7 | // const name = this.parentNode.parentNode.childNodes[1].innerText 8 | const customer = this.parentNode.parentNode.childNodes[3].innerText 9 | const coffee = this.parentNode.parentNode.childNodes[5].innerText 10 | const size = this.parentNode.parentNode.childNodes[7].innerText 11 | const other = this.parentNode.parentNode.childNodes[9].innerText 12 | const check = parseFloat(this.parentNode.parentNode.childNodes[9].innerText) 13 | fetch('orders', { 14 | method: 'put', 15 | headers: {'Content-Type': 'application/json'}, 16 | body: JSON.stringify({ 17 | // 'name': name, 18 | 'customer': customer, 19 | 'coffee': coffee, 20 | 'size': size, 21 | 'other': other, 22 | 'check': check, 23 | complete: true 24 | }) 25 | }) 26 | .then(response => { 27 | console.log(response) 28 | if (response.ok) return response.json() 29 | }) 30 | .then(data => { 31 | console.log(data) 32 | window.location.reload(true) 33 | }) 34 | }); 35 | }); 36 | 37 | // Array.from(thumbDown).forEach(function(element) { 38 | // element.addEventListener('click', function(){ 39 | // const name = this.parentNode.parentNode.childNodes[1].innerText 40 | // const customer = this.parentNode.parentNode.childNodes[3].innerText 41 | // const coffee = this.parentNode.parentNode.childNodes[5].innerText 42 | // const size = this.parentNode.parentNode.childNodes[7].innerText 43 | // const thumbDown = parseFloat(this.parentNode.parentNode.childNodes[11].innerText) 44 | // fetch('copmletedDown', { 45 | // method: 'put', 46 | // headers: {'Content-Type': 'application/json'}, 47 | // body: JSON.stringify({ 48 | // 'name': name, 49 | // 'customer': customer, 50 | // 'coffee': coffee, 51 | // 'size': size, 52 | // 'thumbDown':thumbDown 53 | // }) 54 | // }) 55 | // .then(response => { 56 | // if (response.ok) return response.json() 57 | // }) 58 | // .then(data => { 59 | // console.log(data) 60 | // window.location.reload(true) 61 | // }) 62 | // }); 63 | // }); 64 | 65 | Array.from(ban).forEach(function(element) { 66 | element.addEventListener('click', function(){ 67 | const customer = this.parentNode.parentNode.childNodes[3].innerText 68 | const coffee = this.parentNode.parentNode.childNodes[5].innerText 69 | const size = this.parentNode.parentNode.childNodes[7].innerText 70 | const other = this.parentNode.parentNode.childNodes[9].innerText 71 | fetch('orders', { 72 | method: 'delete', 73 | headers: { 74 | 'Content-Type': 'application/json' 75 | }, 76 | body: JSON.stringify({ 77 | 'customer': customer, 78 | 'coffee': coffee, 79 | 'size': size, 80 | 'other': other 81 | 82 | }) 83 | }).then(function (response) { 84 | window.location.reload() 85 | }) 86 | }); 87 | }); 88 | -------------------------------------------------------------------------------- /views/profile.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Barista 5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 17 | 18 |
19 | 20 | 21 |
22 |
23 |

Local

24 | 25 | <% if (user.local.email) { %> 26 |

27 | id: <%= user._id %>
28 | email: <%= user.local.email %>
29 | 30 |

31 | 32 | Unlink 33 | <% } else { %> 34 | Connect Local 35 | <% } %> 36 | 37 |
38 |
39 |
40 |

Pending Orders

41 |
    42 | <% for(var i=0; i 43 | <% if(orders[i].complete === false) { %> 44 |
  1. 45 | <%= orders[i].name %> 46 | <%= orders[i].customer %> 47 | <%= orders[i].coffee %> 48 | <%= orders[i].size %> 49 | <%= orders[i].other %> 50 | <%= orders[i].check %> 51 | <%= orders[i].thumbDown %> 52 | <%= orders[i].ban %> 53 | 54 | 55 | 56 |
  2. 57 | <% } %> 58 | <% } %> 59 |
60 | 61 |

Completed Orders

62 |
    63 | <% for(var i=0; i 64 | <% if(orders[i].complete === true) { %> 65 |
  1. 66 | <%= orders[i].name %> 67 | <%= orders[i].customer %> 68 | <%= orders[i].coffee %> 69 | <%= orders[i].size %> 70 | <%= orders[i].other %> 71 | <%= orders[i].check %> 72 | <%= orders[i].thumbDown %> 73 | <%= orders[i].ban %> 74 | 75 | 76 | 77 |
  2. 78 | <% } %> 79 | <% } %> 80 |
81 | 82 |

83 | 84 | 85 | 86 | 87 | 103 | 104 |
105 | <% } %> 106 | 107 |
108 | 109 | 110 | 111 | 112 | 113 | -------------------------------------------------------------------------------- /config/passport.js: -------------------------------------------------------------------------------- 1 | // config/passport.js 2 | 3 | // load all the things we need 4 | var LocalStrategy = require('passport-local').Strategy; 5 | 6 | // load up the user model 7 | var User = require('../app/models/user'); 8 | 9 | // expose this function to our app using module.exports 10 | module.exports = function(passport) { 11 | 12 | // ========================================================================= 13 | // passport session setup ================================================== 14 | // ========================================================================= 15 | // required for persistent login sessions 16 | // passport needs ability to serialize and unserialize users out of session 17 | 18 | // used to serialize the user for the session 19 | passport.serializeUser(function(user, done) { 20 | done(null, user.id); 21 | }); 22 | 23 | // used to deserialize the user 24 | passport.deserializeUser(function(id, done) { 25 | User.findById(id, function(err, user) { 26 | done(err, user); 27 | }); 28 | }); 29 | 30 | // ========================================================================= 31 | // LOCAL SIGNUP ============================================================ 32 | // ========================================================================= 33 | // we are using named strategies since we have one for login and one for signup 34 | // by default, if there was no name, it would just be called 'local' 35 | 36 | passport.use('local-signup', new LocalStrategy({ 37 | // by default, local strategy uses username and password, we will override with email 38 | usernameField : 'email', 39 | passwordField : 'password', 40 | passReqToCallback : true // allows us to pass back the entire request to the callback 41 | }, 42 | function(req, email, password, done) { 43 | 44 | // find a user whose email is the same as the forms email 45 | // we are checking to see if the user trying to login already exists 46 | User.findOne({ 'local.email' : email }, function(err, user) { 47 | // if there are any errors, return the error 48 | if (err) 49 | return done(err); 50 | 51 | // check to see if theres already a user with that email 52 | if (user) { 53 | return done(null, false, req.flash('signupMessage', 'That email is already taken.')); 54 | } else { 55 | 56 | // if there is no user with that email 57 | // create the user 58 | var newUser = new User(); 59 | 60 | // set the user's local credentials 61 | newUser.local.email = email; 62 | newUser.local.password = newUser.generateHash(password); // use the generateHash function in our user model 63 | 64 | // save the user 65 | newUser.save(function(err) { 66 | if (err) 67 | throw err; 68 | return done(null, newUser); 69 | }); 70 | } 71 | 72 | }); 73 | 74 | })); 75 | 76 | // ========================================================================= 77 | // LOCAL LOGIN ============================================================= 78 | // ========================================================================= 79 | // we are using named strategies since we have one for login and one for signup 80 | // by default, if there was no name, it would just be called 'local' 81 | 82 | passport.use('local-login', new LocalStrategy({ 83 | // by default, local strategy uses username and password, we will override with email 84 | usernameField : 'email', 85 | passwordField : 'password', 86 | passReqToCallback : true // allows us to pass back the entire request to the callback 87 | }, 88 | function(req, email, password, done) { // callback with email and password from our form 89 | 90 | // find a user whose email is the same as the forms email 91 | // we are checking to see if the user trying to login already exists 92 | User.findOne({ 'local.email' : email }, function(err, user) { 93 | // if there are any errors, return the error before anything else 94 | if (err) 95 | return done(err); 96 | 97 | // if no user is found, return the message 98 | if (!user) 99 | return done(null, false, req.flash('loginMessage', 'No user found.')); // req.flash is the way to set flashdata using connect-flash 100 | 101 | // if the user is found but the password is wrong 102 | if (!user.validPassword(password)) 103 | return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata 104 | 105 | // all is well, return successful user 106 | return done(null, user); 107 | }); 108 | 109 | })); 110 | 111 | }; 112 | -------------------------------------------------------------------------------- /app/routes.js: -------------------------------------------------------------------------------- 1 | module.exports = function(app, passport, db) { 2 | 3 | // normal routes =============================================================== 4 | 5 | // show the home page (will also have our login links) 6 | app.get('/', function(req, res) { 7 | res.render('index.ejs'); 8 | }); 9 | 10 | // Barista SECTION ========================= 11 | app.get('/profile', isLoggedIn, function(req, res) { 12 | db.collection('orders').find().toArray((err, result) => { 13 | if (err) return console.log(err) 14 | res.render('profile.ejs', { 15 | user : req.user, 16 | orders: result 17 | }) 18 | }) 19 | }); 20 | 21 | // cashier SECTION ========================= 22 | app.get('/cashier', isLoggedIn, function(req, res) { 23 | db.collection('orders').find().toArray((err, result) => { 24 | if (err) return console.log(err) 25 | res.render('cashier.ejs', { 26 | user : req.user, 27 | orders: result 28 | }) 29 | }) 30 | }); 31 | 32 | // LOGOUT ============================== 33 | app.get('/logout', function(req, res) { 34 | req.logout(); 35 | res.redirect('/'); 36 | }); 37 | 38 | // message board routes =============================================================== 39 | //cashier collections 40 | app.post('/orders', (req, res) => { 41 | db.collection('orders').save({name: req.body.name, customer: req.body.customer, coffee: req.body.coffee, size: req.body.size, other: req.body.other, complete: false}, (err, result) => { 42 | if (err) return console.log(err) 43 | console.log('saved to database') 44 | res.redirect('/cashier') 45 | }) 46 | }) 47 | // //barista collections 48 | // app.post('/orders', (req, res) => { 49 | // db.collection('orders').save({name: req.body.name, customer: req.body.customer, coffee: req.body.coffee, size: req.body.size, complete: false}, (err, result) => { 50 | // if (err) return console.log(err) 51 | // console.log('saved to database') 52 | // //sends this post back to the cashiers view 53 | // res.redirect('/profile') 54 | // }) 55 | // }) 56 | //barista 57 | app.put('/orders', (req, res) => { 58 | db.collection('orders') 59 | .findOneAndUpdate({name: req.body.name, customer: req.body.customer, coffee: req.body.coffee, size: req.body.size, other: req.body.other, complete: false}, { 60 | $set: { 61 | complete: true 62 | } 63 | }, { 64 | sort: {_id: -1}, 65 | upsert: true 66 | }, (err, result) => { 67 | if (err) return res.send(err) 68 | res.send(result) 69 | }) 70 | }) 71 | // 72 | // app.put('/ordersDown', (req, res) => { 73 | // db.collection('orders') 74 | // .findOneAndUpdate({name: req.body.name, size: req.body.size}, { 75 | // $set: { 76 | // thumbUp:req.body.thumbDown - 1 77 | // } 78 | // }, { 79 | // sort: {_id: -1}, 80 | // upsert: true 81 | // }, (err, result) => { 82 | // if (err) return res.send(err) 83 | // res.send(result) 84 | // }) 85 | // }) 86 | 87 | app.delete('/orders', (req, res) => { 88 | db.collection('orders').findOneAndDelete({name: req.body.name, other: req.body.other}, (err, result) => { 89 | if (err) return res.send(500, err) 90 | res.send('Message deleted!') 91 | }) 92 | }) 93 | 94 | // ============================================================================= 95 | // AUTHENTICATE (FIRST LOGIN) ================================================== 96 | // ============================================================================= 97 | 98 | // locally -------------------------------- 99 | // LOGIN =============================== 100 | // show the login form 101 | app.get('/login', function(req, res) { 102 | res.render('login.ejs', { message: req.flash('loginMessage') }); 103 | }); 104 | 105 | app.get('/cashier', function(req, res) { 106 | res.render('cashier.ejs', { message: req.flash('loginMessage') }); 107 | }); 108 | 109 | // process the login form 110 | app.post('/login', passport.authenticate('local-login', { 111 | successRedirect : '/profile', // redirect to the secure profile section 112 | failureRedirect : '/login', // redirect back to the signup page if there is an error 113 | failureFlash : true // allow flash messages 114 | })); 115 | 116 | app.post('/cashier', passport.authenticate('local-login', { 117 | successRedirect : '/cashier', // redirect to the secure profile section 118 | failureRedirect : '/cashlogin', // redirect back to the signup page if there is an error 119 | failureFlash : true // allow flash messages 120 | })); 121 | 122 | // SIGNUP ================================= 123 | // show the signup form 124 | app.get('/signup', function(req, res) { 125 | res.render('signup.ejs', { message: req.flash('signupMessage') }); 126 | }); 127 | 128 | // app.get('/cashsignup', function(req, res) { 129 | // res.render('cashsignup.ejs', { message: req.flash('signupMessage') }); 130 | // }); 131 | 132 | // process the signup form 133 | app.post('/signup', passport.authenticate('local-signup', { 134 | successRedirect : '/profile', // redirect to the secure profile section 135 | failureRedirect : '/signup', // redirect back to the signup page if there is an error 136 | failureFlash : true // allow flash messages 137 | })); 138 | 139 | // app.post('/cashsignup', passport.authenticate('local-signup', { 140 | // successRedirect : '/cashier', // redirect to the secure profile section 141 | // failureRedirect : '/cashsignup', // redirect back to the signup page if there is an error 142 | // failureFlash : true // allow flash messages 143 | // })); 144 | 145 | // ============================================================================= 146 | // UNLINK ACCOUNTS ============================================================= 147 | // ============================================================================= 148 | // used to unlink accounts. for social accounts, just remove the token 149 | // for local account, remove email and password 150 | // user account will stay active in case they want to reconnect in the future 151 | 152 | // local ----------------------------------- 153 | app.get('/unlink/local', isLoggedIn, function(req, res) { 154 | var user = req.user; 155 | user.local.email = undefined; 156 | user.local.password = undefined; 157 | user.save(function(err) { 158 | res.redirect('/profile'); 159 | }); 160 | }); 161 | 162 | }; 163 | 164 | // route middleware to ensure user is logged in 165 | function isLoggedIn(req, res, next) { 166 | if (req.isAuthenticated()) 167 | return next(); 168 | 169 | res.redirect('/'); 170 | } 171 | --------------------------------------------------------------------------------