From 73514b13a113f0e9d63449903289032c96881dbf Mon Sep 17 00:00:00 2001 From: daneeskripter Date: Mon, 22 Apr 2024 16:44:19 +0200 Subject: [PATCH] working register & login --- routes/auth.js | 39 +++++++++++++++++++++++++++++++++++++-- server.js | 8 ++++++++ views/auth/login.html | 12 ++++++++++++ views/dash.html | 18 ++++++++++++++++++ 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 views/dash.html diff --git a/routes/auth.js b/routes/auth.js index ed134de..e304736 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -1,14 +1,49 @@ const express = require('express'); const { checkNotAuth } = require('../handlers/checkAuth'); +const User = require('../models/UserModel'); const router = express.Router() +const bcrypt = require('bcrypt') +const passport = require('passport') router.get("/login", checkNotAuth, function (req, res) { - res.render("auth/login.html") + res.render("auth/login.html", { loginSuccess: req.flash("loginSuccess")}) }) +router.post("/login", checkNotAuth, passport.authenticate("local", { + successRedirect: "/dash", + failureRedirect: "/auth/login", + failureFlash: true +})) + router.get("/register", checkNotAuth, function (req, res) { - res.render("auth/register.html") + res.render("auth/register.html", { registerError: req.flash("registerError")}) +}) + +router.post("/register", checkNotAuth, async function (req, res) { + const userUsername = await User.findOne({ where: { username: req.body.username}}) + const userEmail = await User.findOne({ where: { email: req.body.email}}) + if (userUsername) { + req.flash("registerError", "User already registered with that username") + res.redirect("/auth/register") + } else if (userEmail) { + req.flash("registerError", "User already registered with that email") + res.redirect("/auth/register") + } else { + const hashedPassword = await bcrypt.hash(req.body.password, 10) + User.create({ + username: req.body.username, + password: hashedPassword, + email: req.body.email, + pteroId: 0, // TODO: Implement Ptero API + ipAddress: "", // TODO: Register IP address + credits: 0, + verifiedEmail: false + }) + req.flash("loginSuccess", "User registered successfully") + res.redirect("/auth/login") + } }) + module.exports = router \ No newline at end of file diff --git a/server.js b/server.js index b0ba371..d003594 100644 --- a/server.js +++ b/server.js @@ -9,7 +9,10 @@ const passport = require('passport'); const loadPassport = require("./handlers/passport"); const session = require("express-session") const SequelizeStore = require("connect-session-sequelize")(session.Store); +const bodyParser = require('body-parser'); +const flash = require('connect-flash'); +app.use(bodyParser.urlencoded({ extended: false })); loadPassport(passport) app.use(session({ secret: process.env.SESSION_SECRET, @@ -22,6 +25,11 @@ app.use(session({ db: db }) })) +app.use(passport.initialize()); +app.use(passport.session()); + +// FLASH MESSAGES +app.use(flash()) app.use("/", express.static(path.join(__dirname, "public"))) diff --git a/views/auth/login.html b/views/auth/login.html index 991cd23..fc8f615 100644 --- a/views/auth/login.html +++ b/views/auth/login.html @@ -5,5 +5,17 @@ {% endblock %} {% block content %} +{% if loginSuccess.length > 0 %} + +{% endif %}

Login

+
+ + + + + +
{% endblock %} \ No newline at end of file diff --git a/views/dash.html b/views/dash.html new file mode 100644 index 0000000..b413968 --- /dev/null +++ b/views/dash.html @@ -0,0 +1,18 @@ +{% extends "template.html" %} + +{% block head %} +PanelMgr | Dashboard +{% endblock %} + +{% block content %} + +
+

Dashboard

+

Welcome to the dashboard

+
+
+{% endblock %} \ No newline at end of file