const mysql = require('mysql') const express = require('express') const session = require('express-session') const handlebars = require('handlebars') const path = require('path') const fs = require('fs') console.log('[MySQL]: Connecting ...') const connection = mysql.createConnection({ host : 'localhost', user : 'root', password : '', database : 'nodelogin' }) const staticPath = path.join(__dirname, '..', 'static') const dynPath = path.join(__dirname, '..', 'dynamic') console.log('[MySQL]: Ok') const app = express() app.use(session({ secret: 'secret', resave: true, saveUninitialized: true, })) app.use(express.json()) app.use(express.urlencoded({ extended: true })) app.use(express.static(staticPath)) app.get('/', (req, res) => { res.sendFile(path.join(staticPath, 'login.html')) }) app.post('/login', function(req, res) { try { const username = req.body.username const email = req.body.email const kind = req.body.kind const password = req.body.password console.log(`Someone wants to ${(kind ?? '???').toLowerCase()}: `, req.body) if (kind === 'Regisztráció') { if (username && password && email) { console.log(`Searching for user with email \"${email}\" and password \"${password}\" ...`) connection.query('SELECT * FROM accounts WHERE email = ? AND password = ?', [email, password], (error, results) => { if (error) { console.error(error) res.send(handlebars.compile(fs.readFileSync(path.join(dynPath, 'error.hbs'), 'utf8'))({ error: error + '' })) res.end() return } if (results.length > 0) { console.log(`User with email \"${email}\" and password \"${password}\" found (already exists)`) res.send(handlebars.compile(fs.readFileSync(path.join(dynPath, 'error.hbs'), 'utf8'))({ error: 'A felhasználó már létezik' })) res.end() } else { console.log(`User with email \"${email}\" and password \"${password}\" not found, registering ...`) connection.query('INSERT INTO accounts (`username`, `password`, `email`) VALUES (?, ?, ?)', [username, password, email], (error2) => { if (error2) { console.error(error2) res.send(handlebars.compile(fs.readFileSync(path.join(dynPath, 'error.hbs'), 'utf8'))({ error: error2 + '' })) res.end() return } console.log(`User with email \"${email}\", name \"${username}\" and password \"${password}\" added (registered)`) req.session.loggedin = true req.session.email = email req.session.username = username res.redirect('/home') }) } }) } else { res.send(handlebars.compile(fs.readFileSync(path.join(dynPath, 'error.hbs'), 'utf8'))({ error: 'Adjon meg egy felhasználónevet, email-t és jelszavat' })) res.end() } } else if (kind === 'Bejelentkezés') { if (password && email) { connection.query('SELECT * FROM accounts WHERE email = ? AND password = ?', [email, password], (error, results) => { if (error) { console.error(error) res.send(handlebars.compile(fs.readFileSync(path.join(dynPath, 'error.hbs'), 'utf8'))({ error: error + '' })) res.end() return } if (results.length > 0) { console.log(`User logged in`) req.session.loggedin = true req.session.email = email req.session.username = username res.redirect('/home') } else { res.send(handlebars.compile(fs.readFileSync(path.join(dynPath, 'error.hbs'), 'utf8'))({ error: 'Hibás jelszó vagy email' })) } res.end() }) } else { res.send(handlebars.compile(fs.readFileSync(path.join(dynPath, 'error.hbs'), 'utf8'))({ error: 'Adjon meg egy email-t és jelszavat' })) res.end() } } else { res.send(handlebars.compile(fs.readFileSync(path.join(dynPath, 'error.hbs'), 'utf8'))({ error: 'Bánszky valamit elrontott' })) res.end() } } catch (error) { console.error(error) res.send(handlebars.compile(fs.readFileSync(path.join(dynPath, 'error.hbs'), 'utf8'))({ error: error + '' })) res.end() } }); app.get('/home', (req, res) => { if (req.session.loggedin) { res.send(handlebars.compile(fs.readFileSync(path.join(dynPath, 'hey.hbs'), 'utf8'))({ username: req.session.username })) } else { res.send(handlebars.compile(fs.readFileSync(path.join(dynPath, 'error.hbs'), 'utf8'))({ error: 'JELENTKEZZ BE!!!' })) } res.end() }) app.listen(3000)