Back

Protecting certain elements or a page using labels

  • 0
  • Auth
  • Web
Zip
21 May, 2024, 10:42

I will just be straight on.

TL;DR
Developers sought assistance with protecting certain page elements based on user labels. They struggled with mapping through items and setting up admin label checks. Ultimately, they managed to hide elements successfully based on the 'hidden' variable in the constants. Solution: In the Sidebar.tsx file, the developers were able to hide items using the 'hidden' variable in the constants. They fetched the user label using 'appwriteService.getCurrentUserLabel()' and leveraged this information to control element visibility.
Zip
21 May, 2024, 10:43

did this to fetch the label

Zip
21 May, 2024, 10:45
TypeScript
if (!item.hidden) {
    return (
      <div key={item.path} className="text-white">
        {item.submenu ? (
          <>
            <button
              //Sub Menus
              onClick={toggleSubMenu}
              className={`flex flex-row items-center p-2 rounded-lg w-full justify-between hover:bg-white hover:text-black ${
                pathname.includes(item.path) ? 'bg-white text-black ' : ''
              }`}
            >
              <div className="flex flex-row space-x-4 items-center">
                {item.icon}
                <span className="font-semibold text-xl  flex">{item.title}</span>
              </div>

              <div className={`${subMenuOpen ? 'rotate-180' : ''} flex`}>
                <Icon icon="lucide:chevron-down" width="24" height="24" />
              </div>
            </button>

            {subMenuOpen && (
              <div className="my-2 ml-12 flex flex-col space-y-4">
                {item.subMenuItems?.map((subItem, idx) => {
                  return (
                    <Link
                      key={idx}
                      href={subItem.path}
                      className={`${
                        subItem.path === pathname ? 'font-bold' : ''
                      }`}
                    >
                      <span>{subItem.title}</span>
                    </Link>
                  );
                })}
              </div>
            )}
          </>
        ) : (
          //Menus
          <Link
            href={item.path}
            className={`flex flex-row space-x-4 items-center p-2 rounded-lg hover:bg-zinc-100 hover:text-black ${
              item.path === pathname ? 'bg-zinc-100 text-black' : ''
            }`}
          >
            {item.icon}
            <span className="font-semibold text-xl flex">{item.title}</span>
          </Link>
        )}
      </div>
    );
  }

This is Sidebar.tsx, currently hides items that has the hidden variable in my constants which is:

TypeScript
import { Icon } from '@iconify/react';
import { SideNavItem } from './types';
import appwriteService from "@/appwrite/config";


const userLabel = appwriteService.getCurrentUserLabel();

export const SIDENAV_ITEMS: SideNavItem[] = [
  {
    title: 'Home',
    path: '/dashboard/home',
    icon: <Icon icon="lucide:home" width="24" height="24" />,
  },
  {
    title: 'Controls',
    path: '/dashboard/controls',
    icon: <Icon icon="lucide:folder" width="24" height="24" />,
    hidden: true,
  },
  {
    title: 'Messages',
    path: '/dashboard/messages',
    icon: <Icon icon="lucide:mail" width="24" height="24" />,
  },
  {
    title: 'Settings',
    path: '/dashboard/settings',
    icon: <Icon icon="lucide:settings" width="24" height="24" />,
    submenu: true,
    subMenuItems: [
      { title: 'Account', path: '/dashboard/settings/account' },
      { title: 'Privacy', path: '/dashboard/settings/privacy' },
    ],
  },
Zip
21 May, 2024, 10:45

now i'm stuck not knowing what to do

Zip
21 May, 2024, 10:50

I gave up on this before, but came back in order to finish it. I'm lost

Steven
24 May, 2024, 23:27

No worries. We all start somewhere!

Zip
24 May, 2024, 23:40

But if you dont mind, how would i exactly going to test account.get to be shown in the logs

Steven
24 May, 2024, 23:43

Do you know how to log things?

Zip
24 May, 2024, 23:43

That should be the console in the site itself

Steven
24 May, 2024, 23:52

Yes. Do you know how to log things there?

Zip
25 May, 2024, 02:49

Not sure though. The only thing that comes in my mind is running it and just checking the console

Steven
25 May, 2024, 03:34

Yes but how to do make stuff show up in the console?

Zip
25 May, 2024, 04:37

That i do not know

Steven
25 May, 2024, 04:38

Have you used console.log()?

Have you done any courses or tutorials on JavaScript?

Zip
25 May, 2024, 04:47

Forgot about that, and as for js. I haven't actually. Only watched a few and know how some of the function works when i look at it but can't really do it when i try to implement it, i really have no knowledge on making websites. Its just that my professor gave me this without him teaching us basic java or javascript properly

Steven
25 May, 2024, 04:49

console.log() is used to log things. So you can call account.get() and then log the result to see the details about it.

Anyways, you should probably learn some more programming or JavaScript basics before trying to learn react

Zip
2 Jun, 2024, 06:54

apologies for not replying for like a week. Homeworks and tp's are a hassle. Just got back on this project again, it does show the user having the label ['admin']. Now I tried a couple of things so that it will actually be only shown if the user is an admin. But still couldn't work it around.

Zip
2 Jun, 2024, 06:54

now I was wondering whether i have to do something with this, cause I am sure that im lacking something. Its just basically checking for the label only

Zip
2 Jun, 2024, 07:30

tried playing around with useEffect but I still can't find a solution

Zip
2 Jun, 2024, 07:41

Wait. I might have actually done it. But its late to appear? on the console, its printing "User is an admin" 12 times

Steven
3 Jun, 2024, 03:25

How is setUserRole() related to item?

Zip
3 Jun, 2024, 10:24

i used a useState so i can set a boolean whether the user does or does not have the label admin. I managed to make it work but its mapping through every single item slowly. Its printing 12 times and the controls element will show at around the 5th print

Steven
3 Jun, 2024, 14:49

Please share your of your code

Steven
3 Jun, 2024, 14:49

Mapping through? Where are you fetching the user?

Zip
6 Jun, 2024, 08:47

Sorry for not replying for a couple of days. Do you mind setting this as solved? I managed to make things work now

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more