create-lyrajs 1.0.15 → 1.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
package/template/package.json
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
"maestro": "maestro"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@lyra-js/core": "^1.0.
|
|
10
|
+
"@lyra-js/core": "^1.0.14",
|
|
11
11
|
"bcrypt": "^6.0.0",
|
|
12
12
|
"cookie-parser": "^1.4.7",
|
|
13
13
|
"cors": "^2.8.5",
|
|
@@ -24,7 +24,6 @@
|
|
|
24
24
|
"@types/bcrypt": "^5.0.2",
|
|
25
25
|
"@types/cookie-parser": "^1.4.8",
|
|
26
26
|
"@types/cors": "^2.8.18",
|
|
27
|
-
"@types/dotenv": "^8.2.3",
|
|
28
27
|
"@types/express": "^5.0.1",
|
|
29
28
|
"@types/jsonwebtoken": "^9.0.9",
|
|
30
29
|
"@types/node": "^22.15.17",
|
|
@@ -2,11 +2,12 @@ import { NextFunction, Request, Response } from "express"
|
|
|
2
2
|
import { Validator, ValidationException } from "@lyra-js/core"
|
|
3
3
|
import { User } from "@entity/User"
|
|
4
4
|
import { userRepository } from "@repository/UserRepository"
|
|
5
|
+
import bcrypt from "bcrypt";
|
|
5
6
|
|
|
6
7
|
export class UserController {
|
|
7
8
|
static list = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
|
8
9
|
try {
|
|
9
|
-
const users = await userRepository.findAll().map( (user: User) => {
|
|
10
|
+
const users = (await userRepository.findAll()).map( (user: User) => {
|
|
10
11
|
delete user.password
|
|
11
12
|
return user
|
|
12
13
|
} )
|
|
@@ -55,7 +56,25 @@ export class UserController {
|
|
|
55
56
|
new ValidationException("Password is to weak. I must be 10 characters long, including at least 1 lowercase, 1 uppercase, 1 number and 1 special character.")
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
data.
|
|
59
|
+
const isEmailUsed = await userRepository.findOneBy({ data.email })
|
|
60
|
+
|
|
61
|
+
if (isEmailUsed) {
|
|
62
|
+
throw new Error("Email already in use")
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (!Validator.isPasswordValid(data.password)) {
|
|
66
|
+
throw new Error("Invalid password")
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const user = new User()
|
|
70
|
+
const hashedPassword = await bcrypt.hash(data.password, 10)
|
|
71
|
+
|
|
72
|
+
user.username = data.username
|
|
73
|
+
user.firstname = data.firstname
|
|
74
|
+
user.lastname = data.lastname
|
|
75
|
+
user.email = data.email
|
|
76
|
+
user.password = hashedPassword
|
|
77
|
+
user.role = 'ROLE_USER'
|
|
59
78
|
|
|
60
79
|
await userRepository.save(data)
|
|
61
80
|
res.status(201).json({ message: "User created successfully" })
|
|
@@ -69,6 +88,7 @@ export class UserController {
|
|
|
69
88
|
const { data }: {data: User} = req.body
|
|
70
89
|
const user = await userRepository.find(data.id)
|
|
71
90
|
if (!user) res.status(404).json({ message: "User not found" })
|
|
91
|
+
if (user && data.password) data.password = await bcrypt.hash(data.password, 10);
|
|
72
92
|
if (user) await userRepository.save(data)
|
|
73
93
|
res.status(200).json({ message: "Users updated successfully" })
|
|
74
94
|
} catch (error) {
|
|
@@ -5,6 +5,6 @@ export const userRoutes = Router()
|
|
|
5
5
|
|
|
6
6
|
userRoutes.get("/all", UserController.list)
|
|
7
7
|
userRoutes.get("/:id", UserController.read)
|
|
8
|
-
userRoutes.
|
|
8
|
+
userRoutes.post("/", UserController.create)
|
|
9
9
|
userRoutes.patch("/:id", UserController.update)
|
|
10
10
|
userRoutes.delete("/:id", UserController.delete)
|
package/template/src/server.ts
CHANGED
|
@@ -15,7 +15,7 @@ const securityConfig = new SecurityConfig().getConfig()
|
|
|
15
15
|
const port = process.env.PORT
|
|
16
16
|
const app = express()
|
|
17
17
|
|
|
18
|
-
app.set("trust proxy",
|
|
18
|
+
app.set("trust proxy", false)
|
|
19
19
|
app.use(cookieParser())
|
|
20
20
|
app.use(express.json({ limit: securityConfig.limits.request_max_size || "10mb" }))
|
|
21
21
|
app.use(express.urlencoded({ limit: securityConfig.limits.request_max_size || "10mb", extended: true }))
|