1 00:00:00,710 --> 00:00:07,610 Right, so let's look at how we in or actually register all the buttons are set up, all the methods, 2 00:00:07,610 --> 00:00:11,630 fire is just up to us to implement the firebase code. 3 00:00:11,930 --> 00:00:15,710 So to do this, of course, we need, first of all, firebase off. 4 00:00:17,920 --> 00:00:24,130 And then when the in is tapped, we need a way to see what the log in or to pass over those email and 5 00:00:24,130 --> 00:00:25,670 password to Firebase. 6 00:00:26,170 --> 00:00:31,020 Now, the first thing we need to do, of course, is double check that the following is true. 7 00:00:31,750 --> 00:00:36,940 That's not empty and that's not empty, because if they are empty, we shouldn't bother making a call 8 00:00:36,940 --> 00:00:41,590 to Firebase because all these calls add up to fees we might have to pay. 9 00:00:42,190 --> 00:00:49,340 So what we're going to do is make sure we don't make a call unless the email and the password look legit. 10 00:00:50,000 --> 00:00:57,130 OK, so what I'm going to do is create a function down here called check input or validate input or 11 00:00:57,130 --> 00:00:58,240 whatever you want to call it. 12 00:00:58,510 --> 00:01:04,720 And then I'm going to return a bool on this. 13 00:01:04,720 --> 00:01:08,620 So maybe we'll call it the input valid. 14 00:01:08,710 --> 00:01:10,030 That's a more that's a better name. 15 00:01:10,840 --> 00:01:16,780 And then we'll say if input valid and if it's not valid. 16 00:01:16,780 --> 00:01:21,600 So we're going to put an exclamation then for now we're going to return. 17 00:01:21,850 --> 00:01:25,800 Of course, we can also show an error to the user. 18 00:01:26,080 --> 00:01:29,410 We can show the error down here so we don't have to repeat the code, of course. 19 00:01:32,040 --> 00:01:38,220 OK, of course, I've been playing with Colin and things like that a lot, so I always forget to put 20 00:01:38,220 --> 00:01:38,760 these in. 21 00:01:39,030 --> 00:01:41,880 So I'm going to copy that and I'm going to put it in register. 22 00:01:42,780 --> 00:01:47,520 I'm going to forget about, forgot about, because I'm not quite sure how we're going to do that flow 23 00:01:48,000 --> 00:01:49,430 when we get to that point. 24 00:01:50,700 --> 00:01:52,450 So input valid? 25 00:01:52,470 --> 00:02:02,580 Well, first of all, we have to get the email input text and we also need to get the password input 26 00:02:03,000 --> 00:02:04,220 dot text. 27 00:02:04,620 --> 00:02:10,830 Now, these are optional as in the text might not exist, but at least I thought they were optional. 28 00:02:12,180 --> 00:02:13,160 Let's see what happens. 29 00:02:14,140 --> 00:02:16,320 No, I think they're perfectly fine. 30 00:02:16,560 --> 00:02:21,030 So let's have a let email equal and let password equal. 31 00:02:23,830 --> 00:02:30,280 OK, so we have the text from those so we can do a very basic validation here. 32 00:02:32,440 --> 00:02:33,610 See, they are optional. 33 00:02:33,640 --> 00:02:40,090 OK, so if email account, what we're doing is accounting a number of characters is less than, let's 34 00:02:40,090 --> 00:02:47,020 say for an email address, you need at least four characters because it's something at something dot 35 00:02:47,200 --> 00:02:51,520 com, actually, five characters or six, rather. 36 00:02:53,290 --> 00:03:05,260 So if it's that or if pass don't count is less, then I don't know, let's say five characters, then 37 00:03:05,260 --> 00:03:07,150 we're also going to kick the user out. 38 00:03:07,160 --> 00:03:10,230 So we're going to say return false. 39 00:03:11,440 --> 00:03:15,580 And at this point we can actually show the user an error. 40 00:03:16,720 --> 00:03:19,280 OK, so this email is giving us errors. 41 00:03:19,300 --> 00:03:25,030 So what I'm going to do is just put an exclamation after these options because I know that the text 42 00:03:25,030 --> 00:03:25,600 exists. 43 00:03:25,870 --> 00:03:29,580 Even if there is nothing in the box, the text itself will exist. 44 00:03:29,890 --> 00:03:36,160 So this is one of those rare cases where you can actually force unwrap the optional again, if you don't 45 00:03:36,160 --> 00:03:37,480 remember what options are they? 46 00:03:37,480 --> 00:03:42,180 Just a box that might contain the type of value you specify or might contain nothing. 47 00:03:42,430 --> 00:03:46,240 Go back to my old courses on Swift, my previous courses, I should say. 48 00:03:46,510 --> 00:03:48,820 They will explain all of that stuff for you. 49 00:03:49,270 --> 00:03:51,310 Otherwise we are going to return. 50 00:03:51,700 --> 00:03:52,150 True. 51 00:03:54,320 --> 00:04:00,770 OK, so that's the validation of the input sorted later on, perhaps we can well, why? 52 00:04:00,770 --> 00:04:01,520 Why later on? 53 00:04:01,910 --> 00:04:05,450 Let us create a new file and let's do it now. 54 00:04:05,840 --> 00:04:06,620 Swift file. 55 00:04:09,000 --> 00:04:17,040 Utilities, my favorite kind of thing, so let's import the UI kit. 56 00:04:19,360 --> 00:04:21,010 And call this what it should be. 57 00:04:25,440 --> 00:04:32,250 Don't need to inherit anything, and then we have some code, so function show alert. 58 00:04:34,160 --> 00:04:40,860 Now, let me see how I've done this in my previous project, because there's no point in dragging you 59 00:04:40,860 --> 00:04:41,700 guys through this. 60 00:04:42,060 --> 00:04:47,150 OK, I'm trying to copy this and I'll talk you through why it's doing what it's doing there. 61 00:04:48,600 --> 00:04:53,730 So to show the alert, we need to have access to The View controller because the View controller contains 62 00:04:53,730 --> 00:04:59,310 the navigation controller that will allow us to display the alert, as in we need a reference to The 63 00:04:59,310 --> 00:04:59,880 View. 64 00:05:00,240 --> 00:05:02,010 OK, so that's what we're pulling in there. 65 00:05:03,300 --> 00:05:08,670 If you didn't do that, there are other ways of architecting this where you could have your utility 66 00:05:08,670 --> 00:05:12,810 class access to The View, but we're not going to do anything complex here. 67 00:05:13,530 --> 00:05:14,730 Then we have an alert. 68 00:05:15,450 --> 00:05:22,260 We have an action which is OK, and we're just going to pass over the title and message. 69 00:05:22,270 --> 00:05:23,190 So this is generic. 70 00:05:24,330 --> 00:05:27,740 Finally, we're going to present that particular view controller. 71 00:05:27,750 --> 00:05:30,300 Feel free to pause and copy this code if you need to. 72 00:05:31,470 --> 00:05:33,920 OK, so let's go back to our orfeu controller. 73 00:05:34,830 --> 00:05:38,490 So if that's wrong, we're going to say utilities. 74 00:05:40,380 --> 00:05:41,010 Utilities. 75 00:05:41,760 --> 00:05:52,560 Utilities don't show alert, and what's the title? 76 00:05:55,500 --> 00:05:59,970 I'm about the title in this case, of course, is an error. 77 00:06:03,350 --> 00:06:11,900 The message is email or password to short. 78 00:06:16,110 --> 00:06:21,870 That's a good enough error, and they're going to pass over the View controller itself, so I don't 79 00:06:21,870 --> 00:06:25,380 know why it's not coming up with the code completion for us here. 80 00:06:25,380 --> 00:06:28,470 But I was just doing what it's doing, I guess. 81 00:06:29,250 --> 00:06:30,750 OK, so we can test that out. 82 00:06:31,120 --> 00:06:32,820 We can go over to our iPod Touch. 83 00:06:33,180 --> 00:06:39,570 And if we put in those values that are too short, then we should see that Arabic's pop up. 84 00:06:43,760 --> 00:06:45,580 So I'm just going to put a little something there. 85 00:06:48,390 --> 00:06:54,480 And perhaps they're going to get rid of the keyboard and then I'm going to hit it with Log-in, wasn't 86 00:06:54,480 --> 00:06:54,660 it? 87 00:06:54,990 --> 00:06:57,300 They go email a password too short. 88 00:06:57,300 --> 00:06:58,800 Of course, register does nothing. 89 00:06:58,860 --> 00:06:59,480 Oh, no. 90 00:06:59,490 --> 00:07:00,840 We set it up in register. 91 00:07:00,840 --> 00:07:01,950 So it does do something. 92 00:07:02,610 --> 00:07:05,090 Login to short, OK. 93 00:07:05,280 --> 00:07:07,260 And now if it goes longer 94 00:07:09,960 --> 00:07:14,590 and we do it, we get nothing happening because we've just fallen through that filter. 95 00:07:15,030 --> 00:07:20,040 Now, obviously, in a production app, you might want to give the user just a little bit of information. 96 00:07:20,040 --> 00:07:26,160 So you'd split out this check and this check to let them know exactly what they were doing wrong. 97 00:07:26,160 --> 00:07:31,950 So say to them, look, you need a longer password, you need a correctly formatted email, and so do 98 00:07:31,950 --> 00:07:32,510 the email. 99 00:07:32,520 --> 00:07:37,830 You do something called regular expressions, which are a nightmare, but it's something you have to 100 00:07:37,830 --> 00:07:38,220 do. 101 00:07:38,220 --> 00:07:43,500 And you can find regex on the Internet pretty easily for what you need, especially stuff like email 102 00:07:43,500 --> 00:07:44,040 addresses. 103 00:07:44,490 --> 00:07:46,160 So anyway, we're going to leave that one there.