refactor: two spaces

This commit is contained in:
Martin Eyben 2024-11-08 12:04:58 +01:00
parent 832112fb41
commit 035eff019b
10 changed files with 167 additions and 153 deletions

View File

@ -12,7 +12,7 @@ const app = express();
const port = process.env.PORT ?? 3000;
const log_format = (process.env.NODE_ENV === "dev") ? "dev": "combined";
const log_format = (process.env.NODE_ENV === "dev") ? "dev" : "combined";
console.log("=== LOG CONFIG ===");
console.log(`log format: ${log_format}`);
console.log("=== END LOG CONFIG ===");
@ -30,5 +30,5 @@ app.use(morgan(log_format));
app.use(routes);
app.listen(port, () => {
return console.log(`Briques is listening at http://localhost:${port}`);
return console.log(`Briques is listening at http://localhost:${port}`);
});

View File

@ -7,65 +7,65 @@ import memberService from '../services/member.service';
import { Either, eitherLeft, eitherRight } from '../utils/utils';
const register = (req: Request, res: Response) => {
if (!req || !req.body || !req.body.name || !req.body.password) {
res.status(400).send();
return;
if (!req || !req.body || !req.body.name || !req.body.password) {
res.status(400).send();
return;
}
const name: string = req.body.name;
bcrypt.genSalt(10, (err, salt) => {
if (err) {
res.status(500).send();
return;
}
const name: string = req.body.name;
bcrypt.genSalt(10, (err, salt) => {
if (err) {
res.status(500).send();
return;
}
bcrypt.hash(req.body.password, salt, async (err, hash) => {
if (err) {
res.status(500).send();
return;
}
const member: Either<Member, string> = await memberService.createMember(name, hash);
if (member.hasRight) {
res.status(401).send(member.right);
return;
}
const token = await new SignJWT({ name })
.setProtectedHeader({ alg: 'HS256' })
.setAudience(JWT_AUDIENCE)
.setIssuer(JWT_ISSUER)
.setExpirationTime(JWT_EXPIRATION)
.sign(JWT_SECRET_KEY);
res.status(200).send({ member: memberService.userAdapter(member.left), token: token});
});
bcrypt.hash(req.body.password, salt, async (err, hash) => {
if (err) {
res.status(500).send();
return;
}
const member: Either<Member, string> = await memberService.createMember(name, hash);
if (member.hasRight) {
res.status(401).send(member.right);
return;
}
const token = await new SignJWT({ name })
.setProtectedHeader({ alg: 'HS256' })
.setAudience(JWT_AUDIENCE)
.setIssuer(JWT_ISSUER)
.setExpirationTime(JWT_EXPIRATION)
.sign(JWT_SECRET_KEY);
res.status(200).send({ member: memberService.userAdapter(member.left), token: token });
});
});
};
const login = async (req: Request, res: Response) => {
if (!req || !req.body || !req.body.name || !req.body.password) {
res.status(400).send();
return;
if (!req || !req.body || !req.body.name || !req.body.password) {
res.status(400).send();
return;
}
const name: string = req.body.name;
const member: Either<Member, string> = await memberService.getMember(name);
if (member.hasRight) {
res.send(member.right).send();
return;
}
bcrypt.compare(req.body.password, member.left.password, async (err, r) => {
if (err) {
res.status(500).send();
return;
}
const name: string = req.body.name;
const member: Either<Member, string> = await memberService.getMember(name);
if (member.hasRight) {
res.send(member.right).send();
return;
if (!r) {
res.status(401).send();
return;
}
bcrypt.compare(req.body.password, member.left.password, async (err, r) => {
if (err) {
res.status(500).send();
return;
}
if (!r) {
res.status(401).send();
return;
}
const token = await new SignJWT({ name })
.setProtectedHeader({ alg: 'HS256' })
.setAudience(JWT_AUDIENCE)
.setIssuer(JWT_ISSUER)
.setExpirationTime(JWT_EXPIRATION)
.sign(JWT_SECRET_KEY);
res.status(200).send({ member: memberService.userAdapter(member.left), token: token});
});
const token = await new SignJWT({ name })
.setProtectedHeader({ alg: 'HS256' })
.setAudience(JWT_AUDIENCE)
.setIssuer(JWT_ISSUER)
.setExpirationTime(JWT_EXPIRATION)
.sign(JWT_SECRET_KEY);
res.status(200).send({ member: memberService.userAdapter(member.left), token: token });
});
};
export { register, login };

View File

@ -4,54 +4,54 @@ import { Marque } from '../types/marque';
import { Either, eitherLeft, eitherRight } from '../utils/utils';
const getMarque = async (idOrName: number | string): Promise<Either<Marque, string>> => {
const client = new_client();
await client.connect();
let res;
if (typeof idOrName !== 'number') {
res = await client.query("SELECT * FROM marques WHERE nom_marque=$1;", [`${idOrName}`]);
} else {
res = await client.query("SELECT * FROM marques WHERE id_marque=$1;", [idOrName]);
}
if (res.rows.length === 0) {
await client.end();
return eitherRight<Marque, string>("Does not exist.");
}
const marque: Marque = { id_marque: res.rows[0].id_marque, name: res.rows[0].nom_marque };
const client = new_client();
await client.connect();
let res;
if (typeof idOrName !== 'number') {
res = await client.query("SELECT * FROM marques WHERE nom_marque=$1;", [`${idOrName}`]);
} else {
res = await client.query("SELECT * FROM marques WHERE id_marque=$1;", [idOrName]);
}
if (res.rows.length === 0) {
await client.end();
return eitherRight<Marque, string>("Does not exist.");
}
const marque: Marque = { id_marque: res.rows[0].id_marque, name: res.rows[0].nom_marque };
await client.end();
return eitherLeft<Marque, string>(marque);
return eitherLeft<Marque, string>(marque);
}
const createMarque = async (name: string): Promise<Either<Marque, string>> => {
const gettingMarque = await getMarque(name);
if (!gettingMarque.hasRight) {
return eitherRight<Marque, string>("Already in database.");
}
const client = new_client();
await client.connect();
const res = await client.query("INSERT INTO marques (nom_membre) VALUES ($1) RETURNING *;", [`${name}`]);
if (res.rows.length === 0) {
await client.end();
return eitherRight<Marque, string>("Something went wrong");
}
const marque: Marque = { id_marque: res.rows[0].id_marque, name: res.rows[0].nom_marque };
const gettingMarque = await getMarque(name);
if (!gettingMarque.hasRight) {
return eitherRight<Marque, string>("Already in database.");
}
const client = new_client();
await client.connect();
const res = await client.query("INSERT INTO marques (nom_membre) VALUES ($1) RETURNING *;", [`${name}`]);
if (res.rows.length === 0) {
await client.end();
return eitherLeft<Marque, string>(marque);
return eitherRight<Marque, string>("Something went wrong");
}
const marque: Marque = { id_marque: res.rows[0].id_marque, name: res.rows[0].nom_marque };
await client.end();
return eitherLeft<Marque, string>(marque);
};
const register = async (req: Request, res: Response) => {
if (!req || !req.body || !req.body.name) {
res.status(400).send();
return;
}
const name: string = req.body.name;
const marque: Either<Marque, string> = await createMarque(name);
if (marque.hasRight) {
res.status(403).send(marque.right);
return;
}
if (!req || !req.body || !req.body.name) {
res.status(400).send();
return;
}
const name: string = req.body.name;
const marque: Either<Marque, string> = await createMarque(name);
if (marque.hasRight) {
res.status(403).send(marque.right);
return;
}
res.status(200).send(marque);
res.status(200).send(marque);
}
export { register };

View File

@ -1,36 +1,36 @@
import { RequestHandler } from "express";
import memberService from "../services/member.service";
const memberByName: RequestHandler<{name: string;}> = async (req, res) => {
const memberEither = await memberService.getMember(req.params.name);
const memberByName: RequestHandler<{ name: string; }> = async (req, res) => {
const memberEither = await memberService.getMember(req.params.name);
if (memberEither.hasRight) {
res.sendStatus(404);
return;
}
if (memberEither.hasRight) {
res.sendStatus(404);
return;
}
const member = memberEither.left;
const member = memberEither.left;
res.render('member.ejs', { member });
};
const memberById: RequestHandler<{id: string;}> = async (req, res) => {
const id = parseInt(req.params.id);
const memberEither = await memberService.getMember(id);
const memberById: RequestHandler<{ id: string; }> = async (req, res) => {
const id = parseInt(req.params.id);
const memberEither = await memberService.getMember(id);
if (memberEither.hasRight) {
res.sendStatus(404);
return;
}
if (memberEither.hasRight) {
res.sendStatus(404);
return;
}
const member = memberEither.left;
const member = memberEither.left;
res.render('member.ejs', { member });
res.render('member.ejs', { member });
};
const memberController = {
memberByName,
memberById,
memberByName,
memberById,
};
export default memberController;

View File

@ -6,43 +6,43 @@ import { Member } from '../types/member';
import { Either } from '../utils/utils';
const extractBearerToken = (headerValue: string) => {
const matches = headerValue.match(/(bearer)\s+(\S+)/i);
return matches && matches[2];
const matches = headerValue.match(/(bearer)\s+(\S+)/i);
return matches && matches[2];
}
const getUsername = async (req: Request, res: Response, next: () => void) => {
const token = req.headers.authorization && extractBearerToken(req.headers.authorization);
const token = req.headers.authorization && extractBearerToken(req.headers.authorization);
if (!token) {
next();
return;
}
try {
const { payload } = await jwtVerify(token, JWT_SECRET_KEY);
const name: string = payload.name as string;
const member: Either<Member, string> = await memberService.getMember(name);
if (!member.hasRight) {
res.locals.user = {
id_member: member.left.id_member,
name: member.left.name
}
next()
} else {
res.status(401).send(member.right)
}
} catch (e) {
res.status(401).send();
if (!token) {
next();
return;
}
try {
const { payload } = await jwtVerify(token, JWT_SECRET_KEY);
const name: string = payload.name as string;
const member: Either<Member, string> = await memberService.getMember(name);
if (!member.hasRight) {
res.locals.user = {
id_member: member.left.id_member,
name: member.left.name
}
next()
} else {
res.status(401).send(member.right)
}
} catch (e) {
res.status(401).send();
}
}
function verifyAuthentication(req: Request, res: Response, next: () => void) {
if (!res.locals.user) {
res.status(401).send();
return;
}
next();
if (!res.locals.user) {
res.status(401).send();
return;
}
next();
}
export { getUsername, verifyAuthentication };

View File

@ -1,13 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Leohl !dworl</title>
</head>
<body>
<h1>Naisu</h1>
<h2><%= message %></h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Sapiente quod dicta molestiae harum veniam iste, nostrum nemo earum commodi fugit modi, ratione obcaecati, beatae ullam. Debitis ducimus dignissimos rem at.</p>
<h2>
<%= message %>
</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Sapiente quod dicta molestiae harum veniam iste, nostrum
nemo earum commodi fugit modi, ratione obcaecati, beatae ullam. Debitis ducimus dignissimos rem at.</p>
</body>
</html>

View File

@ -1,12 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= member.name %></title>
<title>
<%= member.name %>
</title>
</head>
<body>
<h1><%= member.name %></h1>
<h2>id: <%= member.id_member%></h2>
<h1>
<%= member.name %>
</h1>
<h2>id: <%= member.id_member%>
</h2>
</body>
</html>

View File

@ -1,6 +1,6 @@
type Marque = {
id_marque: number,
name: string
id_marque: number,
name: string
};
export { Marque };

View File

@ -1,6 +1,6 @@
type User = {
id_member: number,
name: string
id_member: number,
name: string
}
type Member = User & { password: string };

View File

@ -1,15 +1,15 @@
type Either<T, U> = {
hasRight: boolean,
left: T,
right: U
hasRight: boolean,
left: T,
right: U
};
function eitherLeft<T, U>(left: T): Either<T, U> {
return { hasRight: false, left: left, right: undefined };
return { hasRight: false, left: left, right: undefined };
}
function eitherRight<T, U>(right: U): Either<T, U> {
return { hasRight: true, left: undefined, right: right };
return { hasRight: true, left: undefined, right: right };
}
export { Either, eitherLeft, eitherRight };