Hi everyone, in the last lecture, we learned how to install Firebase and integrate it to our app.
In this lecture, we'll learn to do user authentication with Firebase.
Alright! Last time we installed the Firebase Core framework.
That's enough for setup and integration stuff.
To do Authentication, we need to do more.
In your Firebase console, let's choose the Firebase project for our app.
This is the Firebase dashboard for the app.
On the left hand side, choose Authentication.
First of all, we need to tell Firebase what sign-in method we want to use in our app.
These are sign-in methods Firebase supports.
For this app, we'll use email authentication, so let's enable it.
We learned social sign in in the Zero2Launch chat app already.
Let's see the manual to see what we need to do next.
Manuals is actually our best friends for exploring a library.
Alright, the first thing we need to do is to install the Firebase Authentication library...
so that we can use authentication tools from Firebase.
To do so, simply go back to Pod-file.
And tell pod that we need the Firebase Authentication package.
Then go to the Terminal to let pod actually install these packages by using the "pod install" command.
Pod will look at the Pod-file and install everything we specified in there.
OK GOOD! It's working very hard to install the libraries we want.
OK, let's give the system sometime for processing new stuff.
Alright, unlike social log in where users already have their own social network accounts.
For email log in, we need to create Firebase users first before we can log them in.
In our app, after users sign up their accounts,...
the app will switch to the tab bar controller.
The key point is that users must be authenticated by Firebase before they can see the tab bar views.
So the idea is after creating an account and save it to the database,...
we'll authenticate then fetch the user information from the database before switching views.
Alright, in the Sign Up View Controller swift file and delete this unnecessary stuff.
In the Navigator, hold Option then click the Start storyboard to open it in the Assistant Editor.
Note that the app needs to perform all the tasks we've just discussed after one single touch on the sign up button.
So we need a function in the source code to perform those tasks in response to that single touch on the button.
That connection can be created by selecting the button,...
holding Control then dragging to the source code.
Choose the connection type as Action.
Then name the method something meaningful.
Here we use sign up button touch up inside.
Touch up inside here matches the touch event option below.
It means users touch then lift their fingers inside the bounds of the button.
This connection is called an IB Action, where IB means Interface Builder.
Basically, this function will be executed when the touch event that we specified occurs.
Alright, let's go back to the Firebase manual.
This section shows how we can create a Firebase user.
Steps 1 and 2 were done in the last lecture.
Then this single command lets us create a new Firebase user.
This is the simple interface we mentioned in the last lecture.
We simply grab the Firebase authentication tool, which is the Fire-Auth class,...
call the create-User function with the email and password that the user inputs into our app.
All complicated authentication stuff on the server will be done by Firebase behind the scene.
OK GOOD!...
We will create a Firebase user when the sign up button is hitted.
Let's grab the Firebase authentication toolbox.
Alright, we don't have it here.
Although we installed the package, we still need to import it into our project.
OK. Firebase-Auth imported.
Now type F-I-R...
we can now see the toolbox we want in Xcode suggestions.
This F-I-R-Auth class provides all Firebase authentication tools we need.
But we need an instance of this class to use the tools.
So we need to call this auth() method.
If you go to its reference, you'll see that this is a method of the F-I-R-Auth class,...
which simply returns an instance of the F-I-R-Auth class.
So we can use this instance to call instance methods of this class.
We know what method we want to use.
Simply search for create-User.
GOOD! It's rather straightforward.
Let's input some dummy email and password to experiment with the method.
The last parameter might not be so trivial.
This parameter is a Closure.
You can think of it as a function without a name.
Not exactly, but sort of.
Its parameters or inputs are an Optional Firebase User object, and an Optional Error object.
You name them whatever you like.
Here we use user and error for simplicity.
These are objects we received back from Firebase after the Firebase user creation process.
The user object is an Optional 'cause it might be nil if the authentication process failed due to lost connection or things like that.
In this case, Firebase returned back to us a non-nil error object.
We can check if it's the case using this if statement.
If it's indeed the case, we can just ignore it or print it out to see what's wrong.
Seeing through Xcode suggestions, we'll see that the localized-Description property of this Error object...
can tell us what's wrong.
Finally, simply escape from this method if this user creation process failed...
If there's no error, the authentication process is successful.
We'll received back from Firebase a F-I-R-User object.
Let's print it out to see how it looks.
We'll decide how to proceed later, like switching views and stuff.
There's some warnings, but let's not worry about them for now.
Note that for now we hard code the user email and password to user1 and 1 2 3 4 5 6.
Right now we don't get that information from user input.
We'll do that later.
Alright, here it is.
Let's go to the sign up view to create a Firebase user.
As seen in the code, hitting the sign up button means we want to create a user with email "user1 @ gmail dot com".
OK, the view is switched but that is expected.
In the console, you can see that a F-I-R-User was indeed created and printed out.
Now, the view switched although we don't do that in the source code...
because of this segue we created in the past.
Actually, it's because of this segue from the sign up button.
Let's delete it.
Using segues is not a great way to switch views together with authenticate users.
We'll learn to switch views properly in the source code after we authenticate users.
Now let's go to Firebase to see the new Firebase user.
We can see this information under the Users tab in the Authentication section.
Alright, let's refresh the page.
Note that this is NOT the database. We'll construct it later.
Fantastic!
The Firebase user we've created is sitting nicely here with all associated information.
We'll finish this sign up business next time. See you then!
No comments:
Post a Comment