From 353087e4588578c17f280c58508755642cfff840 Mon Sep 17 00:00:00 2001 From: vextv Date: Sat, 26 Apr 2025 17:08:22 +0200 Subject: [PATCH] - added the function to login with email and password - removed an unnecessary empty file --- public/login/login.html | 75 ++++++++---- public/registrieren/passwordValidation.js | 0 public/registrieren/registrieren.html | 133 +++++++++++----------- server.js | 33 +++++- 4 files changed, 148 insertions(+), 93 deletions(-) delete mode 100644 public/registrieren/passwordValidation.js diff --git a/public/login/login.html b/public/login/login.html index 441ef8f..f534d6f 100644 --- a/public/login/login.html +++ b/public/login/login.html @@ -10,31 +10,60 @@ Login - -
+ +
-
- -
+
+ +
- - + + + diff --git a/public/registrieren/passwordValidation.js b/public/registrieren/passwordValidation.js deleted file mode 100644 index e69de29..0000000 diff --git a/public/registrieren/registrieren.html b/public/registrieren/registrieren.html index 5e472ee..4c5135c 100644 --- a/public/registrieren/registrieren.html +++ b/public/registrieren/registrieren.html @@ -1,93 +1,94 @@ - - - - - - - Registrieren + + + + + + + Registrieren -
+
-
-
-

Registrieren

+
+ +

Registrieren

-
- - -
-
- - -
-
- - -
-
- - -
+
+ + +
+
+ + +
+
+ + +
+
+ + +
-
- - -
+
+ + +
- + - - -
+ + +
-
+
diff --git a/server.js b/server.js index b412eba..53a0685 100644 --- a/server.js +++ b/server.js @@ -3,7 +3,7 @@ const session = require('express-session'); const router = require('express').Router(); const path = require('path'); -require('dotenv').config({path:'process.env'}); +require('dotenv').config({path: 'process.env'}); const app = express(); const mysql = require('mysql'); @@ -32,7 +32,7 @@ app.use(session({ })); app.use(express.json()); -app.use(express.urlencoded({ extended: true })); +app.use(express.urlencoded({extended: true})); app.use(express.static(path.join(__dirname, '/scripts'))); app.use(express.static(path.join(__dirname, '/static'))); @@ -127,14 +127,14 @@ app.get('/api/products/sportwagen', async (req, res) => { }); }); -app.post('/api/user/registration', (req,res)=> { +app.post('/api/user/registration', (req, res) => { // SQL-Query für Nutzerregistration const {name, lower_name, email, passwd} = req.body; const sql = "INSERT INTO user (name, lower_name, email, passwd, passwd_hash_algo) VALUES (?, ?, ?, ?, 'none')" // Query abschicken db.query(sql, [name, lower_name, email, passwd], (err, results) => { - if (err){ + if (err) { console.error('Fehler beim Schreiben in die Datenbank: ', err); res.status(500).send('Fehler beim Schreiben in die Datenbank'); return; @@ -143,6 +143,31 @@ app.post('/api/user/registration', (req,res)=> { }) }) +app.post('/api/user/login', (req, res) => { + const {email, password} = req.body + const sql = 'SELECT * FROM user WHERE email = ?' + + db.query(sql, [email], (err, results) => { + if (err) { + console.error('Fehler beim Abrufen des Nutzers: ', err) + return res.status(500).json({message: 'Serverfehler'}) + } + if (results.length === 0) { + return res.status(401).json({message: 'E-Mail nicht gefunden'}) + } + const user = results[0] + + if (user.passwd !== password) { + return res.status(401).json({message: 'Falsches Passwort'}) + } + + req.session.userId = user.id; + req.session.email = user.email; + + res.json({message: 'Login erfolgreich', id: user.id}) + }) +}) + const getIndexRoute = require('./scripts/routes/other/route-index'); app.use('/', getIndexRoute);