Firebase In Use 1: Dynamic Admin-User Roles with Authentication

Firebase In Use 1: Dynamic Admin-User Roles with Authentication

If you know my man Sam Stern who’s popped up a couple times around my Reddit, you probably heard his talk with Kiana McNellis on Integrating back-end systems with Firebase for better app management. This sparked an idea for what could be a new series here on this blog, Firebase In Use where I go over frequently used app implementations of Firebase for you to learn and implement in your own app.

And what better way for me to start that than to do what I do best: copy and wash... except it won’t be! Correct me if I’m wrong, but the use of the Firebase Admin SDK allowed the setting of specific users as Admins via the server… but this ain’t so dynamic.

This is going to be my version of the integrating back-end systems talk that the Firebase guys did, but this will be dynamic. This will allow user roles to change as they use the app… and this is a challenge to my boys at Firebase. Who’s done it better? Allow me to demand £100, three cups of coffee and a part-time job there if I win the vote.

The App we’re making

We’re going to making a simple group management panel, like the ones of group chats in Messenger where users can be assigned one of two roles:

Admin – These have the power to invite, kick, and change the role of other users, on top of anything a normal user can do.

User – These have the power to view the other members of the group.

The Dependencies we’re importing

Of course, I expect that you’ve already connected your app to Firebase. If you haven’t, what are you doing here? Just go…

On top of that, import the Authentication, Firestore, and FirebaseUI-Auth dependencies above. We won’t be needing anything else. In fact, we’re just using FirebaseUI so we don’t have to make our own authentication screen. Otherwise, you can do without it

Also make sure Email/Password provider is enabled in your Firebase Console.

The Things you’re… copy-pasting

Do you really want me to explain every single list view and text view here? That’s tedious for both us. Just create these classes and layouts and dump them in your app.

activity_main.xml

list_item_member.xml

menu_main.xml

menu_user.xml

User.kt

UsersAdapter.kt

The Code you’re actually writing

First things first, let’s get authentication out the way. We start with this in MainActivity

MainActivity.kt

To explain things a bit, this launches the FirebaseUI Auth if the user isn’t currently authenticated. It also handles inflating the menu which contains a sign out function. That just about wraps up the authentication part.

Adding New Group (FAB)

Create a new activity called GroupActivity where we’ll handle the creation and viewing of groups. Let the FAB in MainActivity send the intent. Note the extra with the intent.

MainActivity.kt (Snippet)

Now in GroupActivity, write a new function createNewGroup() that creates a new group under the “groups” collection of Firestore and stores the current user as an admin of the group. Let it run only if the intent extra is given.

GroupActivity.kt

As soon as the group is created, I also add the same group to the user’s subcollection so that the groups can show up in the user’s MainActivity.

It’s all looking good in our database. So that we can go back into the groups we created, we’ll go back into MainActivity and add the function getGroupsList() and invoke at the end of your OnCreate method.

MainActivity.kt (Snippet)

And now we have our groups showing in the MainActivity.

Now we have the user as an admin, now we need a way to invite other users to fill in the “user” role. (On another note, ignore the skewed FAB).

I changed the FAB in MainActivity to instead show a menu dialog that gives the choice of creating and joining a group. (Yes, I made it so you have to enter the group ID to join. Yes, I’m sadistic. Face it)

MainActivity.kt (Snippet)

Now in GroupActivity, we’re going to get this ID and use it to inflate a list using the UsersAdapter we created much earlier.

activity_group.xml

GroupActivity.kt (Snippet)

We’re almost there! In GroupActivity, we can now see the list of members, but joining a group only opens it up and doesn’t add the user to that group. Let’s go fix that.

I created the function joinGroup() to add the user to the group document and the group to the user document. This time, the user is added as a “user” instead of an admin.

GroupActivity.kt (Snippet)

Now we can have other users added into the group as, well, users.

Now our roles are set, but they don’t do anything. Let’s go into UsersAdapter and add a delete user function there.

UsersAdapter.kt (Snippet)

GroupActivity.kt (Snippet)

Now when I try to delete a user from a user role, I get this message.

But doing the same on another account with the permissions allows the delete to go through…. And that’s roles!

The Message I’m Concluding With

If you want to see the full app and tinker with it yourself, I shared it on Github. You’re welcome.

So that’s Firebase In Use, an installment to this blog that would be interesting for me at the very least. It’s really out of my comfort zone to make these demo apps but if that’s the best way to go about certain topics and use cases, so be it. We both learn.

So how was it? Was it too much code and too little explaining? Just right perhaps? Let me know. How about the series in general? Do you want to see more Firebase In Use?