refactor: two spaces
This commit is contained in:
parent
832112fb41
commit
035eff019b
@ -12,7 +12,7 @@ const app = express();
|
|||||||
|
|
||||||
const port = process.env.PORT ?? 3000;
|
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 CONFIG ===");
|
||||||
console.log(`log format: ${log_format}`);
|
console.log(`log format: ${log_format}`);
|
||||||
console.log("=== END LOG CONFIG ===");
|
console.log("=== END LOG CONFIG ===");
|
||||||
@ -30,5 +30,5 @@ app.use(morgan(log_format));
|
|||||||
app.use(routes);
|
app.use(routes);
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
return console.log(`Briques is listening at http://localhost:${port}`);
|
return console.log(`Briques is listening at http://localhost:${port}`);
|
||||||
});
|
});
|
||||||
|
@ -7,65 +7,65 @@ import memberService from '../services/member.service';
|
|||||||
import { Either, eitherLeft, eitherRight } from '../utils/utils';
|
import { Either, eitherLeft, eitherRight } from '../utils/utils';
|
||||||
|
|
||||||
const register = (req: Request, res: Response) => {
|
const register = (req: Request, res: Response) => {
|
||||||
if (!req || !req.body || !req.body.name || !req.body.password) {
|
if (!req || !req.body || !req.body.name || !req.body.password) {
|
||||||
res.status(400).send();
|
res.status(400).send();
|
||||||
return;
|
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.hash(req.body.password, salt, async (err, hash) => {
|
||||||
bcrypt.genSalt(10, (err, salt) => {
|
if (err) {
|
||||||
if (err) {
|
res.status(500).send();
|
||||||
res.status(500).send();
|
return;
|
||||||
return;
|
}
|
||||||
}
|
const member: Either<Member, string> = await memberService.createMember(name, hash);
|
||||||
bcrypt.hash(req.body.password, salt, async (err, hash) => {
|
if (member.hasRight) {
|
||||||
if (err) {
|
res.status(401).send(member.right);
|
||||||
res.status(500).send();
|
return;
|
||||||
return;
|
}
|
||||||
}
|
const token = await new SignJWT({ name })
|
||||||
const member: Either<Member, string> = await memberService.createMember(name, hash);
|
.setProtectedHeader({ alg: 'HS256' })
|
||||||
if (member.hasRight) {
|
.setAudience(JWT_AUDIENCE)
|
||||||
res.status(401).send(member.right);
|
.setIssuer(JWT_ISSUER)
|
||||||
return;
|
.setExpirationTime(JWT_EXPIRATION)
|
||||||
}
|
.sign(JWT_SECRET_KEY);
|
||||||
const token = await new SignJWT({ name })
|
res.status(200).send({ member: memberService.userAdapter(member.left), token: token });
|
||||||
.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) => {
|
const login = async (req: Request, res: Response) => {
|
||||||
if (!req || !req.body || !req.body.name || !req.body.password) {
|
if (!req || !req.body || !req.body.name || !req.body.password) {
|
||||||
res.status(400).send();
|
res.status(400).send();
|
||||||
return;
|
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;
|
if (!r) {
|
||||||
const member: Either<Member, string> = await memberService.getMember(name);
|
res.status(401).send();
|
||||||
if (member.hasRight) {
|
return;
|
||||||
res.send(member.right).send();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
bcrypt.compare(req.body.password, member.left.password, async (err, r) => {
|
const token = await new SignJWT({ name })
|
||||||
if (err) {
|
.setProtectedHeader({ alg: 'HS256' })
|
||||||
res.status(500).send();
|
.setAudience(JWT_AUDIENCE)
|
||||||
return;
|
.setIssuer(JWT_ISSUER)
|
||||||
}
|
.setExpirationTime(JWT_EXPIRATION)
|
||||||
if (!r) {
|
.sign(JWT_SECRET_KEY);
|
||||||
res.status(401).send();
|
res.status(200).send({ member: memberService.userAdapter(member.left), token: token });
|
||||||
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});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export { register, login };
|
export { register, login };
|
||||||
|
@ -4,54 +4,54 @@ import { Marque } from '../types/marque';
|
|||||||
import { Either, eitherLeft, eitherRight } from '../utils/utils';
|
import { Either, eitherLeft, eitherRight } from '../utils/utils';
|
||||||
|
|
||||||
const getMarque = async (idOrName: number | string): Promise<Either<Marque, string>> => {
|
const getMarque = async (idOrName: number | string): Promise<Either<Marque, string>> => {
|
||||||
const client = new_client();
|
const client = new_client();
|
||||||
await client.connect();
|
await client.connect();
|
||||||
let res;
|
let res;
|
||||||
if (typeof idOrName !== 'number') {
|
if (typeof idOrName !== 'number') {
|
||||||
res = await client.query("SELECT * FROM marques WHERE nom_marque=$1;", [`${idOrName}`]);
|
res = await client.query("SELECT * FROM marques WHERE nom_marque=$1;", [`${idOrName}`]);
|
||||||
} else {
|
} else {
|
||||||
res = await client.query("SELECT * FROM marques WHERE id_marque=$1;", [idOrName]);
|
res = await client.query("SELECT * FROM marques WHERE id_marque=$1;", [idOrName]);
|
||||||
}
|
}
|
||||||
if (res.rows.length === 0) {
|
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();
|
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 createMarque = async (name: string): Promise<Either<Marque, string>> => {
|
||||||
const gettingMarque = await getMarque(name);
|
const gettingMarque = await getMarque(name);
|
||||||
if (!gettingMarque.hasRight) {
|
if (!gettingMarque.hasRight) {
|
||||||
return eitherRight<Marque, string>("Already in database.");
|
return eitherRight<Marque, string>("Already in database.");
|
||||||
}
|
}
|
||||||
const client = new_client();
|
const client = new_client();
|
||||||
await client.connect();
|
await client.connect();
|
||||||
const res = await client.query("INSERT INTO marques (nom_membre) VALUES ($1) RETURNING *;", [`${name}`]);
|
const res = await client.query("INSERT INTO marques (nom_membre) VALUES ($1) RETURNING *;", [`${name}`]);
|
||||||
if (res.rows.length === 0) {
|
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 };
|
|
||||||
await client.end();
|
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) => {
|
const register = async (req: Request, res: Response) => {
|
||||||
if (!req || !req.body || !req.body.name) {
|
if (!req || !req.body || !req.body.name) {
|
||||||
res.status(400).send();
|
res.status(400).send();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const name: string = req.body.name;
|
const name: string = req.body.name;
|
||||||
const marque: Either<Marque, string> = await createMarque(name);
|
const marque: Either<Marque, string> = await createMarque(name);
|
||||||
if (marque.hasRight) {
|
if (marque.hasRight) {
|
||||||
res.status(403).send(marque.right);
|
res.status(403).send(marque.right);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(200).send(marque);
|
res.status(200).send(marque);
|
||||||
}
|
}
|
||||||
|
|
||||||
export { register };
|
export { register };
|
||||||
|
@ -1,36 +1,36 @@
|
|||||||
import { RequestHandler } from "express";
|
import { RequestHandler } from "express";
|
||||||
import memberService from "../services/member.service";
|
import memberService from "../services/member.service";
|
||||||
|
|
||||||
const memberByName: RequestHandler<{name: string;}> = async (req, res) => {
|
const memberByName: RequestHandler<{ name: string; }> = async (req, res) => {
|
||||||
const memberEither = await memberService.getMember(req.params.name);
|
const memberEither = await memberService.getMember(req.params.name);
|
||||||
|
|
||||||
if (memberEither.hasRight) {
|
if (memberEither.hasRight) {
|
||||||
res.sendStatus(404);
|
res.sendStatus(404);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const member = memberEither.left;
|
const member = memberEither.left;
|
||||||
|
|
||||||
res.render('member.ejs', { member });
|
res.render('member.ejs', { member });
|
||||||
};
|
};
|
||||||
|
|
||||||
const memberById: RequestHandler<{id: string;}> = async (req, res) => {
|
const memberById: RequestHandler<{ id: string; }> = async (req, res) => {
|
||||||
const id = parseInt(req.params.id);
|
const id = parseInt(req.params.id);
|
||||||
const memberEither = await memberService.getMember(id);
|
const memberEither = await memberService.getMember(id);
|
||||||
|
|
||||||
if (memberEither.hasRight) {
|
if (memberEither.hasRight) {
|
||||||
res.sendStatus(404);
|
res.sendStatus(404);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const member = memberEither.left;
|
const member = memberEither.left;
|
||||||
|
|
||||||
res.render('member.ejs', { member });
|
res.render('member.ejs', { member });
|
||||||
};
|
};
|
||||||
|
|
||||||
const memberController = {
|
const memberController = {
|
||||||
memberByName,
|
memberByName,
|
||||||
memberById,
|
memberById,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default memberController;
|
export default memberController;
|
||||||
|
@ -6,43 +6,43 @@ import { Member } from '../types/member';
|
|||||||
import { Either } from '../utils/utils';
|
import { Either } from '../utils/utils';
|
||||||
|
|
||||||
const extractBearerToken = (headerValue: string) => {
|
const extractBearerToken = (headerValue: string) => {
|
||||||
const matches = headerValue.match(/(bearer)\s+(\S+)/i);
|
const matches = headerValue.match(/(bearer)\s+(\S+)/i);
|
||||||
return matches && matches[2];
|
return matches && matches[2];
|
||||||
}
|
}
|
||||||
|
|
||||||
const getUsername = async (req: Request, res: Response, next: () => void) => {
|
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) {
|
if (!token) {
|
||||||
next();
|
next();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const { payload } = await jwtVerify(token, JWT_SECRET_KEY);
|
const { payload } = await jwtVerify(token, JWT_SECRET_KEY);
|
||||||
const name: string = payload.name as string;
|
const name: string = payload.name as string;
|
||||||
const member: Either<Member, string> = await memberService.getMember(name);
|
const member: Either<Member, string> = await memberService.getMember(name);
|
||||||
|
|
||||||
if (!member.hasRight) {
|
if (!member.hasRight) {
|
||||||
res.locals.user = {
|
res.locals.user = {
|
||||||
id_member: member.left.id_member,
|
id_member: member.left.id_member,
|
||||||
name: member.left.name
|
name: member.left.name
|
||||||
}
|
}
|
||||||
|
|
||||||
next()
|
next()
|
||||||
} else {
|
} else {
|
||||||
res.status(401).send(member.right)
|
res.status(401).send(member.right)
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
res.status(401).send();
|
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
res.status(401).send();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function verifyAuthentication(req: Request, res: Response, next: () => void) {
|
function verifyAuthentication(req: Request, res: Response, next: () => void) {
|
||||||
if (!res.locals.user) {
|
if (!res.locals.user) {
|
||||||
res.status(401).send();
|
res.status(401).send();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
|
|
||||||
export { getUsername, verifyAuthentication };
|
export { getUsername, verifyAuthentication };
|
||||||
|
@ -1,13 +1,19 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Leohl !dworl</title>
|
<title>Leohl !dworl</title>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<h1>Naisu</h1>
|
<h1>Naisu</h1>
|
||||||
<h2><%= message %></h2>
|
<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>
|
<%= 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>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
@ -1,12 +1,20 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title><%= member.name %></title>
|
<title>
|
||||||
|
<%= member.name %>
|
||||||
|
</title>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<h1><%= member.name %></h1>
|
<h1>
|
||||||
<h2>id: <%= member.id_member%></h2>
|
<%= member.name %>
|
||||||
|
</h1>
|
||||||
|
<h2>id: <%= member.id_member%>
|
||||||
|
</h2>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
type Marque = {
|
type Marque = {
|
||||||
id_marque: number,
|
id_marque: number,
|
||||||
name: string
|
name: string
|
||||||
};
|
};
|
||||||
|
|
||||||
export { Marque };
|
export { Marque };
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
type User = {
|
type User = {
|
||||||
id_member: number,
|
id_member: number,
|
||||||
name: string
|
name: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Member = User & { password: string };
|
type Member = User & { password: string };
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
type Either<T, U> = {
|
type Either<T, U> = {
|
||||||
hasRight: boolean,
|
hasRight: boolean,
|
||||||
left: T,
|
left: T,
|
||||||
right: U
|
right: U
|
||||||
};
|
};
|
||||||
|
|
||||||
function eitherLeft<T, U>(left: T): Either<T, 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> {
|
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 };
|
export { Either, eitherLeft, eitherRight };
|
||||||
|
Loading…
x
Reference in New Issue
Block a user