How to Show/Hide Elements Based on User Roles with Ionic [v4]

ionic-4-role-based-elements-header

When you build an authentication system for your app it’s very likely that you also have some sort of role management. If you want to show or hide different elements in your Ionic app based on a role, this is for your.

In this Quick Win we will use a dummy auth service (so no real backend) but a real directive that will accept a role as input and automatically checks if the user has the role and display the component then.

ionic-4-role-auth

We will have a user and admin, and we’ll also quickly add some animations but let me know below if you’d like to see more content on animations!

Setup Our Role Based App

We start with a blank Ionic 4 app (using the beta while writing this) and add the Angular animations package, a service and directive for our app and finally another module which will become the shared module of our directive:

Once our app ist ready we need to import the Animations package and at the same time remove the import of our directive. It’s automatically added but we can’t use it like this with lazy loading so remove it for now and we’ll see how to add it later. Go ahead and change your app/app.module.ts to:

That’s it for the setup, now let’s build our dummy auth service.

Creating a Dummy Authentication Service

This service is now a lot simplified but you could still use it for your own case as you basically only have to make the HTPP calls to your API in the right places.

In our case we keep the currentUser as a BehaviourSubject in our service so others can easily subscribe to the authentication state, a system you might have used or seen with Firebase before as well.

Of course our login makes no sense but the important part is to have a user with roles that you can check later. In your case, your backend should return those roles so you can keep them in the app and use the hasRoles() later to make a quick check.

For now change your app/services/auth.service.ts to:

Now we can login users (not a real auth system!) and we get their roles so let’s focus on the directive to check the roles now.

The Role Based Angular Directive

The idea of our directive is that it can be easily added to any element inside your app to quickly check if the user is allowed to see it, but of course it could also be used for other cases.

If you want to protect whole pages, you should check out my Quick Win on using Route Guards with Ionic 4!

So our directive will later look like this in action:

This means we can pass an array of strings to our directive and it then decides if the user is allowed to see the element.

Before we build the directive, we need to make it available to our pages and that’s where our shared module comes into play. This file will declare and export all directives (of course just the one in our case) so we can import it into the modules of our lazy loaded pages later. Now change the app/directives/shared-directives.module.ts to this:

Now we can build the real directive, and as said before it needs to handle an array of roles and check if the user has all those roles.

To achieve this we subscribe to the current user state through the Subject (more clearly, the Observable that we get from the Subject) and either call createEmbeddedView() to show it or clear() to remove it from the view.

There’s nothing else this directive needs to do so insert this into your app/directives/has-role.directive.ts:

Now we only need to apply it to our page!

Applying Our Role Based Directive

First of all we need to import the shared module so our page actually knows about the directive. You can do this with every lazy loaded page in your app, but right now we only got one so go ahead and import it to your app/home/home.module.ts like this:

Although the logic for our app is very dummy like and we don’t really need a lot, we need a few functions to trigger our authentication changes and login/logout the user.

As said before I also wanted to include some basic animations which you’ll find at the top inside the @Component annotation. Those animations will fade in/out the elements where we add our directive later, so go ahead and put all of this into your app/home/home.page.ts:

Finally we get to the usage of our directive, but you’ve already seen a spoiler of how to use it before so it’s not really new now 🙁

Anyway, you can apply your directive anywhere and simply add the roles the user needs to have in order to see the element. Besides that we add the animations in form of [@fadeInOut] which is enough to trigger the fade animations once the element is not shown or back on the screen again!

In our view we now apply the directive a few times to show elements to users, admins or both and to get the desired result change your app/home/home.page.html to:

And that’s how you create a simply role based directive to show and hide elements anywhere inside your Ionic 4 app!

The original idea came from an Ionic Academy user who wanted to display items inside his app based on a purchase of a user, so that’s another case this directive could be use for with some minor changes.

You can also find a video version of this Quick Win below.

Tweet
Reddit
+1
Share
Email