Skip to content

Commit

Permalink
[frontend] redirect to "/login" if unauthorized user action (#253)
Browse files Browse the repository at this point in the history
* Fixed redirect to /login if user action is unauthorized
* Added missing 'id' attribute to the HTML of setting modal window
  • Loading branch information
lim396 authored Feb 13, 2024
1 parent 30041ba commit a8752b2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
45 changes: 44 additions & 1 deletion frontend/app/lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ export async function createRoom(
body: payload,
});
const data = await res.json();
if (res.status === 401) {
redirect("/login");
}
if (!res.ok) {
console.error("createRoom error: ", data);
return { error: data.message };
Expand All @@ -251,6 +254,9 @@ export async function createDirectRoom(userId: number) {
}),
});
const data = await res.json();
if (res.status === 401) {
redirect("/login");
}
if (!res.ok) {
console.error("createDirectRoom error: ", data);
return { error: data.message };
Expand All @@ -276,6 +282,9 @@ export async function joinRoom(
body: payload,
});
const data = await res.json();
if (res.status === 401) {
redirect("/login");
}
if (res.status === 409) {
redirect(`/room/${roomId}`, RedirectType.push);
} else if (!res.ok) {
Expand All @@ -297,6 +306,9 @@ export async function inviteUserToRoom(roomId: number, userId: number) {
},
);
const data = await res.json();
if (res.status === 401) {
redirect("/login");
}
if (!res.ok) {
console.error("inviteUserToRoom error: ", data);
return "Error";
Expand All @@ -319,6 +331,9 @@ export async function updateRoom(
},
body: JSON.stringify({ name: roomName, accessLevel, password }),
});
if (res.status === 401) {
redirect("/login");
}
if (!res.ok) {
console.error("updateRoom error: ", await res.json());
return "Error";
Expand All @@ -341,6 +356,9 @@ export async function updateRoomUser(
body: JSON.stringify({ role }),
});
console.log(res.status);
if (res.status === 401) {
redirect("/login");
}
if (!res.ok) {
console.error("updateRoomUser error: ", await res.json());
return "Error";
Expand All @@ -361,6 +379,9 @@ export async function kickUserOnRoom(roomId: number, userId: number) {
},
},
);
if (res.status === 401) {
redirect("/login");
}
if (!res.ok) {
console.error("kickUserOnRoom error: ", await res.json());
return "Error";
Expand Down Expand Up @@ -389,6 +410,9 @@ export async function uploadAvatar(formData: FormData) {
body: payload,
});
const data = await res.json();
if (res.status === 401) {
redirect("/login");
}
if (!res.ok) {
console.error("uploadAvatar error: ", data);
return "Error";
Expand Down Expand Up @@ -421,6 +445,10 @@ export async function updatePassword(
}
const currentPassword = formData.get("current-password");
const user = await getCurrentUser();
if (!user) {
redirect("/login");
return "Error";
}

// Check if current password is correct
const res1 = await fetch(`${process.env.API_URL}/auth/login`, {
Expand Down Expand Up @@ -759,6 +787,9 @@ export async function muteUser(
body: JSON.stringify({ duration: durationSec }),
},
);
if (res.status === 401) {
redirect("/login");
}
if (!res.ok) {
console.error("muteUser error: ", await res.json());
return "Error";
Expand Down Expand Up @@ -792,6 +823,9 @@ export async function unmuteUser(roomId: number, userId: number) {
},
},
);
if (res.status === 401) {
redirect("/login");
}
if (!res.ok) {
console.error("unmuteUser error: ", await res.json());
return "Error";
Expand All @@ -810,6 +844,9 @@ export async function banUser(roomId: number, userId: number) {
},
},
);
if (res.status === 401) {
redirect("/login");
}
if (!res.ok) {
console.error("banUser error: ", await res.json());
return "Error";
Expand Down Expand Up @@ -844,6 +881,9 @@ export async function unbanUser(roomId: number, userId: number) {
},
},
);
if (res.status === 401) {
redirect("/login");
}
if (!res.ok) {
console.error("unbanUser error: ", await res.json());
return "Error";
Expand All @@ -860,11 +900,14 @@ export async function leaveRoom(roomId: number) {
Authorization: "Bearer " + getAccessToken(),
},
});
if (res.status === 401) {
redirect("/login");
}
if (!res.ok) {
console.error("leaveRoom error: ", await res.json());
return "Error";
} else {
redirect(`/room`, RedirectType.push);
redirect("/room");
return "Success";
}
}
Expand Down
1 change: 1 addition & 0 deletions frontend/app/ui/room/setting-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export default function SettingModal({
<select
className="bg-white text-black"
defaultValue={room.accessLevel}
id="accessLevel"
{...register("selectedAccessLevel")}
>
<option value="PUBLIC">PUBLIC</option>
Expand Down

0 comments on commit a8752b2

Please sign in to comment.