-
Notifications
You must be signed in to change notification settings - Fork 27
/
app.js
83 lines (66 loc) · 2.03 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
const express = require('express');
const mysql = require("mysql")
const path = require("path")
const dotenv = require('dotenv')
const jwt = require("jsonwebtoken")
const bcrypt = require("bcryptjs")
dotenv.config({ path: './.env'})
const app = express();
const db = mysql.createConnection({
host: process.env.DATABASE_HOST,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE
})
const publicDir = path.join(__dirname, './public')
app.use(express.static(publicDir))
app.use(express.urlencoded({extended: 'false'}))
app.use(express.json())
app.set('view engine', 'hbs')
db.connect((error) => {
if(error) {
console.log(error)
} else {
console.log("MySQL connected!")
}
})
app.get("/", (req, res) => {
res.render("index")
})
app.get("/register", (req, res) => {
res.render("register")
})
app.get("/login", (req, res) => {
res.render("login")
})
app.post("/auth/register", (req, res) => {
const { name, email, password, password_confirm } = req.body
db.query('SELECT email FROM users WHERE email = ?', [email], async (error, result) => {
if(error){
console.log(error)
}
if( result.length > 0 ) {
return res.render('register', {
message: 'This email is already in use'
})
} else if(password !== password_confirm) {
return res.render('register', {
message: 'Password Didn\'t Match!'
})
}
let hashedPassword = await bcrypt.hash(password, 8)
console.log(hashedPassword)
db.query('INSERT INTO users SET?', {name: name, email: email, password: hashedPassword}, (err, result) => {
if(error) {
console.log(error)
} else {
return res.render('register', {
message: 'User registered!'
})
}
})
})
})
app.listen(5000, ()=> {
console.log("server started on port 5000")
})