Welcome back to class everyone Johnny B here with slope stop calm.
And in this lesson we are going to work on implementing firebase authentication.
All right so the last lesson we created our nice lookin luggin views here.
We got one for logging in and one for creating that user.
So in this lesson we are going to work on hooking those up and getting them actually working and being
able to logon users.
So the first thing we're going to need to do is add the firebase authentication SDK to our project which
we're going to do via cocoa pods.
So if you're under our Pod's target here I can click on our pod file here and we can just add this right
here is from the seed pod single quote.
Firebase slash off get to see that.
And then just going to open up a terminal and I've already I'm already in our CD project here.
So all you've got to do is save pod install and that will update and install the new fire based off
SDK.
All right so we got that install just jump back in here.
What's this error here that we may need to close this and reopen it.
So that's fine is going to re open it.
We're going to build that just to make sure everything's building fine.
All right.
We've got a successful build.
So now we have firebase off inside of our project.
Now this isn't something specific to fire store.
This is a separate SDK that you can use in the fire store database or the real time database.
But just wanted to show you how to use this very important to be able to offer users it.
All right.
So next up we need to go to our hot fire store database.
All right.
So get up in here in a console or in the browser window here and you may notice that we now have an
Android app in our project.
All right so you're able to have different platforms all using the same database.
So I just want to show you that we have our android version of the app run it over here next to our
version and you can kind of see that they have the same sets of data and I guess is a pretty cool thing.
Both of those up and running at the same time offer the same database and if you want to learn how to
make this app in the Android platform then go ahead and check out that course after you finish up this
one.
So I just want to show you that real quick and all right.
So we need to activate the authentication.
So that's right.
Over here above our database we're going to come into it.
And here we're going to go to the seinen method all right and firebase.
The SDK provides API is to integrate with a whole bunch of different types of providers.
We have email phone Google Facebook Twitter Khattab and anonymous.
And so you can use any of these two off hand logging users for this course.
We're going to focus on the e-mail and password as that is one of the most commonly used types of authentication.
So just go ahead and click right here and check enable and to save.
All right so now we have e-mail and password enabled on our project.
So now let's jump back into the X code.
All right.
So to sign the user and we need to first have an existing user right.
So let's we're going to first start in the create user of these.
So that's over here and our controller folder and create user Visi Swift.
You can just talk a little bit real quick about kind of the process and flow of logging a user in.
All right.
So to sign a user into the app you first get the authentication credentials from the user.
And in our case this will be the email and the password.
If we were using Twitter or Facebook or Google Plus we would get an off token from that provider.
And then you pass these credentials to the firebase authentication STK the firebase back in services
then verify those credentials and return a response to the client.
And then after a successful sign and we have access to the user's basic profile information and also
we can control the users access to our database with security rules.
All right so let's get to it to create a user.
It's pretty simple.
Well first we need to import firebase here.
Say import
firebase and then we're going to say I'm off.
OK so off the base class for firebase authentication it manages the authentication for firebase apps.
So we'll say off dot and we access the instance of it and then we say to create user.
OK.
And this is the one we want here.
We create a user with an e-mail.
OK I'm just going to put a empty string there for now and then our completion going to press Return
to get our completion.
And inside of our response we get an optional user and an optional error.
Kips want to rename those user and error.
All right and let's handle the first one to say if left ever is equal to error.
So if there is an error then we're just going to debug printed.
So when I say Debug.
Print and we're going to say error creating user and then do a string interpellation and we will print
out the error dot.
That should be error right there.
There we go.
Griddata localized description.
Get snow if there wasn't an error and we were successful we created a user rights and also talk a little
bit about the user object the user instance the user has a fixed set of basic properties.
It has a unique id called the ID.
And in the case of logging in with e-mail the primary e-mail with which it was logged in.
And that's pretty much it.
There's not a whole lot of other information that you can access or even add.
There are a couple of other things that we can add.
I'm using a transaction that we're going to look into here in a second we can add a display name and
a photo you are l.
All right.
But that's pretty much it.
If you want to add other things like say age employment occupation I or whatever other things you might
need to store for a user.
You would have to create a new document in a collection and you would want to put put as the ID for
that document the ID for that user.
OK.
So we're going to cover both of those instances right here.
All right so we've created a user.
Let's go ahead and add a display name so that we can have a user name to use throughout our app.
OK.
So I want to say what we got is we got to create a change request because when we see that change request
equal I'm going to say user dot create profile change request and.
Says This creates an object which may be used to change the user profile data.
And then we are going to say change request to display name and you'll see here that the other option
is photo you URL.
OK so a change request that display display name and that is going to be equal to the user name.
But we need to actually define that don't we.
So here let's go ahead and create our variable's for the e-mail password and username that we get from
our i'd be outlet's up here.
So I'm just going to say God let email people he mail text that text and then you can actually chain
guards.
So I'm going say comma and I'm going to say that password equal password text that text when I miss
a comma and the user name equal username text dot text Ninnis else return OK.
So if any of these fail then it's just going to return right out.
All right.
So let's see here.
All right so we have created a change request.
We are saying we're going to be updating the display name and then all we have to do is say Change Request
tot commit changes and we get a completion Hendler and press enter and when you get an optional Harris
when to say error in the code and here we're just going to debug print this as well so we'll see if
that error is equal to error and then Debug.
Print and I'm just going to print that right out.
Localize description.
OK so now we have created a user and we have added a display name with this code right here.
And before I forget let's go ahead and add these parameters to our create user functions we are creating
a user with email and the e-mail is this e-mail that we know very well that we created here which comes
from the e-mail textfield and Pittsford here.
Let's see here which is password password text just password.
There we go.
All right so this is how we can add a display ending but I said if you want to add additional information
then we need to be able to create a new document in a new collection that that holds those are.
So we're going to do is we are going to say fire store dot fire store and then we're going to need a
reference to a collection and this collection doesn't exist yet but it is going to be called users.
OK.
And because we don't like to have just magic strings laying around we're going to copy this and we're
going to go to our Constans folder where you at utilities Constans file and I'm going to create a new
ref here going to say Commons or a user's ref actually.
Users are ref is equal to users getting a say that jump back in to create users and then replace this
with users ref.
OK.
So we are referencing a new collection that we haven't created yet.
And then instead of using add a document here like we have done in the past which generates an auto
generated document ID what we want to do is we want to set the document ID equal to the user's ID.
That way in the future when we want to reference this document we can just grab it by the user's ID.
All right.
So the way to do that is we actually say document.
And then we explicitly set the the document name the document ID in the document ID is the user ID.
Now the user ID is optional so I'm going to do a guard str. here in this regard that user id equal user
that you ID else.
Return.
So now we have the user id and we were and said that as the document Id say user ID and then we say
Dot set data and we want the one with the completion handler right here.
All right so a return on the data then tab over to the completion handler and return on that and say
error right there and return on the code and enter.
All right.
So I'm just going to put in a couple of fields here you can add anything that you would like like the
age occupation hobbies whatever whatever you want.
I'm going to put in fields for the username and the time the date created OK.
Basically a time stamp.
So I'm going to write here I'm going to do enter to make this a little bit more readable and then enter
again.
And so here for the field name I'm going to say user name which is a constant that we already have.
And then here I'm going to put in the user name which we created up above here.
And then the second field I'm going to I'm going to create a new constant.
And just because I'm one is referring to a user time stamp doesn't really quite sound right.
So I'm going to say let the date created equal date created.
Get that on a copy constant and jump back into our accrete user visi.
And here we are at a pace that and the value is field value dot servers timestamp.
And then in our completion we're just going to see if left error is equal to error if there isn't error
that we are going to debug.
Print that errors when we say error.
Localized description if there is not an error then we are going to actually just dismiss this view
controller and go back to the log in your control and then we'll log in there.
So I would say self-taught dismiss animated is true and completion is now looks good.
And then if we can't sell it then we're going to do this self-taught dismiss animated true completion.
No actually here I don't think we need the self.
Are you going to save this and we will quickly run through what we have covered so far.
So we have activated the e-mail password authentication in our firebase account.
We have added our authentication create user function here we pass in an email and a password.
If there is not an error then the first thing we're going to do is going to go ahead and add a property
to our user We're going to add a display name so that we can reference a username throughout the rest
of the app and then assuming that there is additional information that we want to store for each user
when they are created.
We are adding a document where the document ID is set to the user's ID so that it will be easily accessible
later on if we need it and then we set the data.
And when you set data.
If the document does not exist it will create the document.
And then if everything goes according to plan we just dismiss the create user Visi and go back to the
log in VC after successfully creating a user.
All right.
So the next lesson we were going to cover logging in after we have created a user and will go on from
there.
Right.
So even.