/*requiring node modules starts */
var express = require("express"), router = express.Router();
var app = module.exports = express();
var http = require('http').Server(app);
var io = require("socket.io")(http);
var Session = require('express-session');
var cookieParser = require('cookie-parser');
/*requiring node modules ends */
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
var bcrypt = require('bcryptjs');
// the session is stored in a cookie, so we use this to parse it
app.use(cookieParser());
var Session = Session({
secret:'secrettokenhere',
saveUninitialized: true,
resave: true
});
io.use(function(socket, next) {
Session(socket.request, socket.request.res, next);
});
app.use(Session);
// app.use(Session({secret:'davzk#1234', cookie:{maxAge:7200}}));
var sessionInfo = {};
/* requiring config file */
var config =require('./middleware/config.js')(app);
/* requiring config db.js file */
var db = require("./middleware/db.js");
var connection_object= new db();
var connection=connection_object.connection; // getting conncetion object here
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
// var User = require('./app/models/user'); // get our mongoose model
// get an instance of the router for api routes
var apiRoutes = express.Router();
// route to authenticate a user (POST http://localhost:8080/api/authenticate)
apiRoutes.post('/authenticate', function(req, res) {
// find the user
connection.query("SELECT * FROM users WHERE phone = ? OR email = ? LIMIT 1", [req.body.username, req.body.username], function(err, user) {
if (err) { // throw err;
res.json({
status: 0,
msg: 'Query failed',
});
}
if (!user || !user[0]) {
res.json({ success: false, message: 'Authentication failed. User not found.' });
} else if (user) {
// check if password matches
if (!bcrypt.compareSync(req.body.password, user[0].password)) {
res.json({ success: false, message: 'Authentication failed. Wrong password.' });
} else {
// if user is found and password is right
// create a token
var token = jwt.sign(user[0], app.get('jwt_secret'), {
//expiresInMinutes: 1440 // expires in 24 hours
expiresIn: 120 //120
});
// return the information including token as JSON
res.json({
status: 1,
msg: 'Success',
token: token
});
}
} // End else check (suser)
}); // End query
});
function requireLogin(req, res, next) {
// check header or url parameters or post parameters for token
var token = req.body.token || req.query.token || req.headers['x-access-token'];
// decode token
if (token) {
// verifies secret and checks exp
jwt.verify(token, app.get('jwt_secret'), function(err, decoded) {
if (err) {
return res.json({ success: false, message: 'Failed to authenticate token.' });
} else {
// if everything is good, save to request for use in other routes
req.decoded = decoded;
next();
}
});
} else {
// if there is no token
// return an error (or redirect if in web-route)
return res.status(403).send({
success: false,
message: 'No token provided.'
});
}
}
// route middleware to verify a token
/*apiRoutes.use(function(req, res, next) {
// check header or url parameters or post parameters for token
var token = req.body.token || req.query.token || req.headers['x-access-token'];
// decode token
if (token) {
// verifies secret and checks exp
jwt.verify(token, app.get('jwt_secret'), function(err, decoded) {
if (err) {
return res.json({ success: false, message: 'Failed to authenticate token.' });
} else {
// if everything is good, save to request for use in other routes
req.decoded = decoded;
next();
}
});
} else {
// if there is no token
// return an error
return res.status(403).send({
success: false,
message: 'No token provided.'
});
}
});*/
// apiRoutes.use(requireLogin);
// route to show a random message (GET http://localhost:8080/api/)
apiRoutes.get('/', function(req, res) {
res.json({ message: 'Welcome to the coolest API on earth!' });
});
apiRoutes.get('/users', requireLogin, function(req, res) {
connection.query("SELECT * FROM users WHERE isVerified = ? ",[1], function(err, rows){
res.json(rows);
});
});
// app.use('/', apiRoutes);
require('./middleware/web-routes.js')(app,connection,io,Session,cookieParser,sessionInfo);
app.use('/api', apiRoutes);
require('./middleware/auth-routes.js')(app,connection,io,Session,cookieParser,sessionInfo);
require('./middleware/routes.js')(app,connection,io,Session,cookieParser,sessionInfo);
require('./routes')(app, connection,io,Session,cookieParser,sessionInfo);
require('./middleware/admin-routes.js')(app,connection,io,Session,cookieParser,sessionInfo);
// app.use('/api', apiRoutes);
http.listen(8081,function() {
console.log("Listening on http://localhost:8081");
});
var express = require("express"), router = express.Router();
var app = module.exports = express();
var http = require('http').Server(app);
var io = require("socket.io")(http);
var Session = require('express-session');
var cookieParser = require('cookie-parser');
/*requiring node modules ends */
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
var bcrypt = require('bcryptjs');
// the session is stored in a cookie, so we use this to parse it
app.use(cookieParser());
var Session = Session({
secret:'secrettokenhere',
saveUninitialized: true,
resave: true
});
io.use(function(socket, next) {
Session(socket.request, socket.request.res, next);
});
app.use(Session);
// app.use(Session({secret:'davzk#1234', cookie:{maxAge:7200}}));
var sessionInfo = {};
/* requiring config file */
var config =require('./middleware/config.js')(app);
/* requiring config db.js file */
var db = require("./middleware/db.js");
var connection_object= new db();
var connection=connection_object.connection; // getting conncetion object here
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
// var User = require('./app/models/user'); // get our mongoose model
// get an instance of the router for api routes
var apiRoutes = express.Router();
// route to authenticate a user (POST http://localhost:8080/api/authenticate)
apiRoutes.post('/authenticate', function(req, res) {
// find the user
connection.query("SELECT * FROM users WHERE phone = ? OR email = ? LIMIT 1", [req.body.username, req.body.username], function(err, user) {
if (err) { // throw err;
res.json({
status: 0,
msg: 'Query failed',
});
}
if (!user || !user[0]) {
res.json({ success: false, message: 'Authentication failed. User not found.' });
} else if (user) {
// check if password matches
if (!bcrypt.compareSync(req.body.password, user[0].password)) {
res.json({ success: false, message: 'Authentication failed. Wrong password.' });
} else {
// if user is found and password is right
// create a token
var token = jwt.sign(user[0], app.get('jwt_secret'), {
//expiresInMinutes: 1440 // expires in 24 hours
expiresIn: 120 //120
});
// return the information including token as JSON
res.json({
status: 1,
msg: 'Success',
token: token
});
}
} // End else check (suser)
}); // End query
});
function requireLogin(req, res, next) {
// check header or url parameters or post parameters for token
var token = req.body.token || req.query.token || req.headers['x-access-token'];
// decode token
if (token) {
// verifies secret and checks exp
jwt.verify(token, app.get('jwt_secret'), function(err, decoded) {
if (err) {
return res.json({ success: false, message: 'Failed to authenticate token.' });
} else {
// if everything is good, save to request for use in other routes
req.decoded = decoded;
next();
}
});
} else {
// if there is no token
// return an error (or redirect if in web-route)
return res.status(403).send({
success: false,
message: 'No token provided.'
});
}
}
// route middleware to verify a token
/*apiRoutes.use(function(req, res, next) {
// check header or url parameters or post parameters for token
var token = req.body.token || req.query.token || req.headers['x-access-token'];
// decode token
if (token) {
// verifies secret and checks exp
jwt.verify(token, app.get('jwt_secret'), function(err, decoded) {
if (err) {
return res.json({ success: false, message: 'Failed to authenticate token.' });
} else {
// if everything is good, save to request for use in other routes
req.decoded = decoded;
next();
}
});
} else {
// if there is no token
// return an error
return res.status(403).send({
success: false,
message: 'No token provided.'
});
}
});*/
// apiRoutes.use(requireLogin);
// route to show a random message (GET http://localhost:8080/api/)
apiRoutes.get('/', function(req, res) {
res.json({ message: 'Welcome to the coolest API on earth!' });
});
apiRoutes.get('/users', requireLogin, function(req, res) {
connection.query("SELECT * FROM users WHERE isVerified = ? ",[1], function(err, rows){
res.json(rows);
});
});
// app.use('/', apiRoutes);
require('./middleware/web-routes.js')(app,connection,io,Session,cookieParser,sessionInfo);
app.use('/api', apiRoutes);
require('./middleware/auth-routes.js')(app,connection,io,Session,cookieParser,sessionInfo);
require('./middleware/routes.js')(app,connection,io,Session,cookieParser,sessionInfo);
require('./routes')(app, connection,io,Session,cookieParser,sessionInfo);
require('./middleware/admin-routes.js')(app,connection,io,Session,cookieParser,sessionInfo);
// app.use('/api', apiRoutes);
http.listen(8081,function() {
console.log("Listening on http://localhost:8081");
});
Comments
Post a Comment