Skip to content

Commit

Permalink
admin role
Browse files Browse the repository at this point in the history
  • Loading branch information
angelalvaigle committed Dec 8, 2024
1 parent 1c662a6 commit 5cfc211
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
11 changes: 6 additions & 5 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ app.post('/addquestion', async (req, res) => {
// Forward the add question request to the question service
const addQuestionResponse = await axios.post(
questionServiceUrl + '/addquestion',
req.body
req.body,
{
headers: {
Authorization: req.headers.authorization,
},
}
);
res.json(addQuestionResponse.data);
} catch (error) {
Expand All @@ -121,8 +126,6 @@ app.get('/questions', async (req, res) => {
});

app.get('/game-questions', async (req, res) => {
console.log('gw');
console.log(req.headers.authorization);
try {
// Forward the get question request to the question service
const getQuestionResponse = await axios.get(
Expand All @@ -142,8 +145,6 @@ app.get('/game-questions', async (req, res) => {
});

app.post('/addstat', async (req, res) => {
console.log('gw');
console.log(req.headers.authorization);
try {
// Forward the add stat request to the stat service
const addStatResponse = await axios.post(
Expand Down
3 changes: 0 additions & 3 deletions statservice/stat-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ export const addStatController = async (req, res) => {
'points',
]);

console.log('addstat');
console.log(req.user.userId);

const newStat = new Stat({
userId: req.user.userId,
gameId: req.body.gameId,
Expand Down
3 changes: 2 additions & 1 deletion users/userservice/user-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export const addUserController = async (req, res) => {

// Encrypt the password before saving it
const hashedPassword = await bcrypt.hash(req.body.password, 10);

const isFirstAccount = (await User.countDocuments()) === 0;
req.body.role = isFirstAccount ? 'admin' : 'user';
const newUser = new User({
name: req.body.name,
lastName: req.body.lastName,
Expand Down
27 changes: 18 additions & 9 deletions webapp/src/components/AddQuestionContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,24 @@ const AddQuestionContainer = () => {
wrong3,
}) => {
try {
await axios.post(`${apiEndpoint}/addquestion`, {
type,
name,
path,
right,
wrong1,
wrong2,
wrong3,
});
const token = localStorage.getItem('token');
await axios.post(
`${apiEndpoint}/addquestion`,
{
type,
name,
path,
right,
wrong1,
wrong2,
wrong3,
},
{
headers: {
Authorization: `Bearer ${token}`,
},
}
);
setOpenSnackbar(true);
} catch (error) {
console.log(error);
Expand Down
7 changes: 4 additions & 3 deletions webapp/src/pages/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ const Login = () => {
withCredentials: true,
}
);
console.log(response.data);
const { token } = response.data;
localStorage.setItem('token', token); // Guarda el token

console.log(token);
setOpenSnackbar(true);
navigate('/dashboard');
// Añadir un retardo antes de navegar
setTimeout(() => {
navigate('/dashboard');
}, 2000); // 2000 ms = 2 segundos
} catch (error) {
setError(error.response.data.error);
}
Expand Down

0 comments on commit 5cfc211

Please sign in to comment.