Hi, Alex here from rebeloper.com and welcome back to the channel and welcome
back to this final episode of how to sign in without a password in firebase.
So without further ado let's jump right into it.
Okay in this final episode we
are going to round it all up and we are going to first of all set up some
capabilities and some URL type. So let's start with that and the easiest one to
start off with is the URL types so what we need to do is copy our a bundle
identifier go back to info and as you can see here we have URL types and hit
that little plus button that has just appeared for the identifiers let's add
in here open app, great. URL scheme just paste that in there your bundle
identifier, okay. One final thing go to capabilities but before we do that let's
just copy our URI prefix. So that is what you want to copy out and go
back to capabilities and select associated domains and turn that on and
let's just wait for this to finish so we do get our entitlements ready. Click that
plus button and delete all of this and type in app links and a colon and just
paste your link there that is your a deep link that you have already set up
here for the URI prefix. Again go ahead and make sure under capabilities app
links colon and firebase passwordless sign-in page link, okay. Now that is it
for the setup of our info.plist file and our capabilities. Now what we want to do
is to the AppDelegate file and add in
some functions and the first one is going to be created by us and it will be
a function called and with URL that will contain a URL and we will just return a
bool value. Let's return here false for now, okay.
So we get rid of this error, okay. So what we want to overwrite now is application
restoration handler. Believe this is it. Yes, so make sure that this application
user activity restoration handler and we are going to return here user activity a
dot and webpage URL and we are going to map that out. So map rather yet let's use
flat map here and just delete all of this
and add in here our function that we have just created. So handle with URL.
There we go. Let's get all of these braces, sorry for confusion that's the way it needs to
do and because this is an optional very we might as well unwrap it, okay.
Now it's time to undo the meat of the handle password less sign-in. So what we want to
get here first is let's see. Let link equals with URL and maybe we need to add
in here underscore. There we go and absolute string, okay. Now that we do have
our link we need to check if we can log in with an email and a link through
firebase. So let's do here if auth and I believe we need to import firebase
authentication. So import firebase auth. There we go and let's import firebase
firestore because we are going to store all of this data in the firestore
database, okay. So auth there we go dot auth sign in with email link and let's
see here it's link. So if we can do that then we can basically go ahead and
start the sign in. Okay and what it would mean is auth dot auth sign in with
email and the link. This is what we want to do but of course we need to grab the
email first. So let's see here let email and if you
recall we did save the email to our user default. So user defaults dot standard
dot value for key and the key is setup dot and kEmail, okay. This is an
optional values so let's guard let unwrap that. Else return, okay. Let's add that
email in there and our link in there. Now let's take a look at the completion.
We get back a result and we get back an error message. So let's take care of the
error message first I believe we need to return false here, oh and we need to set
this as string. There we go. Live coding, okay. Let's go forward with our
authorization. So if let error equals error then we are going to print out our
errors localized description and we are going to return simple as that.
Next up we want to guard let unwrap our result.
So guard let result equals result, yes. We want to return again okay return, sorry
about that auto completion and now that we do have
our result we want to grab our UID and that is our result dot user, there we go dot UID.
Okay and why do we want to do that? Well that is because we want to basically say
all of this in our firestored database. Okay and let's see here.
Firestore or rather yet let's just create our data what we want to send.
So let data equals and let's see here.
UID is the first thing that we want to append in here it will be a UID, okay.
Next up let's see email and that is our email. So let's add in here time stamp
and that is a field value and server time stamp, okay. I believe this should be
not capital, okay. Time stamp. Let's make this a little bit
prettier by adding these new lines in here, okay. Now what we want to do is
create a path where we want to set this data. Of course we might want to fix this
so we can have this as a string to any, okay.
So what we want type in here is firestore dot firestore and the
collection path will be users, okay and we want to have our document with our UID
because that is pretty easy to query so set data and I will choose this this one
so we're going to set this data only once so we don't need merge, so we don't
with set merged true of course because if we want to just update some values
then you set merge true so it doesn't override the whole thing. So we are going
to choose this one and the data will be of course our data and in the completion
we do get an error. So let's handle that so I will
just copy and paste this. There we go and now we can let's say display a UI alert.
So everything went fine. So UI alert service dot show alert and let's see
this will be a style of alert title success and the message successfully
signed in. Yeah, that would be okay and just so we get all of these let's see here.
We want to have some other alerts just so the user get alerted when in
case of an error. So let's add that in there and I will call the title error and
let's add in here our errors localized description and again I will copy and
paste this and scroll all the way down here and add that in there, okay.
Now this should take care of it and I'm pretty sure that this will work so let's
hit command B, okay. Let me just show you user is nil okay that is a great till now.
Let's just sign in and type in our email address. Let's send our magic link
let's open up our mail app and hopefully we will get here our email. There we go.
Now let's sign in to no password sign in. Let's hit open here. There was a bad
request for some reason. So maybe we should add here return true and that
should take care of it. I will add in here some more UI alerts do go ahead and
take a look at the github repository it's in the link in the description
below. Now that is it for this tutorial series. If you have any questions or you
do get stuck anywhere along the way go ahead and comment down below and make
sure that you take a look at the github repository for any typos or something
like that and if you did like this video go ahead and give it a thumbs up,
subscribe to the channel and if you really really like this tutorial series
hit that notification bell to get notified all new ones and as usual I
will see you in the next one.