Initial commit
This commit is contained in:
68
backend/src/Express/ExpressManager.js
Normal file
68
backend/src/Express/ExpressManager.js
Normal file
@@ -0,0 +1,68 @@
|
||||
const express = require("express");
|
||||
const sessions = require("express-session");
|
||||
const bp = require("body-parser");
|
||||
const path = require("path");
|
||||
const DataBaseManager = require("../Database/DataBaseManager");
|
||||
const AccountRoute = require("./Routes/AccountRoute");
|
||||
const DashboardRoute = require("./Routes/DashboardRoute");
|
||||
|
||||
class ExpressManager{
|
||||
/**@param {DataBaseManager} dbManager*/
|
||||
constructor(dbManager){
|
||||
this.db = dbManager;
|
||||
|
||||
this.app = express();
|
||||
|
||||
this.sessionMiddleware = sessions({
|
||||
secret: process.env.SESSION_KEY,
|
||||
saveUninitialized: false,
|
||||
resave: false,
|
||||
cookie: {maxAge: 24 * 60 * 60 * 1000}
|
||||
});
|
||||
|
||||
this.app.use((req, res, next) => { this.logger(req, res, next) });
|
||||
this.app.use(this.sessionMiddleware);
|
||||
this.app.use((req, res, next) => { this.needAuth(req, res, next) });
|
||||
this.app.use(bp.urlencoded({extended: false}));
|
||||
this.app.use(express.json());
|
||||
this.app.use(bp.json());
|
||||
this.app.use(express.static(path.join(__dirname, "./../../../frontend")));
|
||||
|
||||
// Routen wo man für Angemeldet sein muss
|
||||
this.authRoutes = [
|
||||
"/api/dashboard",
|
||||
"/dashboard"
|
||||
];
|
||||
|
||||
// Routen Einbinden
|
||||
this.app.use("/api/account/", new AccountRoute(this.db).router);
|
||||
this.app.use("/api/dashboard/", new DashboardRoute(this.db).router);
|
||||
}
|
||||
|
||||
/**@param {express.Request} req @param {express.Response} res @param {express.NextFunction} next*/
|
||||
logger(req, res, next){
|
||||
const date = new Date();
|
||||
console.log(`${date.toTimeString().slice(0, 8)} | ${req.method} | ${req.url}`);
|
||||
next();
|
||||
}
|
||||
|
||||
/**@param {express.Request} req @param {express.Response} res @param {express.NextFunction} next*/
|
||||
needAuth(req, res, next){
|
||||
let isProtectedRoute = false;
|
||||
this.authRoutes.forEach(route => {
|
||||
if(req.url.startsWith(route)) isProtectedRoute = true;
|
||||
});
|
||||
|
||||
// Geht zum Login wenn User versucht Routen aufzurufen wofür man angemeldet sein muss
|
||||
if(isProtectedRoute && !req.session.user?.isSet) return res.redirect("/login");
|
||||
|
||||
// Geht zum Dashboard wenn der Nutzer versucht sich zu registrieren oder einzuloggen wenn er angemeldet ist
|
||||
if(req.session.user?.isSet && (req.url.startsWith("/login") || req.url.startsWith("/register"))){
|
||||
return res.redirect("/dashboard");
|
||||
}
|
||||
|
||||
next();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ExpressManager;
|
||||
84
backend/src/Express/Routes/AccountRoute.js
Normal file
84
backend/src/Express/Routes/AccountRoute.js
Normal file
@@ -0,0 +1,84 @@
|
||||
const express = require("express");
|
||||
const User = require("../../Database/UserManager/User");
|
||||
const DataBaseManager = require("../../Database/DataBaseManager");
|
||||
const bcrypt = require("bcrypt");
|
||||
|
||||
class AccountRoute {
|
||||
/**@param {DataBaseManager} dbManager*/
|
||||
constructor(dbManager) {
|
||||
this.router = express.Router();
|
||||
|
||||
this.db = dbManager;
|
||||
|
||||
this.router.post("/register", async (req, res) => await this.register(req, res));
|
||||
this.router.post("/login", async (req, res) => await this.login(req, res));
|
||||
this.router.post("/update", async (req, res) => await this.update(req, res));
|
||||
this.router.get("/logout", async (req, res) => await this.logout(req, res));
|
||||
}
|
||||
|
||||
/**@param {express.Request} req @param {express.Response}*/
|
||||
async register(req, res){
|
||||
const body = req.body;
|
||||
if(!body.username || !body.password) return res.redirect("/register?error=1");
|
||||
|
||||
const user = new User(body);
|
||||
|
||||
const result = await this.db.usermanager.createUser(user);
|
||||
if(result !== 1) return res.redirect("/register?error=2");
|
||||
|
||||
res.redirect("/login");
|
||||
}
|
||||
|
||||
/**@param {express.Request} req @param {express.Response}*/
|
||||
async login(req, res){
|
||||
const body = req.body;
|
||||
if(!body.username || !body.password) return res.redirect("/login?error=1")
|
||||
|
||||
const user = await this.db.usermanager.getUser({username: body.username});
|
||||
if(!user) return res.redirect("/login?error=2")
|
||||
|
||||
const passwordMatch = await user.doesPassMatch(body.password);
|
||||
if(!passwordMatch) return res.redirect("/login?error=2")
|
||||
|
||||
req.session.user = {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
isSet: true
|
||||
}
|
||||
|
||||
res.redirect("/dashboard")
|
||||
}
|
||||
|
||||
/**@param {express.Request} req @param {express.Response}*/
|
||||
async update(req, res){
|
||||
const user = await this.db.usermanager.getUser({id: req.session.user.id});
|
||||
const body = req.body;
|
||||
|
||||
if(user.username !== body.username){
|
||||
const checkUser = await this.db.usermanager.getUser({username: body.username});
|
||||
if(checkUser) return res.redirect("/dashboard/account/?error=1");
|
||||
|
||||
user.username = body.username;
|
||||
}
|
||||
|
||||
user.email = body.email;
|
||||
user.fullName = body.fullName;
|
||||
|
||||
if(body.password !== ""){
|
||||
const passHash = await bcrypt.hash(body.password, 10);
|
||||
user.password = passHash;
|
||||
}
|
||||
|
||||
await this.db.usermanager.updateUser(user.id, user);
|
||||
|
||||
res.redirect("/dashboard/account");
|
||||
}
|
||||
|
||||
/**@param {express.Request} req @param {express.Response}*/
|
||||
async logout(req, res){
|
||||
req.session.destroy();
|
||||
res.redirect("/");
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = AccountRoute;
|
||||
23
backend/src/Express/Routes/DashboardRoute.js
Normal file
23
backend/src/Express/Routes/DashboardRoute.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const express = require("express");
|
||||
const User = require("../../Database/UserManager/User");
|
||||
const DataBaseManager = require("../../Database/DataBaseManager");
|
||||
|
||||
class DashboardRoute{
|
||||
/**@param {DataBaseManager} dbManager*/
|
||||
constructor(dbManager){
|
||||
this.router = express.Router();
|
||||
|
||||
this.db = dbManager;
|
||||
|
||||
this.router.get("/", async (req, res) => await this.userInfo(req, res))
|
||||
}
|
||||
|
||||
/**@param {express.Request} req @param {express.Response}*/
|
||||
async userInfo(req, res){
|
||||
const user = await this.db.usermanager.getUser({id: req.session.user.id});
|
||||
|
||||
res.json(user.toUserJSON());
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = DashboardRoute;
|
||||
Reference in New Issue
Block a user