Skip to content

Commit

Permalink
[Temp, Task] #43 basic login page, not yet used as middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Type-Style committed Feb 16, 2024
1 parent d077414 commit 6ebece8
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 36 deletions.
20 changes: 20 additions & 0 deletions httpdocs/css/login.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
form {
margin-inline: auto;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
max-width: 500px;
gap: 10px;
}
input, button {
flex-grow: 1;
}
textarea, h1 {
flex-basis: 100%;
}
textarea {
height: 50vh;
}
h1 {
text-align: center;
}
19 changes: 0 additions & 19 deletions httpdocs/login.html

This file was deleted.

52 changes: 35 additions & 17 deletions src/controller/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,51 @@ import express, { Request, Response, NextFunction } from 'express';
import * as file from '@src/scripts/file';
import { create as createError } from '@src/middleware/error';
import { validationResult, query } from 'express-validator';
import logger from '@src/scripts/logger';

const router = express.Router();

router.get('/',
[query('index').isInt().withMessage("not an integer")
router.get('/',
[query('index').isInt().withMessage("not an integer")
.isLength({ max: 3 }).withMessage("not in range")
.toInt()],
async function getRead(req:Request, res:Response, next:NextFunction) {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return createError(res, 400, JSON.stringify({errors: errors.array()}), next)
}
async function getRead(req: Request, res: Response, next: NextFunction) {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return createError(res, 400, JSON.stringify({ errors: errors.array() }), next)
}

const fileObj: File.Obj = file.getFile(res, next);
fileObj.content = await file.readAsJson(res, fileObj.path, next)
if (!fileObj.content || !Array.isArray(fileObj.content.entries)) {
return createError(res, undefined, `File corrupt: ${fileObj.path}`, next);
}
const fileObj: File.Obj = file.getFile(res, next);
fileObj.content = await file.readAsJson(res, fileObj.path, next)
if (!fileObj.content || !Array.isArray(fileObj.content.entries)) {
return createError(res, undefined, `File corrupt: ${fileObj.path}`, next);
}

let entries = fileObj.content.entries;
let entries = fileObj.content.entries;

if (req.query.index) {
entries = entries.slice(Number(req.query.index));
}
if (req.query.index) {
entries = entries.slice(Number(req.query.index));
}

res.json({entries});
res.json({ entries });
});



// TODO will be converted to middleware
// TODO write test for checking the limit on request body
router.get("/login/", async function login(req: Request, res: Response) {
logger.log("login was called");
res.locals.text = "start";

res.render("login-form");
});

router.post("/login/", async function postLogin(req: Request, res: Response) {
logger.log("post login was called");
logger.log(req.body);
res.locals.text = "post recieved";
res.render("login-form");
});

export default router;
21 changes: 21 additions & 0 deletions views/login-form.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html>

<head>
<meta charset='utf-8'>
<title>Login Form - Lorex</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' href='/css/login.css'>
</head>

<body>
<form class="login" action="/read/login/" method="post">
<h1>Text: <%= locals.text %></h1>
<input name="user" class="login__input" placeholder="User name / Email">
<input name="password" type="password" class="login__input" placeholder="Password">
<button type="submit">Submit</button>
<textarea name="text"></textarea>
</form>
</body>

</html>

0 comments on commit 6ebece8

Please sign in to comment.