Last active
October 30, 2015 22:13
-
-
Save joshbedo/8948143 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'), | |
app = express(), | |
server = require('http').createServer(app); | |
io = require('socket.io').listen(server), | |
jwt = require("jwt-simple"), | |
secret = "dgv33rggr3ewsfsf23gf2grwrf", | |
crypto = require('crypto'), | |
uuid = require('node-uuid'), | |
geoip = require('geoip-lite'), | |
storeStats = require('./storeStats.js'), | |
api = require('./api/api-endpoints'); | |
server.listen(3000); | |
app.use("/css", express.static(__dirname + '/public/css')); | |
app.use("/js", express.static(__dirname + '/public/js')); | |
app.use("/js/app", express.static(__dirname + '/public/js/app')); | |
app.use("/js/lib", express.static(__dirname + '/public/js/lib')); | |
app.use("/font", express.static(__dirname + '/public/font')); | |
app.use("/images", express.static(__dirname + '/public/images')); | |
app.use(express.bodyParser()); | |
app.use(express.cookieParser()); | |
app.engine('.html', require('ejs').__express); | |
app.set('views', __dirname + '/public'); | |
app.set('view engine', 'html'); | |
//session storage | |
store = new express.session.MemoryStore; | |
app.use(express.session({ secret: '2edc$rfv287', store: store})); | |
/*var mongoose = require('mongoose'), | |
db = mongoose.connect('mongodb://localhost/storeStats'), | |
Schema = mongoose.Schema, ObjectId = Schema.ObjectId; | |
*/ | |
var _ = require('lodash'), | |
databaseUrl = "mongodb://localhost/storeStats", | |
collections = ["users", "products"], | |
db = require("mongojs").connect(databaseUrl, collections); | |
var Analytics = module.exports.Analytics = function() { | |
this.routes = app; | |
this.db = db; | |
this.getClientLocation = function(req) { | |
var ipAddress = req; | |
var forwardedIpStr = (req.header && req.header('x-forwarded-for')) ? | |
req.header('x-forwarded-for') : | |
undefined; | |
if(forwardedIpStr) { | |
//'x-forwarded-fr' header may return multiple IPs | |
//the format: "client Ip, procy 1 IP, proxy 2 IP" so take | |
//the first one | |
var forwardedIps = forwardedIps.split(','); | |
ipAddress = forwardedIps[0]; | |
} | |
if(!ipAddress) { | |
ipAddress = req.connection.remoteAddress; | |
} | |
return geoip.lookup(ipAddress); | |
}; | |
this.isLogged = function(session, cookie) { | |
hasSession = (session === undefined) ? false : true; | |
hasCookie = (cookie === undefined) ? false : true; | |
if(hasSession) { | |
return session; | |
} | |
//more logic checking cookies here later on | |
return (hasCookie) ? cookie : false; | |
}; | |
this.loginUser = function(user, callback) { | |
var userid = user.userid, | |
password = (user.password) ? | |
crypto.createHash('md5').update(user.password).digest('hex'): | |
undefined; | |
rememberme = user.rememberme; | |
if(userid && password) { | |
var pattern = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/; | |
if( userid.match( pattern ) ) { | |
db.users.find({ email: userid, password: password }, function(err, user) { | |
if(err) throw err; | |
var logincode = uuid.v4(), | |
token = jwt.encode({email: userid, password: password}, secret); | |
//generate a token which gets passed into headers | |
if(rememberme) { | |
res.cookie('clogincode', logincode, { magAge: 900000 } ); | |
} | |
db.users.findAndModify({ | |
query: { email: userid, password: password }, | |
update: { $set: { logincode: logincode } }, | |
new: true | |
}, function(err, user) { | |
return (user) ? callback(user, null) : callback(null, "User could not be logged in"); | |
}); | |
}); | |
} | |
} else { | |
return callback(null, "Missing userid or password"); | |
} | |
} | |
this.validateUser = function(req, callback) { | |
var user = req.body, | |
pattern = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/, | |
username = user.username, | |
name = user.name, | |
email = user.email, | |
location = this.getClientLocation(req), | |
password = (user.password) ? | |
crypto.createHash('md5').update(user.password).digest("hex") : | |
undefined, | |
apiKey = (username && email) ? | |
crypto.createHash('md5').update(username + email).digest("hex") : | |
undefined; | |
if(username && name && email && password && apiKey && location) { | |
if(email.match( pattern )) { | |
db.users.findOne({ email: email }, function(err, found) { | |
if(err) callback(false); | |
if(found === null) { | |
var User = { | |
username: username, | |
name: name, | |
email: email, | |
password: password, | |
apiKey: apiKey, | |
location: location | |
}; | |
callback(User); | |
} else { | |
callback(false); | |
} | |
}); | |
} else { | |
callback(false); | |
} | |
} else { | |
callback(false); | |
} | |
} | |
this.purchase = function(req, callback) { | |
var products = req.products || undefined, | |
total = req.total || undefined, | |
location = this.getClientLocation(req.ip), | |
apiKey = req.apiKey || undefined, | |
time = new Date().getTime(); | |
if(products && total) { | |
callback({date: time, location: location, apiKey: apiKey, products: products, total: total}); | |
}else{ | |
callback(null, true); | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment