first commit

This commit is contained in:
Klaas Börgmann 2024-09-24 11:44:20 +02:00
commit 61956b562c
7 changed files with 122 additions and 0 deletions

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
.env
.DS_STORE
.DS_Store
server/node_modules/*
app/node_modules/*
package-lock.json
package.json

0
LICENSE.md Normal file
View File

0
README.md Normal file
View File

6
server/.env_default Normal file
View File

@ -0,0 +1,6 @@
PORT=3000
DB_HOST=localhost
DB_USER=postgres
DB_PASSWORD=your_db_password
DB_NAME=your_database_name
JWT_SECRET=your_jwt_secret

17
server/app.js Normal file
View File

@ -0,0 +1,17 @@
require("dotenv").config();
const express = require("express");
const app = express();
const authRoutes = require("./routes/auth");
const bodyParser = require("body-parser");
// Middleware
app.use(bodyParser.json());
// Routes
app.use("/auth", authRoutes);
// Server starten
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server läuft auf Port ${PORT}`);
});

View File

@ -0,0 +1,81 @@
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const { Pool } = require("pg");
// Datenbankverbindung
const pool = new Pool({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
port: 5432,
});
// Registrierungsfunktion
const registerUser = async (req, res) => {
const { username, password, email, role } = req.body;
try {
// Überprüfen, ob der Benutzer bereits existiert
const userExists = await pool.query(
"SELECT * FROM Users WHERE email = $1",
[email]
);
if (userExists.rows.length > 0) {
return res.status(400).json({ message: "Benutzer existiert bereits" });
}
// Passwort hashen
const hashedPassword = await bcrypt.hash(password, 10);
// Neuen Benutzer einfügen
const newUser = await pool.query(
"INSERT INTO Users (username, password, email, role, is_active) VALUES ($1, $2, $3, $4, $5) RETURNING *",
[username, hashedPassword, email, role, true]
);
res
.status(201)
.json({ message: "Benutzer registriert", user: newUser.rows[0] });
} catch (err) {
res
.status(500)
.json({ message: "Fehler beim Registrieren", error: err.message });
}
};
// Loginfunktion
const loginUser = async (req, res) => {
const { email, password } = req.body;
try {
// Überprüfen, ob der Benutzer existiert
const user = await pool.query("SELECT * FROM Users WHERE email = $1", [
email,
]);
if (user.rows.length === 0) {
return res.status(400).json({ message: "Benutzer nicht gefunden" });
}
// Passwort vergleichen
const validPassword = await bcrypt.compare(password, user.rows[0].password);
if (!validPassword) {
return res.status(400).json({ message: "Ungültiges Passwort" });
}
// JWT Token erstellen
const token = jwt.sign(
{ id: user.rows[0].id, role: user.rows[0].role },
process.env.JWT_SECRET,
{
expiresIn: "1h",
}
);
res.status(200).json({ message: "Login erfolgreich", token });
} catch (err) {
res.status(500).json({ message: "Fehler beim Login", error: err.message });
}
};
module.exports = { registerUser, loginUser };

11
server/routes/auth.js Normal file
View File

@ -0,0 +1,11 @@
const express = require("express");
const { loginUser, registerUser } = require("../controllers/userController");
const router = express.Router();
// Registrierungsroute
router.post("/register", registerUser);
// Loginroute
router.post("/login", loginUser);
module.exports = router;