Move api methods into their own folder
This commit is contained in:
parent
c5393b050e
commit
582db56476
21
app.js
21
app.js
@ -11,7 +11,6 @@ var express = require('express'),
|
||||
bodyParser = require('body-parser'),
|
||||
requireDir = require('require-dir'),
|
||||
connection = require('express-myconnection'),
|
||||
bcrypt = require('bcrypt-nodejs'),
|
||||
mysql = require('mysql2');
|
||||
// ----------------------------------------------------------------------------
|
||||
var app = express();
|
||||
@ -71,13 +70,29 @@ app.use(function(err, req, res, next) {
|
||||
// unless the filename is index.
|
||||
//
|
||||
// A '/' route in the users file becomes the '/users/' route.
|
||||
var routes = requireDir('routes');
|
||||
var routes = requireDir('routes', {recurse: true});
|
||||
Object.keys(routes).forEach(function(route) {
|
||||
var path = (route != 'index')
|
||||
? '/' + route
|
||||
: '/';
|
||||
|
||||
app.use(path, routes[route]);
|
||||
var routeMethod = (typeof routes[route])
|
||||
|
||||
// Handle API routes
|
||||
if (typeof routes[route] === 'object')
|
||||
{
|
||||
var innerRoute;
|
||||
|
||||
for (innerRoute in routes[route])
|
||||
{
|
||||
var innerPath = ['', route, innerRoute].join('/');
|
||||
app.use(innerPath, routes[route][innerRoute]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
app.use(path, routes[route]);
|
||||
}
|
||||
});
|
||||
|
||||
// catch 404 and forward to error handler
|
||||
|
5
public/js/site.js
Normal file
5
public/js/site.js
Normal file
@ -0,0 +1,5 @@
|
||||
(function() {
|
||||
"use strict";
|
||||
|
||||
|
||||
})();
|
@ -19,55 +19,10 @@ router.get('/', function(req, res) {
|
||||
res.render('index', {
|
||||
title: 'Node Task Manager',
|
||||
user: req.session.username,
|
||||
req: request
|
||||
req: request,
|
||||
loggedin: true
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/* Login action */
|
||||
router.post('/login', function(req, res) {
|
||||
var bcrypt = require('bcrypt-nodejs');
|
||||
|
||||
var user = req.body.user,
|
||||
pass = req.body.pass;
|
||||
|
||||
req.getConnection(function(err, connection) {
|
||||
if (err) throw err;
|
||||
|
||||
var sql = " SELECT id, username, email, password, timezone, num_format " +
|
||||
" FROM todo_user " +
|
||||
" WHERE email = ? OR username = ? ";
|
||||
|
||||
// Find the username / email
|
||||
connection.execute(sql, [user, user], function(err, rows, fields) {
|
||||
if (err) throw err;
|
||||
|
||||
var user = rows[0];
|
||||
|
||||
// Verify the password hash
|
||||
bcrypt.compare(pass, user.password, function(err, passRes) {
|
||||
if (err) throw err;
|
||||
|
||||
// Password is good, set session data and redirect
|
||||
if (passRes === true)
|
||||
{
|
||||
req.session.uid = user.id;
|
||||
req.session.num_format = user.num_format;
|
||||
req.session.username = user.username;
|
||||
|
||||
res.redirect(303, '/');
|
||||
}
|
||||
});
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
/* Logout action */
|
||||
router.get('/logout', function(req, res) {
|
||||
// Destroy the session, and redirect to the index page
|
||||
req.session.destroy(function(err) {
|
||||
res.redirect(303, '/');
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
@ -1,6 +1,52 @@
|
||||
var express = require('express');
|
||||
var router = express.Router();
|
||||
|
||||
/* Login action */
|
||||
router.post('/login', function(req, res) {
|
||||
var bcrypt = require('bcrypt-nodejs');
|
||||
|
||||
var user = req.body.user,
|
||||
pass = req.body.pass;
|
||||
|
||||
req.getConnection(function(err, connection) {
|
||||
if (err) throw err;
|
||||
|
||||
var sql = " SELECT id, username, email, password, timezone, num_format " +
|
||||
" FROM todo_user " +
|
||||
" WHERE email = ? OR username = ? ";
|
||||
|
||||
// Find the username / email
|
||||
connection.execute(sql, [user, user], function(err, rows, fields) {
|
||||
if (err) throw err;
|
||||
|
||||
var user = rows[0];
|
||||
|
||||
// Verify the password hash
|
||||
bcrypt.compare(pass, user.password, function(err, passRes) {
|
||||
if (err) throw err;
|
||||
|
||||
// Password is good, set session data and redirect
|
||||
if (passRes === true)
|
||||
{
|
||||
req.session.uid = user.id;
|
||||
req.session.num_format = user.num_format;
|
||||
req.session.username = user.username;
|
||||
|
||||
res.redirect(303, '/');
|
||||
}
|
||||
});
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
/* Logout action */
|
||||
router.get('/logout', function(req, res) {
|
||||
// Destroy the session, and redirect to the index page
|
||||
req.session.destroy(function(err) {
|
||||
res.redirect(303, '/');
|
||||
});
|
||||
});
|
||||
|
||||
/* GET users listing. */
|
||||
router.get('/list', function(req, res) {
|
||||
res.send('respond with a resource');
|
||||
|
@ -5,13 +5,16 @@
|
||||
<link rel="stylesheet" href="/css/ink.min.css" />
|
||||
<link rel="stylesheet" href="/css/ink-flex.min.css" />
|
||||
<link rel="stylesheet" href="/css/font-awesome.min.css" />
|
||||
<script src="/js/ink-all.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<header class="ink-grid">
|
||||
<h1>{title}</h1>
|
||||
<nav class="ink-navigation">
|
||||
<ul class="menu horizontal">
|
||||
<li><a href="/logout">Logout</a></li>
|
||||
<ul class="pagination pills green">
|
||||
{#loggedin}
|
||||
<li><a href="/user/logout">Logout</a></li>
|
||||
{/loggedin}
|
||||
</ul>
|
||||
</nav>
|
||||
</header>
|
||||
|
@ -1,7 +1,7 @@
|
||||
{>layout/}
|
||||
|
||||
{<content}
|
||||
<form class="ink-form column-group" action="/login" method="post">
|
||||
<form class="ink-form column-group all-75 tiny-100" action="/user/login" method="post">
|
||||
<fieldset>
|
||||
<legend>Login</legend>
|
||||
<div class="control-group required">
|
||||
@ -20,7 +20,9 @@
|
||||
</div>
|
||||
<div class="control-group">
|
||||
<input type="hidden" name="_csrf" value="{csrfToken}" />
|
||||
<button type="submit">Login</button>
|
||||
<div class="control">
|
||||
<button class="ink-button green" type="submit">Login</button>
|
||||
</div>
|
||||
</div>
|
||||
</fieldset>
|
||||
</form>
|
||||
|
Reference in New Issue
Block a user