<repo_name>piotrmrozit/honda<file_sep>/gulpfile.js
/**
 * Created by wins on 2016-06-19.
 */
const gulp = require("gulp");
const less = require('gulp-less');
const cssnano = require('gulp-cssnano');
const nodemon = require('gulp-nodemon');


gulp.task('less', function () {
	return gulp.src('./src/less/**/*.less')
		.pipe(less())
		.pipe(cssnano())
		.pipe(gulp.dest('./public/css'));
});

gulp.task('dust', function () {
	return gulp.src('./src/views/**/*.dust')
		.pipe(gulp.dest('./public/views'));
});

gulp.task('js',function () {
	return gulp.src('./src/views/js/**/*.js')
		.pipe(gulp.dest('./public/views/js'));
});

gulp.task('default',['less','dust','js','watch','start']);


gulp.task('watch', function() {
	gulp.watch('./src/less/**/*.less', ['less']);
	gulp.watch('./src/views/**/*.dust', ['dust']);
	// gulp.watch('./src/js/**/*.js', ['js']);
});

gulp.task('start', function () {
	nodemon({
		script: 'bin/www',
		ext: 'js',
		watch: ['routes']
	})
});

<file_sep>/lib/middleware/isAuth.js
/**
 * Created by wins on 2016-06-19.
 */
module.exports = function() {
	return function(req, res, next) {
		if(req.session.logged==undefined){
			req.session.logged = false;
		}
		if(req.session.logged==false){
			res.redirect('/login')
		} else {
			next();
		}
	}
};<file_sep>/routes/index.js
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var isAuth = require('../lib/middleware/isAuth');
var connection = mysql.createConnection({
	host: 'localhost',
	user: 'root',
	password: '',
	database: 'hnd'
});
connection.connect();

/* GET home page. */
router.get('/', function (req, res, next) {
	var dane = {
		title: 'Hondziarz.pl',
		zalogowany: false,
		uzytkownik: {
			email: 'brak'
		}
	};

	if (req.session.logged != undefined) {
		dane.zalogowany = req.session.logged,
			dane.uzytkownik = req.session.user
	}

	res.render('index', dane);
});

/* GET login page. */
router.get('/login', function (req, res, next) {
	res.render('login', {title: 'Hondziarz.pl'});
});

/* GET signup page. */
router.get('/signup', function (req, res, next) {
	var response = {
		error: false,
		message: null,
		title: 'Hondziarz.pl'
	};
	res.render('signup', response);
});

router.post('/signup', function (req, res, next) {
	console.log(' -> ', req.body);
	var response = {
		error: false,
		message: null,
		title: 'Hondziarz.pl'
	};
	if (req.body.email == '' || req.body.password == '') {
		response.error = true;
		response.message = "Error! Wypełnij wszystkie pola!";
	}
	if (req.body.password != '' && req.body.password.length < 6) {
		response.error = true;
		response.message = "Error! Hasło musi mieć więcej niż 6 znaków!!";
	}

	var query_check = "SELECT * FROM users WHERE users_email = '" + req.body.email + "'";

	connection.query(query_check, function (err, rows, fields) {
		if (rows[0] != undefined) {
			response.error = true;
			response.message = "Ten mail jest już zajęty.";
			res.render('signup', response);
		} else {
			if (response.error) {
				res.render('signup', response);
			}

			var query = "INSERT INTO `users`(`users_email`, `users_password`) VALUES ('" + req.body.email + "','" + req.body.password + "')";

			connection.query(query, function (err, rows, fields) {
				if (err) {
					response.error = true;
					response.message = "Error! " + err;
					res.render('signup', response);
				}

				req.session.logged = true;
				req.session.user = {
					email: req.body.email,
					password: <PASSWORD>
				};
				console.log("redirect");
				res.redirect('/');
			});
		}
	});

	


});


/* GET logout page. */
router.get('/logout', function (req, res, next) {
	if (req.session.logged == true) {
		req.session.logged = false;
		delete  req.session.user;
	}
	res.redirect('/');
});

/* GET login page. */
router.post('/login', function (req, res, next) {
	console.log(' -> ', req.body);
	var query_back = "SELECT users_email, users_password FROM users WHERE users_email = '" + req.body.email + "'";
	var response = {
		error: false,
		message: null,
		title: 'Hondziarz.pl'
	};
	connection.query(query_back, function (err, rows, fields) {
		console.log('rows -> ', rows[0]);
		if (rows[0] == undefined) {
			response.error = true;
			response.message = "Nie ma takiego użytkownika, zarejestruj się!"
		}
		else {
			if (req.body.password != rows[0].users_password) {
				response.error = true;
				response.message = "Nieprawidłowe dane";
			}
			else {
				req.session.logged = true;
				req.session.user = {
					email: req.body.email,
					password: <PASSWORD>
				};
				res.redirect('/');
			}
		}


		if (response.error) {
			res.render('login', response);
		}

	});
});

router.get('/abc', isAuth(), function(req,res,next){
	res.json({success: true});
});

module.exports = router;
