Not login support

This commit is contained in:
Akif9748 2022-04-03 21:01:55 +03:00
parent fc8d4c1c68
commit 4209822c74
13 changed files with 123 additions and 112 deletions

View file

@ -18,12 +18,10 @@ And, you can learn informations about API in `APIDOCS.md`.
* [Camroku](https://github.com/Camroku) - Made stylesheets
## To Do (Backend, bug fixes)
- Middleware. For logged in etc.
- Better error codes, example 400 for bad request
- Database change. (To MongoDB)
- Better DB writing. Example, not `message.author.id`, `messsage.authorID`
- `/errors/error` will change, better error page.
- Support for not logins...
## Roadmap
- [x] User

View file

@ -38,7 +38,7 @@ module.exports = class User {
this.time = time;
this.admin = admin;
this.deleted = deleted;
return this ;
return this;
}
takeId() {

View file

@ -1,10 +1,3 @@
const { Router } = require("express");
const app = Router();
app.get("/", (req, res, next) => {
module.exports = (req, res) => {
if (!req.session.loggedin) return res.redirect('/login');
next();
});
module.exports = app;
}

View file

@ -1,2 +0,0 @@
const admin = () => document.getElementById("admin").innerHTML =
'<div class="admin"><a class="admin" href="/admin"><p>You are admin, and you can go your page!</p></a></div><hr>';

View file

@ -6,17 +6,12 @@ const app = Router();
app.get("/", (req, res) => {
if (!req.session.loggedin) return res.redirect('/login');
const mem = process.memoryUsage().heapUsed / Math.pow(2, 20);
const users = get("users").length;
const threads = get("threads").length;
const messages = get("messages").length;
const user = new User().getId(req.session.userid)
const
mem = process.memoryUsage().heapUsed / Math.pow(2, 20),
users = get("users").length,
threads = get("threads").length,
messages = get("messages").length,
user = new User().getId(req.session.userid);
res.render("index", { mem, user, users, threads, messages })

View file

@ -5,18 +5,17 @@ const error = require("../errors/error")
const app = Router();
app.use(require("../middlewares/login"));
app.get("/", (req, res) => {
if (!req.session.loggedin) return res.redirect('/login');
const user = new User().getId(req.session.userid)
if (!user.admin) return error(res, 403, "You have not got permissions for view to this page.");
res.render("admin", { user, user2: false })
}
);
});
app.post("/", (req, res) => {
if (!req.session.loggedin) return res.redirect('/login');
const user = new User().getId(req.session.userid)

View file

@ -6,7 +6,6 @@ const { Router } = require("express");
const app = Router();
app.get("/:id", (req, res) => {
if (!req.session.loggedin) return res.redirect('/login');
const message = new Message().getId(req.params.id);
if (!message || message.deleted) return error(res, 404, "We have not got any message declared as this id.");
@ -15,10 +14,9 @@ app.get("/:id", (req, res) => {
});
app.use(require("../middlewares/login"));
app.post("/", (req, res) => {
if (!req.session.loggedin) return res.redirect('/login');
const thread = new Thread().getId(req.body.threadID);
if (thread) {
@ -35,8 +33,6 @@ app.post("/", (req, res) => {
app.post("/:id/delete", (req, res) => {
if (!req.session.loggedin) return res.redirect('/login');
const message = new Message().getId(req.params.id)
if (!message || message.deleted) return error(res, 404, "We have not got any message declared as this id.");
const user = new User().getId(req.session.userid);
@ -50,8 +46,6 @@ app.post("/:id/delete", (req, res) => {
})
app.post("/:id/react", (req, res) => {
if (!req.session.loggedin) return res.redirect('/login');
const { id = null } = req.params;
const info = req.body;
const message = new Message().getId(id);

View file

@ -6,16 +6,8 @@ const { Router } = require("express");
const app = Router();
app.get("/open*", (req, res) => {
if (!req.session.loggedin) return res.redirect('/login');
const user = new User().getId(req.session.userid)
res.render("openThread", { user })
});
app.get("/", (req, res) => {
if (!req.session.loggedin) return res.redirect('/login');
const user = new User().getId(req.session.userid);
@ -28,7 +20,6 @@ app.get("/", (req, res) => {
app.get("/:id", (req, res) => {
if (!req.session.loggedin) return res.redirect('/login');
const { id } = req.params;
@ -42,8 +33,19 @@ app.get("/:id", (req, res) => {
error(res, 404, "We have not got this thread.");
});
app.use(require("../middlewares/login"));
app.get("/open*", (req, res) => {
const user = new User().getId(req.session.userid)
res.render("openThread", { user })
});
app.post("/", (req, res) => {
if (!req.session.loggedin) return res.redirect('/login');
const user = new User().getId(req.session.userid);

View file

@ -7,26 +7,21 @@ const error = require("../errors/error")
const app = Router();
app.get("/", (req, res) => {
if (!req.session.loggedin) return res.redirect('/login');
const user = new User().getId(req.session.userid)
const users = db.get("users").slice(0);
const links = users.filter(user => !user.deleted).map(user => "/users/" + user.id);
return res.render("users", { users, links, user })
});
app.get("/:id", (req, res) => {
if (!req.session.loggedin) return res.redirect('/login');
const user = new User().getId(req.session.userid)
const { id = null } = req.params;
const member = new User().getId(req.params.id);
if (member && (user.admin || !member.deleted)) {
if (member && (user?.admin || !member.deleted)) {
const message = db.get("messages").filter(message => message.author.id === Number(id)).length
const thread = db.get("threads").filter(thread => thread.author.id === Number(id)).length
@ -36,10 +31,13 @@ app.get("/:id", (req, res) => {
else error(res, 404, "We have not got this user.");
});
app.use(require("../middlewares/login"));
app.post("/:id/delete/", (req, res) => {
if (!req.session.loggedin) return res.redirect('/login');
const user = new User().getId(req.session.userid);
if (!user.admin)
if (!user?.admin)
return error(res, 403, "You have not got permission for this.");
const id = req.url.slice(9 + 3)

View file

@ -1,10 +1,14 @@
<!-- Navbar: -->
<script src="/js/scripts.js"></script>
<div id="admin"></div>
<script> if (<%= user.admin %>) admin(); </script>
<% if (user?.admin){ %>
<div class="admin">
<a class="admin" href="/admin"><p>You are admin, and you can go your page!</p></a>
</div>
<hr>
<% } %>
<a href="/"><img class="logo" src="/images/logo.jpg" alt="AKF-FORUM"></a>
<div class="navbar" id="navbar">
@ -14,6 +18,7 @@
<a href="/search">SEARCH</a>
<a href="/threads/open/">OPEN THREAD</a>
<% if (user){ %>
<div style="float: right;" class="user" id="user">
<a href=<%=user.getLink() %>>
<h1>
@ -22,6 +27,16 @@
</a>
</div>
<% } else { %>
<a style="float: right; background-color: #5F875F;" href="/register">REGISTER</a>
<a style="float: right; background-color:#5F87AF; " href="/login">LOGIN</a>
<% } %>
</div>

View file

@ -4,9 +4,9 @@
<%- include("extra/header", {title: "Main page!" }) %>
<body>
<%- include("extra/navbar", {user}) %>
<%- include("extra/navbar") %>
<% if (user) { %>
<h1>Welcome, <a href=<%=user.getLink() %>> <%= user.name %></a>
<img class="yuvarlak" src=<%=user.avatar %> alt=<%= user.name %>>
<br>
@ -16,6 +16,11 @@
</h1>
<br>
<% } else { %>
<h1>Welcome, Guest!<br>You can press for register here:
<button class="buyuk" onclick= "window.location.href = '/register'">REGISTER</button>
</h1>
<% } %>
<h1>Statistics:</h1>
<ul>
<li>

View file

@ -1,9 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<%- include("extra/header", {title: "Main page!" }) %>
<%- include("extra/header", {title: "Main page!" }) %>
<body>
<body>
<%- include("extra/navbar", {user}) %>
@ -12,26 +12,35 @@
<%= thread.title %>
</h1>
<br>
<h2>By <a style="color: #bcbcbc;" href=<%="/users/" + thread.author.id %>> <%= thread.author.name %></a>
<img class="yuvarlak" src=<%= thread.author.avatar %> alt=<%= thread.author.name %>>
</h2>
<hr>
<% messages.forEach(message=>{ %>
<div id=<%="message-" + message.id %> style="border: 2px solid #444444; padding: 5px;">
<div id="message-<%= message.id %>" style="border: 2px solid #444444; padding: 10px;">
<h3 style="float:right;">
<%=new Date(message.time).toLocaleString() %>
</h3>
<h2>
<img class="yuvarlak" src=<%=message.author.avatar %> alt=<%= message.author.name %>>
<a style=" color: #bcbcbc; " href=<%="/users/" + message.author.id %>> <%= message.author.name %></a>:
<a style="color: #bcbcbc;" href=<%="/users/" + message.author.id %>> <%= message.author.name %></a>:
</h2>
<h2>
<%= message.content %>
</h2>
<form style="text-align:right;display:inline;" action="/message/<%= message.id %>/delete/" method="post">
<button style="display:inline;" class="button" type="submit">DELETE</button>
</form>
<form style="text-align:right;" action="/message/<%= message.id %>/react/" method="POST">
<br>
<% if (user){ %>
<form style="display:inline;" action="/message/<%= message.id %>/delete/" method="post">
</a><button type="submit">DELETE</button>
</form>
<form style="float: right;" action="/message/<%= message.id %>/react/" method="POST">
<h3 style="display:inline;">
<%= Object.values(message.react).filter(Boolean).length - Object.values(message.react).filter(x=>
@ -39,12 +48,13 @@
</h3>
<button style="display:inline;" class="button" name="like" type="submit">+🔼</button>
<button style="display:inline;" class="button" name="dislike" type="submit">-🔽</button>
<h3 style="display:inline;">
<%=new Date(message.time).toLocaleString() %>
</h3>
<button style="display:inline;" name="like" type="submit">+🔼</button>
<button style="display:inline;" name="dislike" type="submit">-🔽</button>
</form>
<% } %>
</div>
<br>
@ -58,12 +68,16 @@
<input name="threadID" type="hidden" value="<%= thread.id %>"></input>
<br>
<% if (user){ %>
<button type="submit">Send!</button>
<%} else {%>
<button disabled>Login for send</button>
<% }%>
<button class="button" type="submit">Send!</button>
</form>
<%- include("extra/footer") %>
</body>
</body>
</html>

View file

@ -40,7 +40,7 @@
</li>
</ul>
<% if (user.admin) {%>
<% if (user?.admin) {%>
<form action="/admin/" method="POST">
<input name="userid" type="hidden" value="<%= member.id %>"></input>
<button class="buyuk" type="submit">Make admin!</button>