1 00:00:00,300 --> 00:00:06,360 Right, let's finish up the authentication part of our app, we can log in, we can register, but forgot 2 00:00:06,360 --> 00:00:09,260 password does nothing when it is actually tapped. 3 00:00:09,720 --> 00:00:13,740 So we need a way to send the user a password change request. 4 00:00:14,010 --> 00:00:17,390 And luckily, of course, Firebase makes that pretty easy for us. 5 00:00:17,940 --> 00:00:20,880 So go over to your auth view controller. 6 00:00:21,300 --> 00:00:24,870 And inside there we have this action called Forgot Tapped. 7 00:00:25,650 --> 00:00:31,920 OK, so what we want to do here is just make sure, first of all, that the email address that they're 8 00:00:31,920 --> 00:00:34,050 putting in is actually long enough. 9 00:00:34,710 --> 00:00:35,230 Right. 10 00:00:35,250 --> 00:00:42,480 So we do have an input valid here, but that checks the email and the password. 11 00:00:43,080 --> 00:00:48,060 So this is probably the point at which I would start to separate things out. 12 00:00:48,600 --> 00:00:54,060 So I might say let's have function email valid. 13 00:01:01,130 --> 00:01:09,530 OK, and then down here, what I would do is cut this email and put it there, and then of course, 14 00:01:09,560 --> 00:01:14,660 this is going to not render properly, but I can just say if the email is valid. 15 00:01:15,910 --> 00:01:22,940 Or rather, if email is not valid, then we will have basically the same result. 16 00:01:23,320 --> 00:01:24,280 So the email is not valid. 17 00:01:24,280 --> 00:01:25,300 The past is not valid. 18 00:01:25,720 --> 00:01:26,340 Off we go. 19 00:01:27,190 --> 00:01:29,230 OK, and that means in email valid. 20 00:01:29,230 --> 00:01:31,140 We can have what we have here. 21 00:01:31,480 --> 00:01:32,880 We can have the same logic. 22 00:01:33,370 --> 00:01:43,750 So if email don't count is less than what did we say, five something at something dot something. 23 00:01:43,900 --> 00:01:45,990 Yeah, less than six actually. 24 00:01:46,810 --> 00:01:51,730 That would be like A at B C that's six characters in total. 25 00:01:54,680 --> 00:01:56,360 Then we're going to return false. 26 00:01:57,890 --> 00:02:01,270 OK, otherwise we're going to return to SRI. 27 00:02:02,960 --> 00:02:08,750 Same logic as we've got down here, except we've now separated this out into its own function, so we 28 00:02:08,750 --> 00:02:14,180 don't call it again because in the future you could imagine in a production app, you're actually going 29 00:02:14,180 --> 00:02:19,670 to have a proper email validation function, something that checks using regular expressions if the 30 00:02:19,670 --> 00:02:21,080 format is correct. 31 00:02:21,080 --> 00:02:25,850 We're just doing characters for experience here because, you know, you don't really need to learn 32 00:02:25,850 --> 00:02:28,280 this until you're actually doing it yourself. 33 00:02:29,120 --> 00:02:36,620 Search on stack overflow for our ex email, Regitze regex email and you'll get what you need. 34 00:02:36,800 --> 00:02:39,640 It's a bit confusing at first, but you'll get there anyway. 35 00:02:39,950 --> 00:02:41,120 We've got four got tapped. 36 00:02:41,120 --> 00:02:51,140 So if email valid or rather if the email is not valid, then we are going to do what we can actually 37 00:02:51,140 --> 00:02:56,840 tell the user utilities show alert error. 38 00:02:59,280 --> 00:03:04,560 Give them the message email not valid. 39 00:03:06,870 --> 00:03:13,730 And the controller is, of course, self, OK, so that's going to tell them your email is not valid 40 00:03:14,190 --> 00:03:19,290 and they'll be able to figure out why it's not valid because, you know, it doesn't have an act. 41 00:03:19,290 --> 00:03:20,430 It doesn't have a period. 42 00:03:20,880 --> 00:03:21,780 They'll figure it out. 43 00:03:22,380 --> 00:03:24,510 Otherwise, what we're going to do. 44 00:03:25,950 --> 00:03:32,520 And we're going to put Elsia, we could just put return here so we don't have to write else, that's 45 00:03:32,520 --> 00:03:33,450 a top tip for you. 46 00:03:33,930 --> 00:03:41,670 Sometimes when you're doing if Elsas like this or if LGF is IFES, then these things can get nested 47 00:03:41,670 --> 00:03:42,720 quite far down. 48 00:03:43,080 --> 00:03:46,320 You know, you have like this and then you'll have like this. 49 00:03:47,010 --> 00:03:50,610 And all this nesting will carry on down, down the stages. 50 00:03:51,360 --> 00:03:52,920 I find wherever possible. 51 00:03:53,400 --> 00:03:59,490 It's much cleaner looking and easier to understand if I do something like a return on the if statement 52 00:03:59,700 --> 00:04:06,340 so I can take my code all the way back to that indentation point where the cursor is currently flashing. 53 00:04:07,290 --> 00:04:16,650 OK, so now we can call the auth object from Firebase and we can look for the item that we want here, 54 00:04:16,650 --> 00:04:24,300 which is send password reset and then we want with email initiates, a password reset for the email 55 00:04:24,300 --> 00:04:24,680 address. 56 00:04:24,990 --> 00:04:31,410 So this on the firebase end is going to grab that email address and send the password reset, go through 57 00:04:31,410 --> 00:04:33,900 the whole process on Firebase, not in our app. 58 00:04:34,110 --> 00:04:41,340 And we can customize that within the firebase app itself, not here, but within the Firebase UI online 59 00:04:41,760 --> 00:04:46,820 so that, you know, the email layouts and things are within the theme of your application. 60 00:04:47,220 --> 00:04:49,530 To be honest, I wouldn't bother with that right now. 61 00:04:50,040 --> 00:04:53,410 I would just go with whatever is given at this point. 62 00:04:54,460 --> 00:04:57,450 OK, we don't have the email, do we? 63 00:04:58,620 --> 00:05:06,030 OK, so what we're going to do is perhaps get the email input dart text. 64 00:05:07,290 --> 00:05:08,730 And that was that wasn't it. 65 00:05:09,240 --> 00:05:10,260 Yes, that's correct. 66 00:05:10,950 --> 00:05:13,520 Because we kind of know if the emails valid them are OK. 67 00:05:14,160 --> 00:05:16,890 So yet we are effectively repeating this line. 68 00:05:17,190 --> 00:05:20,390 I don't think that's a problem when it comes to text input boxes. 69 00:05:20,400 --> 00:05:26,580 To be honest, some people will see differently about that and then we're going to have an error. 70 00:05:28,230 --> 00:05:37,290 And if Firebase returns an error, then, of course, we can show their utility alert and we can just 71 00:05:37,290 --> 00:05:41,990 give it the error dot localized description. 72 00:05:44,790 --> 00:05:46,950 And the view controller, of course, is Seth. 73 00:05:50,560 --> 00:05:57,010 If ever there we go is not equal to no, that means there is an error, then we're going to grab this 74 00:05:57,010 --> 00:05:58,300 localized description. 75 00:05:59,750 --> 00:06:01,350 Which, of course, is give me an error. 76 00:06:02,330 --> 00:06:03,350 Let's see what it is. 77 00:06:08,550 --> 00:06:10,230 Now we can force unwrap it. 78 00:06:11,710 --> 00:06:12,160 Or. 79 00:06:13,970 --> 00:06:16,400 So so let me explain what's just happened here. 80 00:06:16,760 --> 00:06:22,160 It says we can force unwrap the localized description error or we can say test it. 81 00:06:22,460 --> 00:06:27,470 If there is a localized description or if there is an error, then do the following. 82 00:06:28,410 --> 00:06:34,380 Then use that description, rather, if there isn't a localized description, then give me a default 83 00:06:34,380 --> 00:06:36,600 value that I can use. 84 00:06:36,930 --> 00:06:37,500 But you know what? 85 00:06:37,500 --> 00:06:46,440 I'm just going to skip all that because I know there is an error at this point because I've tested it 86 00:06:46,680 --> 00:06:47,280 up here. 87 00:06:48,150 --> 00:06:59,020 So I could say if let E equals error and remove this nail, then down here I could grab the E localized 88 00:06:59,020 --> 00:06:59,720 a description. 89 00:07:00,180 --> 00:07:00,540 Right. 90 00:07:00,560 --> 00:07:02,280 So we're testing if it exists or not. 91 00:07:02,610 --> 00:07:07,290 The other way was effectively the same way of doing it, just with a force onramp. 92 00:07:07,650 --> 00:07:13,350 This way that you're looking at is probably a safer way of doing it, because in the previous one, 93 00:07:13,710 --> 00:07:19,860 when I'm forced unwrapping it, if I change my code in any way, that force unwrapped will probably 94 00:07:19,860 --> 00:07:20,520 stay in. 95 00:07:20,790 --> 00:07:21,990 I won't pick up that. 96 00:07:21,990 --> 00:07:24,880 It's something I need to change and that could lead to trouble down the road. 97 00:07:25,350 --> 00:07:26,910 So this is a safe way of doing it. 98 00:07:29,010 --> 00:07:35,790 OK, and then once we've got that, well, we're just going to return, otherwise we are going to tell 99 00:07:35,790 --> 00:07:39,870 the user, shall we let 100 00:07:43,380 --> 00:07:44,190 success? 101 00:07:55,820 --> 00:07:58,450 So she's going to say, yep, everything is all good. 102 00:07:59,600 --> 00:08:03,140 So now if I play this in our little iPod Touch. 103 00:08:04,690 --> 00:08:06,920 You can see the screen recording in the background there. 104 00:08:09,430 --> 00:08:14,050 It's like a behind the scenes show, this, isn't it, when you see the recordings in the folders and 105 00:08:14,050 --> 00:08:15,330 stuff that I'm preparing here. 106 00:08:15,910 --> 00:08:23,980 So if I hit forgot password email's not valid, if I do some random email that I know doesn't exist. 107 00:08:25,980 --> 00:08:30,660 There's no use a record correspondent to identify the user may have been deleted. 108 00:08:31,110 --> 00:08:32,210 OK, fair enough. 109 00:08:32,610 --> 00:08:36,000 But then of course, that will work if we put in a real email address. 110 00:08:36,470 --> 00:08:41,400 I know it works because I've tested it prior to this, but I won't I'll spare you the boring details 111 00:08:41,400 --> 00:08:41,960 of that. 112 00:08:42,210 --> 00:08:44,050 But by all means, test it out yourself. 113 00:08:44,460 --> 00:08:52,410 We are missing one thing in our code here, though, and that is if the activate UI, that's what we 114 00:08:52,410 --> 00:08:52,740 want. 115 00:08:53,970 --> 00:08:56,250 So if the email is not valid, I don't care about that. 116 00:08:57,330 --> 00:09:05,790 So we want to inactivate the UI and then at the end here we are going to activate the UI. 117 00:09:06,030 --> 00:09:07,380 Now this is going to happen. 118 00:09:15,410 --> 00:09:22,700 There before any error checking, before any success alerts, because we, of course, we need self 119 00:09:26,060 --> 00:09:31,910 and we're doing it before everything, because once we've gone through the network, called to Firebase 120 00:09:32,120 --> 00:09:37,310 or our local checks, then we want the users to actually press buttons from that point because they'll 121 00:09:37,310 --> 00:09:42,140 go after their email, they'll come back with the password and put it in some kind of hit forgot password 122 00:09:42,680 --> 00:09:43,060 email. 123 00:09:43,070 --> 00:09:43,680 Not valid. 124 00:09:44,030 --> 00:09:48,890 And now if I put in a bogus one and I do it, watch the UI. 125 00:09:49,460 --> 00:09:49,990 There we go. 126 00:09:50,570 --> 00:09:53,210 There was a little spinny ball and off we go. 127 00:09:54,580 --> 00:10:01,040 OK, so that is it working perfectly now, he is a bit of homework for you. 128 00:10:01,330 --> 00:10:03,880 You'll notice I can grab the top and pull it down. 129 00:10:04,240 --> 00:10:10,090 And if I do that, I just get a blank screen underneath because obviously it can't connect to anything 130 00:10:10,090 --> 00:10:10,960 if you're not logged in. 131 00:10:11,230 --> 00:10:12,520 So it'll be a useless app. 132 00:10:12,970 --> 00:10:20,140 What I want you to do for your homework or your task here is to figure out using Google or Stack Overflow, 133 00:10:20,470 --> 00:10:24,200 how to stop this view controller from disappearing. 134 00:10:24,640 --> 00:10:29,010 It should amount to one line of code, although it could be more depending on your solution. 135 00:10:29,230 --> 00:10:32,140 So go ahead and do that and I'll give you the solution next time.