Regardless of how the app allows the user to enter their username and password, the Firebase SDK provides the means to create the user account. Firebase provides an authentication call to create the user. Also, the Google Firebase Console provides a view of the data to verify the action was successful.
- [Narrator] Now that we have our project set up for authentication, it's time to actually create a user. There's a function on the authentication object called "create user." It takes a callback closure with two parameters, "user" and "error." Obviously if it succeeds, the user is signed in and we should be passed back a user. Otherwise there is an error. In normal cases we would check for that. In our case we're going to assume the user is successful and will display his name by printing it out to the console. That's what we'll do when we handle the response.
We'll also be able to go to the Firebase console and verify that a user was created. So back in Xcode, we have our UI setup, we've imported Firebase into our 'ViewController'. We also have in our 'AppDelegate' Firebase imported and configured. So in our 'ViewController' we're going to implement our do button create function to create a user. First thing we need to do is to be able to get the email and password that was entered by the user.
We're going to optionally bind those. So on line 32 I'll type, 'if let email=tfEmail.text, let password=tfPassword.text', and then the body of this 'if' I'll use that email and password to create the user. So on line 33 I'll type, 'Auth.auth().createUser'.
This takes an email which we have so, with an email placeholder I'll type, 'email'. For the password placeholder I'll replace it with 'password'. For the completion block, I'll specify the two return parameters as user and error. Assuming there was no error, I should have a user now and I can print out their name. I can print it from what was returned to us. In this case I'll print out the email address. But I can also print it out in the 'Auth.auth().currentUser' dot email, or other values like user id.
Since these are optionals, I'll also provide the no coalescence option for another string, 'no userid'. And for the email, on line 34 'no email'. So if was able to login I should be able to print these two items, and I'll set a break point here for it. So now we'll run the app, stopping the previous version from running. And I'll enter an email address. If it's not a valid email address we'll get an error telling us that.
Create the account and I get the call back. I print out the email address to the console, and also the user ID. I should also be able to see those in the console through the browser. So if I go back to the console, click on 'Users', refresh, I have one user now with the identifier of my email address. It told me I logged in through email password, creation date, sign in, and my user ID.
I can provide other actions on this like reset password, diasble account, delete account straight from that line on the console. So by using the createUser call on the auth object, we're able to create a user with an email and password that we can verify on the Firebase console.
- Creating a new project and app
- Installing Firebase CocoaPods
- Configuring authentication with Firebase
- Building the Firebase authentication UI
- Configuring data storage with Firebase Realtime Database
- Storing files in Cloud Storage
- Working with data from Firestore