Anomalum
Sure, I don't think this "good code" but this was the best we (me & ChatGPT) were able to come up with 🥲
<style>
/* Initially hide the .App--user content */
.hidden-content {
display: none;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function () {
// Hide the .App--user initially
const userAppElement = document.querySelector('.App--user');
if (userAppElement) {
userAppElement.classList.add('hidden-content');
}
// Check if the URL contains "/u/" to determine if it's a profile page
const path = window.location.pathname;
const isProfilePage = path.includes('/u/');
if (isProfilePage) {
// Get the current user and the profile being viewed
const currentUser = app.session.user;
const profileUsername = path.split('/u/')[1].split('/')[0]; // Assumes URL format contains /u/:username
// Check if the user is logged in
if (currentUser) {
const currentUsername = currentUser.username(); // Get the current user's username
const currentUserGroups = currentUser.groups();
// Check if the user is viewing their own profile or is in allowed groups
const isAllowed = currentUsername === profileUsername || currentUserGroups.some(group => group.id() === '1' || group.id() === '4');
if (isAllowed) {
// Show the .App--user content if the user is authorized
userAppElement.classList.remove('hidden-content');
} else {
// If not allowed, update the URL to the home page and refresh
window.history.replaceState({}, document.title, '/');
location.reload();
}
} else {
// If not logged in, update the URL to the home page and refresh
window.history.replaceState({}, document.title, '/');
location.reload();
}
} else {
// Not on a profile page, show the .App--user content
if (userAppElement) {
userAppElement.classList.remove('hidden-content');
}
}
});
</script>
Here 1 & 4 are the group ids of Admins & Mods respectively.