Skip to content

Commit 14eca5c

Browse files
committed
initial commit
1 parent ff5f9a5 commit 14eca5c

File tree

644 files changed

+83803
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

644 files changed

+83803
-0
lines changed

config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports={
2+
// secret: used when we create and verify JSON Web Tokens
3+
secret:'jsismagic',
4+
5+
};

database.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var mysql = require('mysql');
2+
3+
var connection = mysql.createConnection({
4+
host : '127.0.0.1',
5+
user : 'root',
6+
password : '',
7+
database : 'lyive',
8+
debug : true
9+
});
10+
11+
connection.connect(function(err) {
12+
if (err) throw err;
13+
});
14+
15+
module.exports = connection;

database.sql

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
2+
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
3+
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
4+
5+
CREATE SCHEMA IF NOT EXISTS `restful_api_demo` DEFAULT CHARACTER SET latin1 ;
6+
USE `restful_api_demo` ;
7+
8+
-- -----------------------------------------------------
9+
-- Table `restful_api_demo`.`user_login`
10+
-- -----------------------------------------------------
11+
DROP TABLE IF EXISTS `restful_api_demo`.`user_login` ;
12+
13+
CREATE TABLE IF NOT EXISTS `restful_api_demo`.`user_login` (
14+
`user_id` INT(70) NOT NULL AUTO_INCREMENT,
15+
`user_email` VARCHAR(45) NOT NULL,
16+
`user_password` VARCHAR(45) NULL,
17+
`user_join_date` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
18+
PRIMARY KEY (`user_id`),
19+
UNIQUE INDEX `user_email_UNIQUE` (`user_email` ASC))
20+
ENGINE = InnoDB;
21+
22+
23+
-- -----------------------------------------------------
24+
-- Table `restful_api_demo`.`user_info`
25+
-- -----------------------------------------------------
26+
DROP TABLE IF EXISTS `restful_api_demo`.`user_info` ;
27+
28+
CREATE TABLE IF NOT EXISTS `restful_api_demo`.`user_info` (
29+
`user_info_id` INT(70) NOT NULL AUTO_INCREMENT,
30+
`user_id_fk` INT(70) NOT NULL,
31+
`user_name` VARCHAR(45) NULL,
32+
`user_location` VARCHAR(45) NULL,
33+
PRIMARY KEY (`user_info_id`),
34+
UNIQUE INDEX `user_id_fk_UNIQUE` (`user_id_fk` ASC),
35+
CONSTRAINT `user_info_foreign_key`
36+
FOREIGN KEY (`user_id_fk`)
37+
REFERENCES `restful_api_demo`.`user_login` (`user_id`)
38+
ON DELETE NO ACTION
39+
ON UPDATE NO ACTION)
40+
ENGINE = InnoDB;
41+
42+
43+
-- -----------------------------------------------------
44+
-- Table `restful_api_demo`.`user_status`
45+
-- -----------------------------------------------------
46+
DROP TABLE IF EXISTS `restful_api_demo`.`user_status` ;
47+
48+
CREATE TABLE IF NOT EXISTS `restful_api_demo`.`user_status` (
49+
`user_status_id` INT(70) NOT NULL AUTO_INCREMENT,
50+
`user_id_fk` INT(70) NOT NULL,
51+
`status_text` TEXT NULL DEFAULT NULL,
52+
`status_time` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
53+
PRIMARY KEY (`user_status_id`),
54+
UNIQUE INDEX `user_id_fk_UNIQUE` (`user_id_fk` ASC),
55+
CONSTRAINT `user_status_foreign_key`
56+
FOREIGN KEY (`user_id_fk`)
57+
REFERENCES `restful_api_demo`.`user_login` (`user_id`)
58+
ON DELETE NO ACTION
59+
ON UPDATE NO ACTION)
60+
ENGINE = InnoDB;
61+
62+
SET SQL_MODE=@OLD_SQL_MODE;
63+
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
64+
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

middleware/addNewUser.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
var mysql = require("mysql");
3+
var express = require("express");
4+
var md5 = require("MD5");
5+
var connection = require("../database");
6+
7+
8+
var addNewUser = function(req,res, next){
9+
var date = new Date();
10+
var post = {
11+
first_name:req.body.first_name,
12+
last_name:req.body.last_name,
13+
email:req.body.email,
14+
password:md5(req.body.password),
15+
dob:req.body.dob,
16+
latitude:req.body.latitude,
17+
longitude:req.body.longitude,
18+
device_type:req.body.device_type,
19+
device_token:req.body.device_token,
20+
// time_zone:req.body.time_zone
21+
22+
};
23+
console.log(post);
24+
var query = "SELECT email FROM ?? WHERE ??=?";
25+
26+
var table = ["user", "email", post.email];
27+
28+
query = mysql.format(query,table);
29+
30+
connection.query(query,function(err,rows){
31+
if(err) {
32+
res.json({"Error" : true, "Message" : "Error executing MySQL query"});
33+
}
34+
else {
35+
36+
if(rows.length==0){
37+
38+
var query = "INSERT INTO ?? SET ?";
39+
var table = ["user"];
40+
query = mysql.format(query,table);
41+
connection.query(query, post, function(err,rows){
42+
if(err) {
43+
res.json({"Error" : true, "Message" : "Error executing MySQL query"});
44+
} else {
45+
res.json({"Error" : false, "Message" : "Success"});
46+
}
47+
});
48+
49+
}
50+
else{
51+
res.json({"Error" : false, "Message" : "Email Id already registered"});
52+
}
53+
}
54+
});
55+
}
56+
57+
module.exports = addNewUser;
58+
59+

middleware/findAllUsers.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
var mysql = require("mysql");
3+
var express = require("express");
4+
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
5+
var config = require('../config');
6+
var connection = require("../database");
7+
8+
var findAllUsers = function (req, res) {
9+
10+
var query = "SELECT * FROM ?? ";
11+
12+
var table = ["user"];
13+
14+
query = mysql.format(query,table);
15+
16+
connection.query(query,function(err,rows){
17+
if(err) {
18+
res.json({"Error" : true, "Message" : "Error executing MySQL query"});
19+
} else {
20+
res.json({"Error" : false, "Message" : "Success", "Users" : rows});
21+
}
22+
});
23+
};
24+
module.exports = findAllUsers;

middleware/userLoginCheck.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
2+
var mysql = require("mysql");
3+
var express = require("express");
4+
var md5 = require("MD5");
5+
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
6+
var config = require('../config');
7+
var connection = require("../database"); // get our config file
8+
9+
10+
var userLoginCheck = function (req, res) {
11+
12+
//var em = req.body.email || req.query.email;
13+
var post = {
14+
password:req.body.password,
15+
email:req.body.email
16+
}
17+
18+
var query = "SELECT * FROM ?? WHERE ??=? AND ??=?";
19+
20+
var table = ["user","password", md5(post.password), "email", post.email];
21+
22+
query = mysql.format(query,table);
23+
24+
connection.query(query,function(err,rows){
25+
if(err) {
26+
res.json({"Error" : true, "Message" : "Error executing MySQL query"});
27+
}
28+
else {
29+
30+
if(rows.length==1){
31+
var token = jwt.sign(rows, config.secret, {
32+
expiresIn: 1440
33+
});
34+
user_id= rows[0].userid;
35+
var data = {
36+
user_id:rows[0].userid,
37+
device_type:rows[0].device_type,
38+
access_token:token,
39+
device_token:rows[0].device_token,
40+
ip_address:rows[0].ip_address
41+
}
42+
var query = "INSERT INTO ?? SET ?";
43+
var table = ["access_token"];
44+
query = mysql.format(query,table);
45+
connection.query(query, data, function(err,rows){
46+
if(err) {
47+
res.json({"Error" : true, "Message" : "Error executing MySQL query"});
48+
} else {
49+
res.json({
50+
success: true,
51+
message: 'Token generated',
52+
token: token,
53+
currUser: user_id
54+
});
55+
} // return info including token
56+
});
57+
}
58+
else {
59+
res.json({"Error" : true, "Message" : "wrong email/password combination"});
60+
}
61+
62+
}
63+
});
64+
}
65+
66+
module.exports = userLoginCheck;
67+
68+
69+
70+
// User.findOne({ email: em }, function (err, user) {
71+
// if (err) {
72+
// res.error(err);
73+
// } else if (!user) {
74+
// // res.json({ success: false, message: 'Authentication failed wrong email' });
75+
// res.sendStatus(401);
76+
77+
// } else if (user) {
78+
// // check if password matches
79+
// if (!bcrypt.compareSync(req.body.password, user.password)) {
80+
// // res.json({ success: false, message: 'Wrong password' })
81+
// res.sendStatus(401);
82+
// } else {
83+
// // create token
84+
// var token = jwt.sign(user, config.secret, {
85+
// expiresInMinutes: 1440
86+
// });
87+
// // return info including token
88+
// res.json({
89+
// success: true,
90+
// message: 'Token generated',
91+
// token: token,
92+
// currUser: user._id
93+
// });
94+
// }
95+
// }
96+
// });
97+
// }

middleware/verifyToken.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
// get our mongoose model
3+
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
4+
var config = require('../config'); // get our config file
5+
6+
/**
7+
* This api is use to verify token for each request,
8+
* when client requests with a token this API decodes that match with existing token and send with
9+
* decoded object.
10+
* We set currUser i.e. current user to req object so we can access somewhere else.
11+
*
12+
*/
13+
14+
var verifyToken=function (req, res,next) {
15+
var token = req.body.token || req.query.token || req.headers['token'];
16+
if (token) {
17+
// verify secret and checks exp
18+
jwt.verify(token, config.secret, function (err, currUser) {
19+
if (err) {
20+
res.send(err);
21+
} else {
22+
// decoded object
23+
req.currUser = currUser;
24+
next();
25+
}
26+
});
27+
}
28+
else {
29+
// send not found error
30+
//res.send(401, " ");
31+
res.status(401).send("Invalid Access");
32+
}
33+
};
34+
module.exports=verifyToken;

middleware/welcome.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports=function(req, res) {
2+
res.send('Welcome..!! Now you are now authenticated !');
3+
};

node_modules/.bin/mime

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/MD5/.npmignore

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/MD5/.travis.yml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/MD5/LICENSE

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)