diff --git a/config/config.js b/config/config.js index 18b0d62..8496a17 100644 --- a/config/config.js +++ b/config/config.js @@ -3,5 +3,13 @@ */ // config/auth.js module.exports = { - 'baseURL' : "http://localhost:3000" + 'baseURL' : "http://localhost:3000", + + 'Database' : { + + 'host' : '127.0.0.1', + 'user' : 'asp', + 'password' : 'hidden', + 'database' : 'asp' + } }; \ No newline at end of file diff --git a/config/db.js b/config/db.js new file mode 100644 index 0000000..f5b32a1 --- /dev/null +++ b/config/db.js @@ -0,0 +1,23 @@ +var mysql = require('mysql'); + +var settings = require('./config'); +var db; +var exports = {}; +exports.connectdb = function () { + // if (!db){ + db = mysql.createConnection(settings.Database); + + db.connect(function(err){ + + if(!err) { + console.log('Database is connected!'); + return db; + } else { + console.log('Error connecting database!'+err); + return null; + } + }); + // } +} + +module.exports = exports; diff --git a/config/passport.js b/config/passport.js index 04f23ae..644482c 100644 --- a/config/passport.js +++ b/config/passport.js @@ -4,6 +4,7 @@ var LocalStrategy = require('passport-local').Strategy; var FacebookStrategy = require('passport-facebook').Strategy; var TwitterStrategy = require('passport-twitter').Strategy; +var usermodule = require('../models/user'); // load up the user model var User = require('../models/user'); @@ -43,16 +44,24 @@ module.exports = function(passport) { passReqToCallback : true // allows us to pass back the entire request to the callback }, function(req, email, password, done) { // callback with email and password from our form +console.log('here passport'); + var user={}; + var result = usermodule.login(email,password); + if(result == null ) + { + console.log('null data'); + }else{ + console.log('usercraeted'); + user["email"]=email; + user["status"]=true; + user["name"]="John Snow"; + user["avatar"]=""; + user["loginStatus"]=true; + } /* database check here */ - var user={}; - user["email"]=email; - user["status"]=true; - user["name"]="John Snow"; - user["avatar"]=""; - user["loginStatus"]=true; return done(null, user); })); @@ -77,15 +86,25 @@ module.exports = function(passport) { // User.findOne wont fire unless data is sent back process.nextTick(function() { + var result = usermodule.createuser(email,password); + + var user={}; + if(result == true ) + { + console.log('usercraeted'); + user["email"]=email; + user["status"]=true; + user["name"]="John Snow"; + user["avatar"]=""; + user["loginStatus"]=true; + + }else{ + + console.log('error '); + } /* database check here */ - var user={}; - user["email"]=email; - user["status"]=true; - user["name"]="John Snow"; - user["avatar"]=""; - user["loginStatus"]=true; return done(null, user); }); @@ -109,6 +128,7 @@ module.exports = function(passport) { // asynchronous process.nextTick(function() { + // database code here /*User.findOne({ 'facebook.id' : profile.id }, function(err, user) { @@ -142,6 +162,11 @@ module.exports = function(passport) { });*/ var user={}; + + + var result = usermodule.login(profile.emails[0].value,profile.id); + + user["id"]=profile.id; user["token"]=token; user["email"]=profile.emails[0].value; diff --git a/models/user.js b/models/user.js index 4ab079f..65e64a4 100644 --- a/models/user.js +++ b/models/user.js @@ -1,40 +1,76 @@ -/** - * Created by Shoaib on 11/05/2016. - */ -// models/user.js -// load the things we need -var bcrypt = require('bcrypt-nodejs'); - -// define the schema for our user model -var userSchema = { - - local : { - email : String, - password : String, - }, - facebook : { - id : String, - token : String, - email : String, - name : String - }, - twitter : { - id : String, - token : String, - displayName : String, - username : String - }, - google : { - id : String, - token : String, - email : String, - name : String - } +var exports = {}; +var dbcon = require('../config/db.js'); +var dbconn = dbcon.connectdb(); +exports.login = function(email,password) { + var userdetails = { name:email, password:password}; + if ( dbconn == null ) console.log('still nul'); + else console.log('not nul'); + + dbconn.query("SELECT * FROM users where email = '"+email+"' and password = '"+password +"'", function (err, result) { + if(err) + { + console.log(result[0]+' err'); + return null; + } + else { + console.log(result[0] + ' ress' ); + console.log('query ok'); + return result[0]; + } + // console.log('The solution is: ', result); // object difference result is same + }); }; +exports.fblogin = function(email,profileid) { + + var userdetails = { name:email, password:profileid}; + dbconn.query("SELECT * FROM users where email = '"+email+"' and password = '"+password +"'", function (err, result) { + if(err) + { + console.log(result[0]+' err'); + + var userdetails = { name:email, password:profileid}; + dbconn.query('INSERT INTO users SET ?', userdetails, function(err,res){ + if(err) + {console.log('err in insert fb'); + } + else { + console.log('inserted fb'); + + } + + return null; + }); + } + else { + console.log(result[0] + ' ress' ); + console.log('query ok'); + } + // console.log('The solution is: ', result); // object difference result is same + }); +}; +exports.createuser = function(email,password){ + if(dbconn ==null) + console.log('null');else + console.log('not null'); + + var userdetails = { name:email, password:password}; + + dbconn.query('INSERT INTO users SET ?', userdetails, function(err,res){ + if(err) + { + // throw err; + return null; + } + else { + + console.log('Last insert ID:', res.insertId); + return true; + } + }); + +}; -// create the model for users and expose it to our app -//module.exports = mongoose.model('User', userSchema); -module.exports = userSchema; \ No newline at end of file +module.exports = exports; \ No newline at end of file diff --git a/package.json b/package.json index 4355479..dfa8f3e 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "passport-facebook" : "~1.0.2", "passport-twitter": "~1.0.2", "connect-flash" : "~0.1.1", - "bcrypt-nodejs" : "latest" + "bcrypt-nodejs" : "latest", + "mysql": "~2.12.0" } } diff --git a/public/scripts/globle.js b/public/scripts/globle.js index a7bd6e7..d834b0d 100644 --- a/public/scripts/globle.js +++ b/public/scripts/globle.js @@ -192,21 +192,7 @@ function toggleMapPlanPanle() { $(document).ready(function(){ initMap(); var lastSend = 0; - $("#userLoginForm").submit(function(event) { - /* - @TODO: daniyal , Shoaib - yaha pay login request handle ho gi - */ - return false; - var userLoginData= { - Action: "Login", - email: "shahab@bbs.com", - password: "1234" - }; - socket.emit('login-user-request', userLoginData); - - event.preventDefault(); - }); + // local-login $("#send-message").submit(function(event) { if(pos==null) return false; diff --git a/routes/index.js b/routes/index.js index 2a1b9c6..7c97583 100644 --- a/routes/index.js +++ b/routes/index.js @@ -2,6 +2,7 @@ var express = require('express'); var router = express.Router(); var passport = require('passport'); + require('../config/passport')(passport); // pass passport for configuration var config =require('../config/config'); header = []; @@ -30,6 +31,7 @@ router.get('/', function(req, res, next) { /* GET about page. */ router.get('/about', function(req, res, next) { header["title"] = 'About Us'; + //console.log(req.session); res.render('about', { header: header,user: user,title: 'About us' }); }); @@ -68,7 +70,7 @@ router.post('/signup', passport.authenticate('local-signup', { // process the login form router.post('/login', passport.authenticate('local-login', { successRedirect : '/', // redirect to the secure profile section - failureRedirect : '/login', // redirect back to the signup page if there is an error + failureRedirect : '/', // redirect back to the signup page if there is an error failureFlash : true // allow flash messages })); /*