Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
itajenglish committed Feb 12, 2017
1 parent 5adba50 commit 6fe4919
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 42 deletions.
44 changes: 7 additions & 37 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//Require NPM Packages
var express = require('express'),
const express = require('express'),
app = express(),
mustache = require('mustache-express'),
pgp = require('pg-promise')(),
Expand All @@ -11,7 +11,8 @@ var express = require('express'),
fetch = require('node-fetch');
var db = pgp(process.env.DATABASE_URL || 'postgres://tajenglish@localhost:5432/quedj');

const User = require('./routes/User.js');
const USER_ROUTER = require('./controllers/Users');
const SESSION_ROUTER = require('./controllers/Sessions')


//configure express and related packages
Expand All @@ -20,12 +21,13 @@ app.set('view engine', 'html');
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/views');
app.use(methodOverride('__method')); //method override
app.use(morgan('dev'));
app.use(morgan('dev')); // Log All Requests
app.use(bdPars.urlencoded({
extended: false
})); //body parser
app.use(bdPars.json()); //body parser


app.use(session({
secret: 'test',
resave: false,
Expand Down Expand Up @@ -63,31 +65,6 @@ app.get('/', function(req, res) {
}
});

//Register Route
app.post('/register', function(req, res) {
function capitalFunc(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
var data = req.body;
var fname = capitalFunc(data.first_name);
var lname = capitalFunc(data.last_name);
var image = "images/Music-Dj-icon.png"
if (data.accountPicker === "dj") {
bcrypt.hash(data.password, 10, function(err, hash) {
db.none("INSERT INTO djs (First_Name,Last_Name,type,email,image,location,password) VAlUES ($1,$2,$3,$4,$5,$6,$7)", [fname, lname, data.accountPicker, data.email, image, data.location, hash]).then(function(data) {
console.log(data);
res.redirect("/login");
});
});
} else {
bcrypt.hash(data.password, 10, function(err, hash) {
db.none("INSERT INTO fans (First_Name,Last_Name,type,email,image,location,password) VAlUES ($1,$2,$3,$4,$5,$6,$7)", [fname, lname, data.accountPicker, data.email, image, data.location, hash]).then(function(data) {
console.log(data);
res.redirect("/login");
});
});
}
});

//Update Info Route
app.put('/updateInfo', function(req, res) {
Expand All @@ -108,12 +85,9 @@ app.put('/updateInfo', function(req, res) {

});

//Login Routes
// app.get('/login', function(req, res) {
// res.render('home/login');
// });
app.use('/', USER_ROUTER)
app.use('/', SESSION_ROUTER)

app.use('/', User)

// app.post('/login', function(req, res) {
// var data = req.body;
Expand All @@ -135,10 +109,6 @@ app.use('/', User)
// })
// })

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

//Dashboard Routes
app.get('/djboard', function(req, res) {
Expand Down
21 changes: 21 additions & 0 deletions controllers/Sessions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const express = require('express');
const router = express.Router();
const { authenticate } = require('../models/User.js');

//USER#NEW
router.get('/login', (req, res, next) => {
res.render('home/login');
});

//USER#CREATE
router.post('/login', authenticate, (req, res, next) => {
res.redirect('/');
});

//USER#DESTORY
router.get('/logout', (req, res) => {
req.session.destroy();
res.redirect('/');
});

module.exports = router;
25 changes: 25 additions & 0 deletions controllers/Users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const express = require('express');
const router = express.Router();
const { authenticate, register } = require('../models/User.js');

//Get Routes
router.get('/login', (req, res, next) => {
res.render('home/login');
});

router.get('/logout', (req, res) => {
req.session.destroy();
res.redirect('/');
});

//Post Routes
router.post('/login', authenticate, (req, res, next) => {
res.redirect('/');
});

router.post('/register', register, (req, res, next) => {
res.redirect('/');
});


module.exports = router;
3 changes: 3 additions & 0 deletions lib/helpers/capitalName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = capitalName = (string) => {
return string.charAt(0).toUpperCase() + string.slice(1);
}
38 changes: 36 additions & 2 deletions models/User.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
const db = require('../lib/db');
const bcrypt = require('bcrypt');
const capitalName = require('../lib/helpers/capitalName');


//Checks Database for user and sets session to user object
const authenticate = (req, res, next) => {

const data = req.body;

db.one(
"(SELECT * FROM fans where email = $1) UNION (SELECT * FROM djs where email = $1)", [data.email])
.then((user) => {

bcrypt.compare(data.password, user.password, (err, cmp) => {
if (cmp) {
req.session.user = user;
delete req.session.user.password;
next();
} else {
res.send('Email/Password not found.');
Expand All @@ -18,6 +24,34 @@ const authenticate = (req, res, next) => {
}).catch(() => {
res.send('Email/Password not found.');
})
}
};

module.exports = authenticate;
//Sends User Data into the Database
const register = (req, res, next) => {

const data = req.body;
const fname = capitalName(data.first_name);
const lname = capitalName(data.last_name);
const image = "images/Music-Dj-icon.png"

if (data.accountPicker === "dj") {

bcrypt.hash(data.password, 10, (err, hash) => {
db.none("INSERT INTO djs (First_Name,Last_Name,type,email,image,location,password) VAlUES ($1,$2,$3,$4,$5,$6,$7)", [fname, lname, data.accountPicker, data.email, image, data.location, hash]).then(function(data) {
console.log(data);
next();
});
});

} else {

bcrypt.hash(data.password, 10, (err, hash) => {
db.none("INSERT INTO fans (First_Name,Last_Name,type,email,image,location,password) VAlUES ($1,$2,$3,$4,$5,$6,$7)", [fname, lname, data.accountPicker, data.email, image, data.location, hash]).then(function(data) {
console.log(data);
next();
});
});
}

}
module.exports = { authenticate, register } ;
11 changes: 8 additions & 3 deletions routes/User.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
const express = require('express');
s;const express = require('express');
const router = express.Router();
const authenticate = require('../models/User.js')
const { authenticate, register } = require('../models/User')

router.get('/login', (req, res, next) => {
res.render('home/login');
});

router.post('/login', authenticate, (req, res, next) => {
res.redirect('/');
})
});

router.post('/register', register, (req, res, next) => {
res.redirect("/login");

});

module.exports = router;

0 comments on commit 6fe4919

Please sign in to comment.