Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express');
- const cookieParser = require('cookie-parser');
- const bodyParser = require('body-parser');
- const hbs = require('hbs');
- const expressValidator = require('express-validator');
- const flash = require('connect-flash');
- const session = require('express-session');
- const passport = require('passport');
- const LocalStrategy = require('passport-local').Strategy;
- const mongoose = require('mongoose');
- const fs = require('fs');
- var app = express();
- // Server port
- const port = process.env.PORT || 3000;
- // Server starting message
- app.listen(port, () => {
- console.log(`Server is up on port ${port}`);
- });
- // Views directory established and handbars engine
- hbs.registerPartials(__dirname + '/views/layouts')
- app.set('view engine', 'hbs');
- // static assets rendered
- app.use(express.static(__dirname + '/public'));
- app.use('/users', express.static(__dirname + '/public'));
- // body-parser middleware
- app.use(bodyParser.urlencoded({ extended: false }))
- app.use(bodyParser.json())
- app.use(cookieParser());
- // expressSession
- app.use(session({
- secret: 'secret',
- saveUninitialized: true,
- resave: true
- }));
- // passport
- app.use(passport.initialize());
- app.use(passport.session());
- // expressValidator
- app.use(expressValidator({
- errorFormatter: function(param, msg, value) {
- var namespace = param.split('.'),
- root = namespace.shift(),
- formParam = root;
- while(namespace.length) {
- formParam += '[' + namespace.shift() + ']';
- }
- return {
- param: formParam,
- msg: msg,
- value: value
- };
- }
- }));
- // Connect Flash
- app.use(flash());
- // Global Vars
- app.use(function (req, res, next) {
- res.locals.success_msg = req.flash('success_msg');
- res.locals.error_msg = req.flash('error_msg');
- res.locals.error = req.flash('error');
- res.locals.user = req.user || null;
- next();
- });
- // server.log setup middleware
- app.use((req, res, next) => {
- var now = new Date().toString();
- var log = `${now}: ${req.method} ${req.url}`
- console.log(log);
- fs.appendFile('server.log', log + 'n', (err) => {
- if (err) {
- console.log('Unable to append to server.log');
- }
- });
- next();
- });
- // Routes
- const routes = require('./routes/routes');
- const users = require('./routes/users');
- app.use("/", routes);
- app.use("/users", users);
- // Get year for footer
- hbs.registerHelper('getCurrentYear', () => {
- return new Date().getFullYear()
- });
- const express = require('express');
- const mongoose = require('mongoose');
- const bcrypt = require('bcryptjs');
- var app = express();
- if (app.get('env') === 'production') {
- mongoose.connect(process.env.DATABASE_URL);
- } else {
- mongoose.connect('mongodb://localhost/pol-development');
- }
- var db = mongoose.connection;
- db.on('error', console.error.bind(console, 'connection error:'));
- db.once('open', function() {
- console.log("Connection has been established");
- });
- var UserSchema = mongoose.Schema({
- schoolName: String,
- schoolAddress: String,
- schoolAddress2: String,
- city: String,
- zipCode: String,
- addressCheck: Boolean,
- postalAddress: String,
- postalCity: String,
- postalZipCode: String,
- telephone: Number,
- fax: Number,
- email: String,
- password: String,
- schoolType: String,
- schoolDistrict: String,
- schoolRegion: String,
- curriculum: String,
- participationBefore: Boolean,
- participationYears: Number,
- directorName: String,
- directorTelephone: Number,
- directorEmail: String,
- directorAttendanceRehersal: Boolean,
- directorAttendanceEvent: Boolean,
- schoolLiaisonName: String,
- schoolLiaisonTelephone: Number,
- schoolLiaisonEmail: String,
- schoolLiaisonPosition: String,
- schoolLiaisonOtherPosition: String,
- schoolLiaisonTShirt: String,
- schoolLiaisonTutorMentor: String,
- attendanceRehersal: Boolean,
- attendanceEvent: Boolean
- });
- var User = module.exports = mongoose.model('User', UserSchema);
- module.exports.createUser = function(newUser, callback){
- bcrypt.genSalt(10, function(err, salt) {
- bcrypt.hash(newUser.password, salt, function(err, hash) {
- newUser.password = hash;
- newUser.save(callback);
- });
- });
- }
- module.exports.getUserByEmail = function(email, callback){
- var query = {email: email};
- User.findOne(query, callback);
- }
- module.exports.getUserById = function(id, callback){
- user.findById(id, callback);
- }
- module.exports.comparePassword = function(candidatePassword, hash, callback) {
- bcrypt.compare(candidatePassword, hash, function(err, isMatch) {
- if(err) throw err;
- callback(null, isMatch);
- });
- }
- const express = require('express');
- const router = express.Router();
- const passport = require('passport');
- const LocalStrategy = require('passport-local').Strategy;
- var User = require('../models/user');
- router.get('/register', (req, res) => {
- res.render('register.hbs', {
- pageTitle: 'Register'
- });
- });
- router.get('/login', (req, res) => {
- res.render('login.hbs', {
- pageTitle: 'Login'
- });
- });
- router.post('/register', (req, res) => {
- var schoolName = req.body.schoolName;
- var schoolAddress = req.body.schoolAddress;
- var city = req.body.city;
- var zipCode = req.body.zipCode;
- var postalAddress = req.body.postalAddress;
- var postalCity = req.body.postalCity;
- var postalZipCode = req.body.postalZipCode;
- var telephone = req.body.telephone;
- var email = req.body.email;
- var password = req.body.password;
- var schoolType = req.body.schoolType;
- var schoolDistrict = req.body.schoolDistrict;
- var schoolRegion = req.body.schoolRegion;
- var curriculum = req.body.curriculum;
- var directorName = req.body.directorName;
- var directorTelephone = req.body.directorTelephone;
- var directorEmail = req.body.directorEmail;
- var schoolLiaisonName = req.body.schoolLiaisonName;
- var schoolLiaisonTelephone = req.body.schoolLiaisonTelephone;
- var schoolLiaisonEmail = req.body.schoolLiaisonEmail;
- var schoolLiaisonPosition = req.body.schoolLiaisonPosition;
- var schoolLiaisonTShirt = req.body.schoolLiaisonTShirt;
- var schoolLiaisonTutorMentor = req.body.schoolLiaisonTutorMentor;
- // validations
- req.checkBody('schoolName', 'The school name is required').notEmpty();
- req.checkBody('schoolAddress', 'The school address is required').notEmpty();
- req.checkBody('city', 'The city is required').notEmpty();
- req.checkBody('zipCode', 'This zip code is required').notEmpty();
- // req.checkBody('postalAddress', 'The postal address is required').notEmpty();
- // req.checkBody('postalCity', 'The postal city is required').notEmpty();
- // req.checkBody('postalZipCode', 'The postal zip code is required').notEmpty();
- req.checkBody('telephone', 'A telephone number is required').notEmpty();
- req.checkBody('email', 'An account email is required').notEmpty();
- // req.checkBody('email', 'This account email is not valid').isEmail();
- req.checkBody('password', 'An account password is required').notEmpty();
- req.checkBody('schoolType', 'A school type is required').notEmpty();
- req.checkBody('schoolDistrict', 'A school district is required').notEmpty();
- req.checkBody('schoolRegion', 'A school region is required').notEmpty();
- req.checkBody('curriculum', 'A curriculum is required').notEmpty();
- req.checkBody('directorName', 'A directors name is required').notEmpty();
- req.checkBody('directorTelephone', 'A directors telephone is required').notEmpty();
- req.checkBody('directorEmail', 'A directors email is required').notEmpty();
- req.checkBody('directorEmail', 'This email is not valid').isEmail();
- req.checkBody('schoolLiaisonName', 'A school liaison name is required').notEmpty();
- req.checkBody('schoolLiaisonTelephone', 'A school liaison telephone is required').notEmpty();
- req.checkBody('schoolLiaisonEmail', 'The school liaison email is not valid').isEmail();
- req.checkBody('schoolLiaisonEmail', 'A school liaison email is required').notEmpty();
- req.checkBody('schoolLiaisonPosition', 'A school liaison position is required').notEmpty();
- req.checkBody('schoolLiaisonTShirt', 'A school liaison t-shirt size is required').notEmpty();
- req.checkBody('schoolLiaisonTutorMentor', 'A school liaison tutor/mentor is required').notEmpty();
- var errors = req.validationErrors();
- if (errors) {
- res.render('register', {
- errors:errors
- });
- } else {
- var newUser = new User({
- schoolName: schoolName,
- schoolAddress: schoolAddress,
- city: city,
- zipCode: zipCode,
- postalAddress: postalAddress,
- postalCity: postalCity,
- postalZipCode: postalZipCode,
- telephone: telephone,
- email: email,
- password: password,
- schoolType: schoolType,
- schoolDistrict: schoolDistrict,
- schoolRegion: schoolRegion,
- curriculum: curriculum,
- directorName: directorName,
- directorTelephone: directorTelephone,
- directorEmail: directorEmail,
- schoolLiaisonName: schoolLiaisonName,
- schoolLiaisonTelephone: schoolLiaisonTelephone,
- schoolLiaisonEmail: schoolLiaisonEmail,
- schoolLiaisonPosition: schoolLiaisonPosition,
- schoolLiaisonTShirt: schoolLiaisonTShirt,
- schoolLiaisonTutorMentor: schoolLiaisonTutorMentor,
- });
- User.createUser(newUser, function(err, user) {
- if(err) throw err;
- console.log(user);
- });
- req.flash('success_msg', 'You are now registered, you can now login!');
- res.redirect('/users/login');
- }
- });
- passport.use(new LocalStrategy(
- function(email, password, done) {
- User.getUserByEmail(email, function(err, user){
- if(err) throw err;
- if(!user){
- return done(null, false, {message: 'Unknown Email Address'});
- }
- User.comparePassword(password, user.password, function(err, isMatch){
- if(err) throw err;
- if(isMatch){
- return done(null, user);
- } else {
- return done(null, false, {message: 'Invalid password'});
- }
- });
- });
- }));
- passport.serializeUser(function(user, done) {
- done(null, user.id);
- });
- passport.deserializeUser(function(id, done) {
- User.getUserById(id, function(err, user) {
- done(err, user);
- });
- });
- router.post('/login', passport.authenticate('local', {
- successRedirect: '/',
- failureRedirect: '/users/login',
- successFlash: 'Welcome!',
- failureFlash: 'Invalid username or password.'
- }), function(req, res) {
- res.redirect('/');
- });
- module.exports = router;
- {{> header }}
- <div class="container">
- <div class="row">
- <div class="col-lg-12">
- {{#if success_msg}}
- <div class="alert alert-success">{{success_msg}}</div>
- {{/if}}
- {{#if error_msg}}
- <div class="alert alert-danger">{{error_msg}}</div>
- {{/if}}
- {{#if error}}
- <div class="alert alert-danger">{{error}}</div>
- {{/if}}
- </div>
- </div>
- <form action="/users/login" method="post">
- <div class="panel panel-default">
- <div class="panel-heading">Log In</div>
- <div class="panel-body">
- <div class="form-group">
- <label for="exampleInputEmail1">Email address</label>
- <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email" name="email">
- <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
- </div>
- <div class="form-group">
- <label for="exampleInputPassword1">Password</label>
- <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password" name="password">
- </div>
- <button type="submit" class="btn btn-primary">Login</button>
- </div><!-- Panel Body -->
- </div><!-- Panel Default -->
- </form>
- </div><!-- Container ends -->
- {{> footer }}
- {{> header }}
- <div class="container">
- <form action="/users/register" method="post">
- <h2>Registration Form</h2>
- {{# if errors}}
- <div class="alert alert-danger">
- {{#each errors}}
- <li>{{msg}}</li>
- {{/each}}
- </div>
- {{/if}}
- <div class="panel panel-default">
- <div class="panel-heading">
- School Information
- </div>
- <div class="panel-body">
- <div class="row">
- <div class="form-group col-md-12">
- <label for="schoolName">School Name</label>
- <input type="text" class="form-control" id="schoolName" name="schoolName" placeholder="Enter school name">
- </div>
- <div class="form-group col-md-12">
- <label for="shippingAddress">Physical Address</label>
- <input type="text" class="form-control" id="shippingAddress" name="schoolAddress" placeholder="Enter your school's physical address">
- </div>
- <div class="form-group col-md-12">
- <label for="inputAddress2">Address 2</label>
- <input type="text" class="form-control" id="inputAddress2" name="schoolAddress2" placeholder="Apartment, studio, or floor">
- </div>
- <div class="form-group col-md-6">
- <label for="city">City</label>
- <input type="text" class="form-control" id="city" name="city" placeholder="City">
- </div>
- <div class="form-group col-md-6">
- <label for="zip">Zip Code</label>
- <input type="text" class="form-control" id="zip" name="zipCode" placeholder="Zip Code">
- </div>
- <div class="form-check col-md-12">
- <input type="checkbox" class="form-check-input" id="addressCheck">
- <label class="form-check-label" for="addressCheck">Check if the school's postal address is the same as the physical address</label>
- </div>
- <!-- This should be hidden if the box above has been checked -->
- <!-- Postal address, usually a P.O. Box -->
- <div class="form-group col-md-12">
- <label for="postalAddress">Postal Address</label>
- <input type="text" class="form-control" id="postalAddress" name="postalAddress" placeholder="Enter your school's postal address">
- </div>
- <div class="form-group col-md-6">
- <label for="city2">City</label>
- <input type="text" class="form-control" id="city2" name="postalCity" placeholder="postalCity">
- </div>
- <div class="form-group col-md-6">
- <label for="zip2">Zip Code</label>
- <input type="text" class="form-control" id="zip2" placeholder="Zip Code">
- </div>
- <!-- End of hidden fields -->
- <div class="form-group col-md-6">
- <label for="telephone">Telephone</label>
- <input type="text" class="form-control" id="telephone" name="telephone" placeholder="Enter your school's Telephone Number">
- </div>
- <div class="form-group col-md-6">
- <label for="fax">Fax</label>
- <input type="text" class="form-control" id="fax" name="email" placeholder="Enter your school's Fax Number">
- </div>
- <div class="form-group col-md-6">
- <label for="exampleFormControlInput1">Email address</label>
- <input type="email" class="form-control" id="exampleFormControlInput1" name="email" placeholder="name@example.com">
- <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
- </div>
- <div class="form-group col-md-6">
- <label for="exampleInputPassword1">Password</label>
- <input type="password" class="form-control" id="exampleInputPassword1" name="password" placeholder="Password">
- </div>
- </div><!-- row ends -->
- </div><!-- End of panel body -->
- </div><!-- School Information panel ends -->
- <!-- Other School Information -->
- <div class="panel panel-default">
- <div class="panel-heading">
- Other School Information
- </div><!-- Panel heading ends -->
- <div class="panel-body">
- <div class="row">
- <div class="form-group col-md-6">
- <label for="schoolTypeControlSelect">Type of School</label>
- <select class="form-control" id="schoolTypeControlSelect" name="schoolType">
- <option>Public</option>
- <option>Private</option>
- <option>Home School</option>
- </select>
- </div>
- <!-- If Public is selected than the following fields become VISIBLE, otherwise they stay hidden -->
- <div class="form-group col-md-6">
- <label for="districtControlSelect">School District</label>
- <select class="form-control" id="districtControlSelect" name="schoolDistrict">
- <option>Choice 1</option>
- <option>Choice 2</option>
- <option>Choice 3</option>
- </select>
- </div>
- <div class="form-group col-md-6">
- <label for="regionControlSelect">School Region</label>
- <select class="form-control" id="regionControlSelect" name="schoolRegion">
- <option>Region 1</option>
- <option>Region 2</option>
- <option>Region 3</option>
- </select>
- </div>
- <!-- End of hidden fields -->
- <div class="form-group col-md-6">
- <label for="curriculumControlSelect">Curriculum</label>
- <select class="form-control" id="curriculumControlSelect" name="curriculum">
- <option>English as a second language</option>
- <option>English as a main language</option>
- <option>Bilingual</option>
- <option>Other</option>
- </select>
- </div>
- <p class="col-md-12">Has your school participated in previous years?</p>
- <div class="col-md-12">
- <div class="form-check">
- <input class="form-check-input" type="radio" name="participationRadio" id="yesRadio" value="yes" checked>
- <label class="form-check-label" for="yesRadio">
- Yes
- </label>
- </div>
- <div class="form-check">
- <input class="form-check-input" type="radio" name="participationRadio" id="noRadio" value="no">
- <label class="form-check-label" for="noRadio">
- No
- </label>
- </div>
- </div>
- <div class="form-group col-md-6">
- <label for="years">How many years participating?</label>
- <input type="text" class="form-control" id="years" placeholder="If your previous answer is YES, enter the number of years" name="participationYears">
- </div>
- </div><!-- form-row ends -->
- </div><!-- Panel body -->
- </div><!-- Other School panel ends -->
- <!-- School Principal/director information -->
- <div class="panel panel-default">
- <div class="panel-heading">
- School Director / Principal Information
- </div>
- <div class="panel-body">
- <div class="form-group">
- <label for="directorName">Director's Name</label>
- <input type="text" class="form-control" id="directorName" placeholder="Enter the School Director's Name" name="directorName">
- </div>
- <div class="form-group">
- <label for="telephone2">Telephone</label>
- <input type="text" class="form-control" id="telephone2" placeholder="Enter your school director's Telephone Number" name="directorTelephone">
- </div>
- <div class="form-group">
- <label for="directorEmail">Email address</label>
- <input type="email" class="form-control" id="directorEmail" placeholder="director@example.com" name="directorEmail">
- </div>
- </div><!-- panel body ends -->
- </div><!-- panel -->
- <!-- The following fields are hidden to users and should ONLY be visible and editable by a site admin-level user. -->
- <div class="panel panel-danger">
- <div class="panel-heading">
- Administrators Only
- </div>
- <div class="panel-body">
- <p>Director Attended Rehersal</p>
- <div class="form-check">
- <input class="form-check-input" type="radio" name="attendanceRadio" id="yesRadio2" value="yes">
- <label class="form-check-label" for="yesRadio2">
- Yes
- </label>
- </div>
- <div class="form-check">
- <input class="form-check-input" type="radio" name="attendanceRadio" id="noRadio2" value="no">
- <label class="form-check-label" for="noRadio2">
- No
- </label>
- </div>
- <p>Director Attended the Competition Event</p>
- <div class="form-check">
- <input class="form-check-input" type="radio" name="eventRadio" id="yesRadio3" value="yes">
- <label class="form-check-label" for="yesRadio3">
- Yes
- </label>
- </div>
- <div class="form-check">
- <input class="form-check-input" type="radio" name="eventRadio" id="noRadio3" value="no">
- <label class="form-check-label" for="noRadio3">
- No
- </label>
- </div>
- </div>
- </div>
- <!-- End of fields hidden to user -->
- <!-- School Liaison Info -->
- <div class="panel panel-default">
- <div class="panel-heading">
- School Liaison
- </div>
- <div class="panel-body">
- <div class="form-group">
- <label for="liaison">Liaison's Name</label>
- <input type="text" class="form-control" id="liaison" placeholder="Enter the School Liaison's Name" name="schoolLiaisonName">
- </div>
- <div class="form-group">
- <label for="telephone3">Telephone</label>
- <input type="text" class="form-control" id="telephone3" placeholder="Enter your school liaison's Telephone Number" name="schoolLiaisonTelephone">
- </div>
- <div class="form-group">
- <label for="liaisonEmail">Email address</label>
- <input type="email" class="form-control" id="liaisonEmail" placeholder="liaison@example.com" name="schoolLiaisonEmail">
- </div>
- <div class="form-group">
- <label for="positionControlSelect">Position</label>
- <select class="form-control" id="positionControlSelect" name="schoolLiaisonPosition">
- <option>Teacher</option>
- <option>Intern</option>
- <option>Coordinator</option>
- <option>Facilitator</option>
- <option>Director</option>
- <option>Other</option>
- </select>
- </div>
- <p>If other, specify position</p>
- <div class="form-group">
- <label for="otherPosition">"Other" position</label>
- <input type="text" class="form-control" id="otherPosition" placeholder="Position" name="schoolLiaisonOtherPosition">
- </div>
- <div class="form-group">
- <label for="sizeControlSelect">T-shirt Size</label>
- <select class="form-control" id="sizeControlSelect" name="schoolLiaisonTShirt">
- <option>Small</option>
- <option>Medium</option>
- <option>Large</option>
- <option>X-Large</option>
- </select>
- </div>
- <div class="form-group">
- <label for="mentorControlSelect">Poet Tutor/Mentor</label>
- <select class="form-control" id="mentorControlSelect" name="schoolLiaisonTutorMentor">
- <option>Jacqueline Jiang</option>
- <option>Mara Pastor</option>
- <option>Penelope</option>
- <option>Rubén Durán Morales</option>
- </select>
- </div>
- </div>
- </div>
- <!-- The following fields are hidden to users and should ONLY be visible and editable by a site admin-level user. -->
- <div class="panel panel-danger">
- <div class="panel-heading">
- Administrators Only
- </div>
- <div class="panel-body">
- <p>Attended Rehersal?</p>
- <div class="form-check">
- <input class="form-check-input" type="radio" name="attendanceRadio2" id="yesLiaisonRadio" value="yes">
- <label class="form-check-label" for="yesLiaisonRadio">
- Yes
- </label>
- </div>
- <div class="form-check">
- <input class="form-check-input" type="radio" name="attendanceRadio2" id="noLiaisonRadio" value="no">
- <label class="form-check-label" for="noLiaisonRadio">
- No
- </label>
- </div>
- <p>Attended the Competition Event?</p>
- <div class="form-check">
- <input class="form-check-input" type="radio" name="eventRadio2" id="yesLiaisonRadio2" value="yes">
- <label class="form-check-label" for="yesLiaisonRadio2">
- Yes
- </label>
- </div>
- <div class="form-check">
- <input class="form-check-input" type="radio" name="eventRadio2" id="noLiaisonRadio2" value="no">
- <label class="form-check-label" for="noLiaisonRadio2">
- No
- </label>
- </div>
- </div>
- </div>
- <!-- End of fields hidden to user -->
- <button type="submit" class="btn btn-primary">Register</button>
- </form>
- </div>
- {{> footer }}
Add Comment
Please, Sign In to add comment