Skip to content

Commit

Permalink
database files
Browse files Browse the repository at this point in the history
  • Loading branch information
syeddaniyalasif committed Nov 25, 2016
1 parent 866bf24 commit c8dd198
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 65 deletions.
10 changes: 9 additions & 1 deletion config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
};
23 changes: 23 additions & 0 deletions config/db.js
Original file line number Diff line number Diff line change
@@ -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;
49 changes: 37 additions & 12 deletions config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);

}));
Expand All @@ -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);

});
Expand All @@ -109,6 +128,7 @@ module.exports = function(passport) {

// asynchronous
process.nextTick(function() {

// database code here
/*User.findOne({ 'facebook.id' : profile.id }, function(err, user) {
Expand Down Expand Up @@ -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;
Expand Down
106 changes: 71 additions & 35 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -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;
module.exports = exports;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
16 changes: 1 addition & 15 deletions public/scripts/globle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]",
password: "1234"
};
socket.emit('login-user-request', userLoginData);

event.preventDefault();
});
// local-login
$("#send-message").submit(function(event) {
if(pos==null) return false;

Expand Down
4 changes: 3 additions & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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' });
});
Expand Down Expand Up @@ -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
}));
/*
Expand Down

0 comments on commit c8dd198

Please sign in to comment.