Right, let's finish up the authentication part of our app, we can log in, we can register, but forgot
password does nothing when it is actually tapped.
So we need a way to send the user a password change request.
And luckily, of course, Firebase makes that pretty easy for us.
So go over to your auth view controller.
And inside there we have this action called Forgot Tapped.
OK, so what we want to do here is just make sure, first of all, that the email address that they're
putting in is actually long enough.
Right.
So we do have an input valid here, but that checks the email and the password.
So this is probably the point at which I would start to separate things out.
So I might say let's have function email valid.
OK, and then down here, what I would do is cut this email and put it there, and then of course,
this is going to not render properly, but I can just say if the email is valid.
Or rather, if email is not valid, then we will have basically the same result.
So the email is not valid.
The past is not valid.
Off we go.
OK, and that means in email valid.
We can have what we have here.
We can have the same logic.
So if email don't count is less than what did we say, five something at something dot something.
Yeah, less than six actually.
That would be like A at B C that's six characters in total.
Then we're going to return false.
OK, otherwise we're going to return to SRI.
Same logic as we've got down here, except we've now separated this out into its own function, so we
don't call it again because in the future you could imagine in a production app, you're actually going
to have a proper email validation function, something that checks using regular expressions if the
format is correct.
We're just doing characters for experience here because, you know, you don't really need to learn
this until you're actually doing it yourself.
Search on stack overflow for our ex email, Regitze regex email and you'll get what you need.
It's a bit confusing at first, but you'll get there anyway.
We've got four got tapped.
So if email valid or rather if the email is not valid, then we are going to do what we can actually
tell the user utilities show alert error.
Give them the message email not valid.
And the controller is, of course, self, OK, so that's going to tell them your email is not valid
and they'll be able to figure out why it's not valid because, you know, it doesn't have an act.
It doesn't have a period.
They'll figure it out.
Otherwise, what we're going to do.
And we're going to put Elsia, we could just put return here so we don't have to write else, that's
a top tip for you.
Sometimes when you're doing if Elsas like this or if LGF is IFES, then these things can get nested
quite far down.
You know, you have like this and then you'll have like this.
And all this nesting will carry on down, down the stages.
I find wherever possible.
It's much cleaner looking and easier to understand if I do something like a return on the if statement
so I can take my code all the way back to that indentation point where the cursor is currently flashing.
OK, so now we can call the auth object from Firebase and we can look for the item that we want here,
which is send password reset and then we want with email initiates, a password reset for the email
address.
So this on the firebase end is going to grab that email address and send the password reset, go through
the whole process on Firebase, not in our app.
And we can customize that within the firebase app itself, not here, but within the Firebase UI online
so that, you know, the email layouts and things are within the theme of your application.
To be honest, I wouldn't bother with that right now.
I would just go with whatever is given at this point.
OK, we don't have the email, do we?
OK, so what we're going to do is perhaps get the email input dart text.
And that was that wasn't it.
Yes, that's correct.
Because we kind of know if the emails valid them are OK.
So yet we are effectively repeating this line.
I don't think that's a problem when it comes to text input boxes.
To be honest, some people will see differently about that and then we're going to have an error.
And if Firebase returns an error, then, of course, we can show their utility alert and we can just
give it the error dot localized description.
And the view controller, of course, is Seth.
If ever there we go is not equal to no, that means there is an error, then we're going to grab this
localized description.
Which, of course, is give me an error.
Let's see what it is.
Now we can force unwrap it.
Or.
So so let me explain what's just happened here.
It says we can force unwrap the localized description error or we can say test it.
If there is a localized description or if there is an error, then do the following.
Then use that description, rather, if there isn't a localized description, then give me a default
value that I can use.
But you know what?
I'm just going to skip all that because I know there is an error at this point because I've tested it
up here.
So I could say if let E equals error and remove this nail, then down here I could grab the E localized
a description.
Right.
So we're testing if it exists or not.
The other way was effectively the same way of doing it, just with a force onramp.
This way that you're looking at is probably a safer way of doing it, because in the previous one,
when I'm forced unwrapping it, if I change my code in any way, that force unwrapped will probably
stay in.
I won't pick up that.
It's something I need to change and that could lead to trouble down the road.
So this is a safe way of doing it.
OK, and then once we've got that, well, we're just going to return, otherwise we are going to tell
the user, shall we let
success?
So she's going to say, yep, everything is all good.
So now if I play this in our little iPod Touch.
You can see the screen recording in the background there.
It's like a behind the scenes show, this, isn't it, when you see the recordings in the folders and
stuff that I'm preparing here.
So if I hit forgot password email's not valid, if I do some random email that I know doesn't exist.
There's no use a record correspondent to identify the user may have been deleted.
OK, fair enough.
But then of course, that will work if we put in a real email address.
I know it works because I've tested it prior to this, but I won't I'll spare you the boring details
of that.
But by all means, test it out yourself.
We are missing one thing in our code here, though, and that is if the activate UI, that's what we
want.
So if the email is not valid, I don't care about that.
So we want to inactivate the UI and then at the end here we are going to activate the UI.
Now this is going to happen.
There before any error checking, before any success alerts, because we, of course, we need self
and we're doing it before everything, because once we've gone through the network, called to Firebase
or our local checks, then we want the users to actually press buttons from that point because they'll
go after their email, they'll come back with the password and put it in some kind of hit forgot password
email.
Not valid.
And now if I put in a bogus one and I do it, watch the UI.
There we go.
There was a little spinny ball and off we go.
OK, so that is it working perfectly now, he is a bit of homework for you.
You'll notice I can grab the top and pull it down.
And if I do that, I just get a blank screen underneath because obviously it can't connect to anything
if you're not logged in.
So it'll be a useless app.
What I want you to do for your homework or your task here is to figure out using Google or Stack Overflow,
how to stop this view controller from disappearing.
It should amount to one line of code, although it could be more depending on your solution.
So go ahead and do that and I'll give you the solution next time.