-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.controller.js
More file actions
151 lines (135 loc) · 4.66 KB
/
Copy pathUser.controller.js
File metadata and controls
151 lines (135 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const { User, CreateUserModel, UpdateUserModel } = require("../models/Models")
const jsonwebtoken = require("jsonwebtoken");
const bcrypt = require("bcrypt");
module.exports = {
async getAllUsers(req, res) {
try {
const users = await User.findAll({
attributes: ["id", "nom_prenom", "username", "createdAt", "updatedAt"]
});
res.Response({ data: users })
} catch (error) {
res.status(400).Response({ message: error.message })
}
},
async createUser(req, res, next) {
try {
CreateUserModel.validateAsync(req.body).then(async (value) => {
// Check if the username already exists
const existingUser = await User.findOne({
where: {
username: req.body.username
}
});
if (existingUser) {
return res.status(400).Response({ message: "Username already exists !" });
}
const newUser = await User.create({
nom_prenom: req.body.nom_prenom,
username: req.body.username,
password: bcrypt.hashSync(req.body.password, parseInt(process.env.UserPasswordSaltRound))
})
res.Response({ data: newUser })
}).catch(err => {
next(err);
})
} catch (error) {
next(err);
}
},
async getUserByUsername(req, res) {
try {
const user = await User.findOne({
where: {
username: req.params.username
},
order: [["createdAt", "DESC"]],
attributes: ["id", "nom_prenom", "username", "createdAt", "updatedAt"]
})
if (!user) {
res.status(404).Response({ message: "User not found !" })
}
else {
res.Response({ data: user })
}
} catch (error) {
res.status(400).Response({ message: error.message })
}
},
async deleteUser(req, res) {
User.destroy({
where: {
username: req.params.username
}
}).then(async (value) => {
if (value == 0) {
res.status(404).Response({ message: "User not found !" })
}
else {
res.Response({ message: "User deleted !" })
}
}).catch(error => {
res.status(400).Response({ message: error.message })
})
},
async updateUser(req, res) {
try {
const user = await User.findOne({
where: {
username: req.params.username
}
})
if (!user) {
res.status(404).Response({ message: "User not found !" })
}
else {
UpdateUserModel.validateAsync(req.body).then(async (value) => {
await user.update(req.body)
await user.save()
res.Response({ data: user })
})
}
} catch (error) {
res.status(400).Response({ message: error.message })
}
},
async loginUser(req, res) {
try {
const user_exist = await User.findOne({
where: {
username: req.body.username,
}
});
if (!user_exist) {
return res.status(401).Response({ message: "Invalid credentials" });
}
// Vérifier le mot de passe
const isPasswordValid = bcrypt.compareSync(req.body.password, user_exist.password);
if (!isPasswordValid) {
return res.status(401).Response({ message: "Invalid credentials" });
}
// Générer le token JWT
const token = jsonwebtoken.sign(
{
id: user_exist.id,
username: user_exist.username,
nom_prenom: user_exist.nom_prenom,
role: user_exist.role,
},
process.env.JWTKey,
{ expiresIn: '24h' }
);
// Format OAuth2
res.Response({
data: {
access_token: token,
token_type: "Bearer",
username: user_exist.username,
nom_prenom: user_exist.nom_prenom
}
});
} catch (error) {
res.status(400).Response({ message: error.message });
}
}
}