How To Manage Users With Firebase In 10 Minutes

Olivia Rhye
11 Jan 2022
5 min read

Managing users is part of building any app. And yet it feels intimidating every time because people are what makes apps―screwing up user management leads to security threats and unhappy customers.

But it doesn't have to be this way. You don't have to build an authentication and authorization system from scratch, and you certainly don't need to worry about leaking data every time you want to build something new. Libraries exist. Auth services exist. And in today's article we are going to see exactly how to do that with little to no code at all thanks to Rowy, the Firebase CMS.

Without further ado, let's see how to build the following user management database:

1.png

1. Creating An Account On Rowy

First, get setup with Rowy using this link. It takes only a couple of minutes to connect your Firestore database and set up your project with the step-by-step guided process. You can also self-host Rowy using the open-source version (https://github.com/rowyio/rowy).

As previously mentionned, Rowy is a Firebase CMS with a cool spreadsheet user interface and handy low-code tools, so we don't actually store any data ourselves. Firebase has Firebase Auth to handle authentication, Security Rules to manage authorization, and Firestore to store user data. Let's see how to leverage all three.

2. Adding Basic Columns

At this point we have an empty table connected to Firestore's userManagement collection and no data, so we need to define columns. Let's start simple with some basic information we'll need to improve our users' experience: a first name, a last name, an email address, a residential address, and a country, for example. Just click on the Add column button and you'll be asked to pick a name and a data type for each:

2.jpg

You can ask for these information in your front-end app and use Firestore's API to update it in the database. For example, this is how you sign up a new user:

import { initializeApp } from "firebase/app"

import {
    getAuth,
    createUserWithEmailAndPassword,
    sendEmailVerification
} from "firebase/auth";

const firebaseConfig = YOUR_FIREBASE_CONFIG_HERE
const app = initializeApp(firebaseConfig)
const auth = getAuth(app)

const signup = async (email, password) => {
    await createUserWithEmailAndPassword(auth, email, password)
    await sendEmailVerification(auth.currentUser)
}

And this is how you update user information with Firebase's Admin SDK:

await auth.updateUser(user_uid, {
      firstName: 'John',
      lastName: 'Doe'
})

For more information on Firebase Auth, check out our article on the subject.

As a database admin, you'll need several features to make your life easier like disabling user accounts, resetting passwords, or deleting spam accounts. Let's see how in the following sections.

3. Disabling user accounts

Disabling user accounts require some code. Rowy provides Action columns to make it easy to run custom code, so we are going to use them. An action column is simply a column displayed as a button that, once pressed, runs a custom script from the column configuration.

Disabling a user is as simple as an API call to Firebase Auth:

await auth.updateUser(ref.id, {
    disabled:true
})

By default, action scripts can access your whole Firebase environment, so you don't have to worry about fetching data or importing Firebase services―it's all there already. An action script must return a success boolean and a message:

4.jpg

4. Resetting user passwords

The same principle applies to resetting user passwords:

const action:Action = async ({row,ref,db,storage,auth,actionParams,user}) => {
    await sendPasswordResetEmail(auth, row.email)

    return {success:true, message:`${row.email} account: password reset successfully`}
}

5. Deleting user accounts

And for deleting user accounts:

const action:Action = async ({row,ref,db,storage,auth,actionParams,user}) => {
    await auth.deleteUser(ref.id)
    await ref.delete()
    return {success:true, message:`${row.email} account has been successfully deleted`}
}

Finally, we obtain the same result available in our official User Management demo:

3.jpg

Check Out More Demos

User management is key to any web or mobile app, but it can't be your only value proposition. Browse our 24 other demos and examples on how to use Rowy to build powerful apps like how to detect text in receipts to automate accounting with Vision AI and GPT-3 or how to generate images with Stable Diffusion.

Olivia Rhye
11 Jan 2022
5 min read
Get started with Rowy in minutes

Continue reading

Browse all