SHARE
TWEET

lists

samcio Aug 12th, 2016 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --lists.jade--
  2. extends layout
  3.  
  4. block content
  5.   div(id='titlediv')
  6.     h1= title
  7.  
  8.     if user
  9.       p Welcome #{user.profile.displayName}
  10.         a(href="/logout") Logout
  11.       p choose a list or create a new list
  12.       div
  13.         container
  14.           h3 Lists
  15.           ul
  16.             each list in wishlists
  17.               li
  18.                 a(href='/myList') #{list.listTitle}
  19.              
  20.           form(action='lists/add', method='post')
  21.             input(type='text', id='newList', name="newList", placeholder='New List')
  22.             input(type='hidden',id='userId', name="userId", value='#{user.facebookId}')
  23.             input(type='submit', value='add')
  24.    
  25. --lists.js--
  26. var express = require('express');
  27. var router = express.Router();
  28.  
  29. var passport = require('passport');
  30. var FacebookStrategy = require('passport-facebook').Strategy;
  31. var session = require('express-session');
  32. var mongoose = require('mongoose');
  33.  
  34. var User = mongoose.model('User');
  35. var Wishlist = mongoose.model('Wishlist');
  36.  
  37. /* GET home page. */
  38. router.get('/', function(req, res, next) {
  39.   //how do i make it to show the users wishlist when they are logged in
  40.   if(req.user){
  41.     console.log(req.user.facebookId);
  42.     Wishlist.find({userId: req.user.facebookId},function(err,docs){
  43.       res.locals.error = req.session.error;
  44.       res.locals.success= req.session.success;
  45.       req.session.error = null;
  46.       req.session.success = null;
  47.       Wishlist.findOne({listTitle: "My WishList"}, function(err,list){
  48.         res.locals.error = req.session.error;
  49.         list = JSON.parse(JSON.stringify(list));
  50.         console.log(list);
  51.         res.render('lists', { title: 'Wishy', wishlists: docs, currentlist: list});
  52.       });
  53.  
  54.     });
  55.   }
  56.   else{
  57.     res.render('index', { title: 'Wishy'});
  58.   }
  59. });
  60.  
  61. router.post('/add', function(req, res, next) {
  62.   console.log("the list "+req.body);
  63.   if(req.body.newList!==""){
  64.     var wishlist = new Wishlist({
  65.       userId: req.body.userId,
  66.       listTitle: req.body.newList
  67.     });
  68.     wishlist.save(function(err){
  69.       if(err){
  70.         console.log(err.message);
  71.       }
  72.     });
  73.   }
  74.  
  75.   res.redirect('/lists');
  76. });
  77.  
  78. module.exports = router;
  79.  
  80. --app.js--
  81. const express = require('express');
  82. const path = require('path');
  83. const logger = require('morgan');
  84. const cookieParser = require('cookie-parser');
  85. const bodyParser = require('body-parser');
  86. const passport = require('passport');
  87. const favicon = require('serve-favicon');
  88. const FacebookStrategy = require('passport-facebook').Strategy;
  89. const session = require('express-session');
  90. const mongoose = require('mongoose');
  91. const debug = require('debug')('app');
  92. const helmet = require('helmet')
  93.  
  94. const User = require('./models/User');
  95. const Wishlist = require('./models/Wishlist.js');
  96.  
  97. const routes = require('./routes/index');
  98. const users = require('./routes/users');
  99. const lists = require('./routes/lists');
  100. const myList = require('./routes/myList');
  101.  
  102. require('dotenv').config();
  103.  
  104. const DB_NAME = process.env.DB_NAME;
  105. const MONGO_PASS = process.env.MONGO_DB_PASS;
  106. const MONGO_HOST = process.env.OPENSHIFT_MONGODB_DB_HOST;
  107. const MONGO_PORT = process.env.OPENSHIFT_MONGODB_DB_PORT;
  108.  
  109. if (MONGO_HOST) {
  110.   mongoose.connect(`mongodb://admin:${MONGO_PASS}@${MONGO_HOST}:${MONGO_PORT}/${DB_NAME}`);
  111. } else {
  112.   mongoose.connect(`mongodb://localhost/${DB_NAME}`);
  113. }
  114.  
  115. const app = express();
  116.  
  117. app.use(session({
  118.   secret: process.env.SESSION_SECRET,
  119.   resave: true,
  120.   saveUninitialized: true,
  121.   cookie: { maxAge: 600000 } //10 minutes
  122. }));
  123.  
  124.  
  125. app.use(passport.initialize());
  126. app.use(passport.session());
  127.  
  128. app.get('*', (req, res, next) => {
  129.   if (req.user) {
  130.     req.session.userId = req.user.facebookId;
  131.     res.locals.user = req.user; // eslint-disable-line no-param-reassign
  132.   }
  133.   next();
  134. });
  135.  
  136. app.use('/', routes);
  137. app.use('/users', users);
  138. app.use('/lists', lists);
  139. app.use('/myList', myList);
  140.  
  141. passport.use(new FacebookStrategy({
  142.   clientID: process.env.FACEBOOK_APP_ID,
  143.   clientSecret: process.env.FACEBOOK_APP_SECRET,
  144.   callbackURL: process.env.FACEBOOK_CALLBACK_URL,
  145. }, (accessToken, refreshToken, profile, cb) => {
  146.   User.findOne({ facebookId: profile.id }, (err, user) => {
  147.     if (err) {
  148.       cb(err);
  149.     } else if (user) {
  150.       cb(err, user);
  151.     }
  152.  
  153.     const newUser = new User({
  154.       facebookId: profile.id,
  155.       profile,
  156.     });
  157.  
  158.     newUser.save((newUserError) => {
  159.       if (newUserError) {
  160.         cb(newUserError);
  161.       } else {
  162.         cb(null, newUser);
  163.       }
  164.     });
  165.   });
  166. }));
  167.  
  168. passport.serializeUser((user, cb) => cb(null, user._id)); // eslint-disable-line
  169.  
  170. passport.deserializeUser((id, cb) => {
  171.   User.findOne(id, (err, user) => {
  172.     if (err) {
  173.       cb(err);
  174.     } else {
  175.       cb(null, user);
  176.     }
  177.   });
  178. });
  179.  
  180. // view engine setup
  181. app.set('views', path.join(__dirname, 'views'));
  182. app.set('view engine', 'jade');
  183.  
  184. app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
  185. app.use(logger('dev'));
  186. app.use(bodyParser.json());
  187. app.use(bodyParser.urlencoded({ extended: false }));
  188. app.use(cookieParser());
  189.  
  190. app.use(express.static(path.join(__dirname, './public')));
  191.  
  192. // catch 404 and forward to error handler
  193. app.use((req, res, next) => {
  194.   const err = new Error('Not Found');
  195.   err.status = 404;
  196.   next(err);
  197. });
  198.  
  199. app.use((err, req, res) => {
  200.   res.status(err.status || 500);
  201.   res.render('error', {
  202.     message: err.message,
  203.     error: (app.get('env') === 'development') ? err : {},
  204.   });
  205. });
  206.  
  207. module.exports = app;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top