const express 	= require('express')
const path 		= require('path')
const passport 	= require('passport')

const router = express.Router()

const db = require('../config/db')

const BCRYPT_ROUNDS = 13

router.get('/', function (req, res) { 
	res.sendFile('index.html', { root: path.join(__dirname, '../views') }) 
})

router.get('/loggedin', function (req, res) {
	res.send(req.isAuthenticated() ? req.user : '0')
})

router.post('/login', function (req, res, next) {
	passport.authenticate('local', function (err, user, info) {
		if (err) { return next(err) }
		if (!user) {
			return res.send('invalid')
		}
		if (user.verified === 0){
			return res.send('unverified')
		}
		req.logIn(user, function (err) {
			if (err) { return next(err) }
			return res.send(req.user)
		})
	})(req, res, next)
})

router.post('/register', function(req, res) {
	var user = { 
		email	: req.body.email,
		type 	: req.body.type,
		pass 	: req.body.pass,
		first 	: req.body.first,
		last 	: req.body.last
	}

	db.query('SELECT 1 FROM Users WHERE email=? LIMIT 1;', [user.email]).then( function (result){
		if(result[0][0]){
			res.send('0')
		}else{
			return bcrypt.hash(user.pass, BCRYPT_ROUNDS, function (err, hash) {
				return db.query('INSERT INTO Users (userType, email, lastName, firstName, password) VALUES (?,?,?,?,?);', [user.type, user.email, user.first, user.last, hash]).then( function (result){
					user.user_id = result[0].insertId
					res.send('2')
				})
			})
		}
	}).catch( function (err){
		console.error(err)
	}).done()
})

router.get('/logout', function (req, res){
	if(req.user){ req.logout() }
	res.redirect('/')
})

router.get('*', function (req, res) {
	//catch all other requests
	res.sendFile('index.html', { root: path.join(__dirname, '../views') })
})

module.exports = router
@@ -807,16 +807,23 @@
 .post('/
+doctor-
 register
@@ -833,16 +833,17 @@
 function
+ 
 (req, re
@@ -902,21 +902,9 @@
  %09: 
-req.body.type
+0
 ,%0A%09%09
@@ -980,18 +980,314 @@
 last%0A%09%7D%0A
-%0A
 %09
+register(req, res, user)%0A%7D)%0A%0Arouter.post('/patient-register', function (req, res) %7B%0A%09var user = %7B %0A%09%09email%09: req.body.email,%0A%09%09type %09: 1,%0A%09%09pass %09: req.body.pass,%0A%09%09first %09: req.body.first,%0A%09%09last %09: req.body.last%0A%09%7D%0A%09register(req, res, user)%0A%7D)%0A%0Avar register = function (req, res, user)%7B%0A%09return 
 db.query
@@ -1813,17 +1813,16 @@
 done()%0A%7D
-)
 %0A%0Arouter

split register functionality to patient/doctor