-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
108 lines (88 loc) · 3.69 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const expresss = require('express');
const database = require('./database/connection');
const router = require('./src/routes/routes');
const bodyParser = require('body-parser');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken')
const app = expresss();
const saltRounds = 10;
const myPassword = "Store-Book";
const myOtherPassword = "Other-Store-Book";
const llave = "Store-Book Password"
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(router);
app.set('llave', llave);
dato = 'Francisco';
let hash = '$2b$10$6giE5XBZpol7mCsREGxbmOoMER3ziv/G1LsBrEp0XxSq4XLEUpgYe';
/* bcrypt.hash(myPassword, saltRounds, function(err, hash) {
if(err) console.log(err)
hash = hash
console.log(hash)
});
bcrypt.compare(myPassword, hash, function(err, result) {
if(err)console.log(err);
console.log(result)
}); */
app.get('/lol3', (req,res)=>{
bcrypt.hash(myPassword, saltRounds, function(err, hash) {
if(err) console.log(err)
hash = hash
console.log(hash)
res.json(hash)
});
/* token = jwt.sign( { data : dato, iat: Math.floor(Date.now() / 1000)} , llave, { expiresIn: '1m' } ) ;
console.log({'hash':token, "fecha":Date()}) // Francisco
res.json({'hash':token, "fecha":Date()}) */
});
app.get('/lol2', (req,res)=>{
bcrypt.compare(myPassword, hash, function(err, result) {
if(err)console.log(err);
console.log(result)
res.json(result)
});
/* jwt.verify(token, llave, function(err, decoded) {
if(err){
err = {
name: 'TokenExpiredError',
message:'Token expired',
expireAt: err.expiredAt
}
console.log(err)
res.send(err)
}else{
console.log({'hash':decoded, "fecha":Date()}) // Francisco
res.json({'hash':decoded, "fecha":Date()})
}
}); */
});
//Create Database Store
database.schema.raw('CREATE DATABASE IF NOT EXISTS Store;').raw('USE Store;').raw('CREATE TABLE IF NOT EXISTS authors(idAuthor INT AUTO_INCREMENT NOT NULL PRIMARY KEY, name VARCHAR(65) NOT NULL, picture VARCHAR(255) NOT NULL);').raw('CREATE TABLE IF NOT EXISTS eBook( id INT(11) AUTO_INCREMENT NOT NULL PRIMARY KEY, idAuthor INT(11) NOT NULL, category VARCHAR(65) NOT NULL, title VARCHAR(255) NOT NULL, summary TEXT NOT NULL, firstParagraph VARCHAR(255) NOT NULL, body VARCHAR(255) NOT NULL, CONSTRAINT idAuthor FOREIGN KEY (idAuthor) REFERENCES authors (idAuthor));').raw('CREATE TABLE IF NOT EXISTS users( id INT(11) AUTO_INCREMENT NOT NULL PRIMARY KEY, username VARCHAR(65) NOT NULL, email VARCHAR(100) NOT NULL UNIQUE, password VARCHAR(65) NOT NULL);').then(data =>{
console.log({message: "Database Store created with success!"})
}).catch(error =>{
console.log(error)
});
//Home page
app.get('/', (req,res,next)=>{
if(req.isAuthenticated()) return next();
//is not logined
console.log("is not logined")
console.log({login: req.isAuthenticated(), user: req.user})
res.send({login: req.isAuthenticated(), user: req.user})
//res.send('<h1>Welcome to my eBook-Store!</h1> <br> <p>Please do it login</p> <br> <p>from login use "/api/login"</p>')
},(req,res)=>{
//is logined
console.log("is logined")
console.log({login: req.isAuthenticated(), user: req.user})
res.send({login: req.isAuthenticated(), user: req.user})
//res.send("<h1>Welcome to my eBook-Store!</h1>")
});
app.get('/lol', (req,res)=>{
database.select('id', 'username').table('users').then( data => {
console.log(data)
res.json(data)
})
});
app.listen(4000,()=> {
console.log("Working in the port 4000" + " http://localhost:4000/");
});