Add user to the database
You probably want to know how to add a user to your database after they authenticate. With Doorman, that's simple.
If you already have your own logic set up for adding users to your database, then you might not need this guide, but we still recommend reading through it.
Want to skip to the real code example? Click here.

Core Concepts

There are typically two separate steps to a user signing in to an app:
  1. 1.
    Authenticate: Let the user verify their identity and securely enter your app (this is what Doorman does).
  2. 2.
    Add user to database: Most apps want to collect information from users after they authenticate, like a name, profile picture, etc. In order to do so, once a user has authenticated, you will probably do the following steps:
    1. 1.
      Make a request to your database, and check if the user exists/has completed onboarding.
    2. 2.
      If the user exists, send them into your app.
    3. 3.
      If the user does not exist, send them to an onboarding flow.

Visualization

How to add user to DB

Adding your user to the database with Doorman is easy. It just requires a few extra steps after the initial set up.

Oversimplified example

This is an oversimplified version of the logic we want to create:
1
if (user) {
2
// user is authenticated
3
const uid = user.uid // user id
4
5
// check DB to see if the user exists
6
const { exists } = await doesUserExistInDb(uid)
7
8
// set state based on the DB value
9
setUserExists(exists)
10
} else {
11
// user is not signed in, reset the state
12
setUserExists(false)
13
}
Copied!
In a component, this oversimplified code might look like this:
1
// this screen shows up after the user has authenticated
2
3
export default function App() {
4
const [checkingIfExists, setCheckingIfExists] = useState(false)
5
const [userExists, setUserExists] = useState(false)
6
7
const onAuthStateChanged = async (user) => {
8
if (user) {
9
// user is authenticated
10
11
const uid = user.uid // user id
12
13
// show loading indicator
14
setCheckingIfExists(true)
15
16
// check DB to see if the user exists
17
const { exists } = await doesUserExistInDb(uid)
18
19
// set state based on the DB value
20
setUserExists(exists)
21
22
// set loading state to false
23
setCheckingIfExists(false)
24
} else {
25
// user is not signed in, reset the state
26
setUserExists(false)
27
setCheckingIfExists(false)
28
}
29
}
30
31
// if the DB call is loading
32
if (checkingIfExists) return <LoadingScreen />
33
34
// if the user doesn't exist
35
if (!userExists) return <OnboardingStack />
36
37
// user exists! render the app
38
return <App />
39
}
Copied!
This screen would show up after your user has authenticated.

Real code example

Let's continue to the next page to see how to implement this in real code with Doorman: