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.
There are typically two separate steps to a user signing in to an app:
- 1.Authenticate: Let the user verify their identity and securely enter your app (this is what Doorman does).
- 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.Make a request to your database, and check if the user exists/has completed onboarding.
- 2.If the user exists, send them into your app.
- 3.If the user does not exist, send them to an onboarding flow.
.png?alt=media&token=2d79af6e-d335-4973-ac75-927d9854b6a3)
Adding your user to the database with Doorman is easy. It just requires a few extra steps after the initial set up.
This is an oversimplified version of the logic we want to create:
if (user) {
// user is authenticated
const uid = user.uid // user id
// check DB to see if the user exists
const { exists } = await doesUserExistInDb(uid)
// set state based on the DB value
setUserExists(exists)
} else {
// user is not signed in, reset the state
setUserExists(false)
}
In a component, this oversimplified code might look like this:
// this screen shows up after the user has authenticated
export default function App() {
const [checkingIfExists, setCheckingIfExists] = useState(false)
const [userExists, setUserExists] = useState(false)
const onAuthStateChanged = async (user) => {
if (user) {
// user is authenticated
const uid = user.uid // user id
// show loading indicator
setCheckingIfExists(true)
// check DB to see if the user exists
const { exists } = await doesUserExistInDb(uid)
// set state based on the DB value
setUserExists(exists)
// set loading state to false
setCheckingIfExists(false)
} else {
// user is not signed in, reset the state
setUserExists(false)
setCheckingIfExists(false)
}
}
// if the DB call is loading
if (checkingIfExists) return <LoadingScreen />
// if the user doesn't exist
if (!userExists) return <OnboardingStack />
// user exists! render the app
return <App />
}
This screen would show up after your user has authenticated.
Let's continue to the next page to see how to implement this in real code with Doorman:
Last modified 3yr ago