Skip to content

Commit 51b9fef

Browse files
committed
ini push
1 parent 41d668b commit 51b9fef

9 files changed

+708
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules

database_create.sql

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CREATE TABLE `user` (
2+
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
3+
`Owner` bigint(20) unsigned NOT NULL,
4+
`firstname` varchar(50) DEFAULT NULL,
5+
`lastname` varchar(50) DEFAULT NULL,
6+
`email` varchar(50) DEFAULT NULL,
7+
`password` varchar(255) DEFAULT NULL,
8+
`token` varchar(255) DEFAULT NULL,
9+
`INSERT_DT` timestamp DEFAULT CURRENT_TIMESTAMP,
10+
`UPDATE_DT` datetime DEFAULT NULL,
11+
PRIMARY KEY (`ID`)
12+
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1;

db.js

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

index.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var express = require('express');
2+
var app = express();
3+
var path = require('path');
4+
var bodyParser = require('body-parser');
5+
6+
/* Getting all the db querys */
7+
const {saveUser,findUser, UpdateToken} = require('./models/user');
8+
/* This middle ware checks if the token given by the user is right */
9+
const {authenticate} = require('./middleware/authenticate');
10+
11+
// The code below allows the node js to find the public directory with the index.html file
12+
const publicPath = path.join(__dirname, './public');
13+
// Node js is using port 3000/ and when you push to cloud it will use process.env.PORT
14+
const port = process.env.PORT || 3000;
15+
16+
// Bodyparser for using json data
17+
app.use(bodyParser.json());
18+
app.use(bodyParser.urlencoded({ extended: false }));
19+
app.use(express.static(publicPath));
20+
21+
22+
/* GET index page */
23+
app.get('/', function(req, res, next) {
24+
res.render('index');
25+
});
26+
27+
/* This function saves a user to the db
28+
It uses promises.
29+
*/
30+
app.post('/createUser',(req,res,next)=>{
31+
saveUser(req.body).then((result)=>{
32+
return res.header('x-auth', result.token).send({email : result.email});
33+
}).catch((e)=>{
34+
return res.status(400).send(e);
35+
});
36+
});
37+
38+
/* When the user from the front-end wants to use a function,
39+
The below code is an example of using the word authenticate to see if the
40+
user is actually authenticated
41+
*/
42+
app.get('/get/me',authenticate,(req,res,next)=>{
43+
res.send(req.user);
44+
});
45+
46+
app.listen(port, () => {
47+
console.log(`Server is up on ${port}`);
48+
});

middleware/authenticate.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const {getUserByToken} = require('../models/user');
2+
3+
// First we call the model using the above code.
4+
// We pass in the token from the request header and see if we can get the
5+
// User or not, if not then we return a 401 and if it works we pass next()
6+
var authenticate = (req,res,next)=>{
7+
var token = req.header('x-auth');
8+
getUserByToken(token).then((result)=>{
9+
req.user = result;
10+
req.token = token;
11+
next();
12+
}).catch((e)=>{
13+
return res.status(401).send();
14+
});
15+
}
16+
17+
module.exports = {authenticate}

models/user.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var db = require('../db');
2+
var jwt = require('jsonwebtoken');
3+
var bcrypt = require('bcryptjs');
4+
5+
getAllUser = () => new Promise((resolve, reject) => {
6+
db.query('SELECT * from user', function (error, results, fields) {
7+
if (error){
8+
reject();
9+
}else{
10+
resolve(results[0]);
11+
}
12+
});
13+
});
14+
15+
saveUser = (userinfo) => new Promise((resolve,reject)=>{
16+
var salt = bcrypt.genSaltSync(10);
17+
var hash = bcrypt.hashSync(userinfo.password, salt);
18+
19+
userinfo.password = hash;
20+
userinfo.token = jwt.sign({Owner : userinfo.Owner},'secretkeyyo');
21+
22+
db.query('INSERT INTO user SET ?',userinfo,function(error,results,fields){
23+
if(error){
24+
reject();
25+
}else{
26+
resolve(userinfo);
27+
}
28+
})
29+
});
30+
31+
getUserByToken = (token) => new Promise((resolve, reject) => {
32+
var decoded ;
33+
try{
34+
decoded = jwt.verify(token,'secretkeyyo');
35+
resolve(decoded);
36+
}catch(e){
37+
reject();
38+
}
39+
});
40+
41+
// The code below export the above functios so it can be used in other files.
42+
module.exports = {
43+
saveUser,
44+
getUserByToken
45+
};

0 commit comments

Comments
 (0)