Move api methods into their own folder
This commit is contained in:
parent
c5393b050e
commit
582db56476
19
app.js
19
app.js
@ -11,7 +11,6 @@ var express = require('express'),
|
|||||||
bodyParser = require('body-parser'),
|
bodyParser = require('body-parser'),
|
||||||
requireDir = require('require-dir'),
|
requireDir = require('require-dir'),
|
||||||
connection = require('express-myconnection'),
|
connection = require('express-myconnection'),
|
||||||
bcrypt = require('bcrypt-nodejs'),
|
|
||||||
mysql = require('mysql2');
|
mysql = require('mysql2');
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
var app = express();
|
var app = express();
|
||||||
@ -71,13 +70,29 @@ app.use(function(err, req, res, next) {
|
|||||||
// unless the filename is index.
|
// unless the filename is index.
|
||||||
//
|
//
|
||||||
// A '/' route in the users file becomes the '/users/' route.
|
// 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) {
|
Object.keys(routes).forEach(function(route) {
|
||||||
var path = (route != 'index')
|
var path = (route != 'index')
|
||||||
? '/' + route
|
? '/' + 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]);
|
app.use(path, routes[route]);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// catch 404 and forward to error handler
|
// 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', {
|
res.render('index', {
|
||||||
title: 'Node Task Manager',
|
title: 'Node Task Manager',
|
||||||
user: req.session.username,
|
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;
|
module.exports = router;
|
||||||
|
@ -1,6 +1,52 @@
|
|||||||
var express = require('express');
|
var express = require('express');
|
||||||
var router = express.Router();
|
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. */
|
/* GET users listing. */
|
||||||
router.get('/list', function(req, res) {
|
router.get('/list', function(req, res) {
|
||||||
res.send('respond with a resource');
|
res.send('respond with a resource');
|
||||||
|
@ -5,13 +5,16 @@
|
|||||||
<link rel="stylesheet" href="/css/ink.min.css" />
|
<link rel="stylesheet" href="/css/ink.min.css" />
|
||||||
<link rel="stylesheet" href="/css/ink-flex.min.css" />
|
<link rel="stylesheet" href="/css/ink-flex.min.css" />
|
||||||
<link rel="stylesheet" href="/css/font-awesome.min.css" />
|
<link rel="stylesheet" href="/css/font-awesome.min.css" />
|
||||||
|
<script src="/js/ink-all.min.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<header class="ink-grid">
|
<header class="ink-grid">
|
||||||
<h1>{title}</h1>
|
<h1>{title}</h1>
|
||||||
<nav class="ink-navigation">
|
<nav class="ink-navigation">
|
||||||
<ul class="menu horizontal">
|
<ul class="pagination pills green">
|
||||||
<li><a href="/logout">Logout</a></li>
|
{#loggedin}
|
||||||
|
<li><a href="/user/logout">Logout</a></li>
|
||||||
|
{/loggedin}
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
</header>
|
</header>
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
{>layout/}
|
{>layout/}
|
||||||
|
|
||||||
{<content}
|
{<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>
|
<fieldset>
|
||||||
<legend>Login</legend>
|
<legend>Login</legend>
|
||||||
<div class="control-group required">
|
<div class="control-group required">
|
||||||
@ -20,7 +20,9 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="control-group">
|
<div class="control-group">
|
||||||
<input type="hidden" name="_csrf" value="{csrfToken}" />
|
<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>
|
</div>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
</form>
|
</form>
|
||||||
|
Reference in New Issue
Block a user