Skip to content

Commit

Permalink
working register & login
Browse files Browse the repository at this point in the history
  • Loading branch information
daneedev committed Apr 22, 2024
1 parent eb65421 commit 73514b1
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
39 changes: 37 additions & 2 deletions routes/auth.js
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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")))
Expand Down
12 changes: 12 additions & 0 deletions views/auth/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,17 @@
{% endblock %}

{% block content %}
{% if loginSuccess.length > 0 %}
<div class="alert alert-success" role="alert">
{{ loginSuccess }}
</div>
{% endif %}
<p>Login</p>
<form action="/auth/login" method="post">
<label for="username">Username</label>
<input type="text" id="username" name="username" required>
<label for="password">Password</label>
<input type="password" id="password" name="password" required>
<button type="submit">Login</button>
</form>
{% endblock %}
18 changes: 18 additions & 0 deletions views/dash.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends "template.html" %}

{% block head %}
<title>PanelMgr | Dashboard</title>
{% endblock %}

{% block content %}
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="#">PanelMgr</a>
</div>
</nav>
<div class="container">
<h1>Dashboard</h1>
<p>Welcome to the dashboard</p>
</div>
<form>
{% endblock %}

0 comments on commit 73514b1

Please sign in to comment.