1 00:00:01,200 --> 00:00:04,800 All right my friends last thing I do inside of our sign and screen is make sure that we hook this thing 2 00:00:04,800 --> 00:00:06,680 up to our off context. 3 00:00:06,960 --> 00:00:11,520 So to get started at the very top of the file I'll find my import from react and I'm going to make sure 4 00:00:11,520 --> 00:00:13,570 that I get use context as well. 5 00:00:14,790 --> 00:00:25,620 Then I'll also add in an import or my context object from up one directory context off context and then 6 00:00:25,620 --> 00:00:36,500 inside of sine in screen I'm going to get my state object and my sign in action function not from use 7 00:00:36,560 --> 00:00:39,260 context and I'll pass in my context like so 8 00:00:43,310 --> 00:00:43,640 OK. 9 00:00:43,650 --> 00:00:47,790 Now we've got that state object we can take the error message property off of it and throw it into our 10 00:00:47,850 --> 00:00:52,260 art form just as we did inside of our sign up screen right. 11 00:00:52,260 --> 00:00:59,520 You're going to put in my red curly braces and I'll say state DOT error message likewise we can take 12 00:00:59,520 --> 00:01:04,890 that action function of sign in and rather than passing in a ERO function in empty or a function for 13 00:01:04,890 --> 00:01:11,310 on summit we can pass in sign in instead remember on submit is going to call that function automatically 14 00:01:11,310 --> 00:01:16,390 with an object that has the email and password that the user entered so that should pretty much be it 15 00:01:16,390 --> 00:01:18,030 for our sign and screen. 16 00:01:18,200 --> 00:01:22,860 So let's save this and we can start to flip back over to our off context. 17 00:01:23,140 --> 00:01:29,260 And the last thing we do is add in some additional code to sign in and go through this sign and process. 18 00:01:29,260 --> 00:01:33,070 So we want to make sure that we essentially do the same exact stuff that we we did it inside of sign 19 00:01:33,130 --> 00:01:34,030 up. 20 00:01:34,090 --> 00:01:41,200 So we want to make sure that we put in a try catch in make our request to sign in store the token dispatch 21 00:01:41,200 --> 00:01:46,480 and action to update our state navigate over to a track list and then handle an error as well. 22 00:01:46,990 --> 00:01:50,170 So let's try to get through some of these steps right away because I think we've already got a pretty 23 00:01:50,170 --> 00:01:56,780 good idea of what we have to do going to delete the comments inside of sign in and I'll replace it with 24 00:01:56,780 --> 00:02:00,900 a try catch. 25 00:02:00,940 --> 00:02:05,990 So first off let's handle the error case just in case something goes wrong with our request. 26 00:02:06,010 --> 00:02:09,910 So inside if you're all dispatch and action if something goes wrong with our request or the entire sign 27 00:02:09,910 --> 00:02:19,240 in process so give it a type of ad error and a payload of something went wrong with sign in 28 00:02:22,640 --> 00:02:24,980 guess that's all we have do for our air handling case. 29 00:02:24,980 --> 00:02:30,920 So now we can work on the actual sign in process before I do so I'm just going to refactor this aero 30 00:02:30,920 --> 00:02:31,810 function right here. 31 00:02:31,860 --> 00:02:36,800 I'm going to take out that return statement just as we discussed back up here with sign up we'll take 32 00:02:36,800 --> 00:02:42,620 out the return statement I'm going to take out that opening curly brace right there and the closing 33 00:02:42,620 --> 00:02:44,230 curly brace down there. 34 00:02:44,270 --> 00:02:51,120 Just the one and then I'm going to unwind dent or dent all that stuff and then put that function declaration 35 00:02:51,120 --> 00:02:52,330 on the same line. 36 00:02:52,380 --> 00:02:56,340 So again what we have right here is 100 percent equivalent to what we had just a moment ago just some 37 00:02:56,340 --> 00:02:59,000 slightly compacted syntax. 38 00:02:59,240 --> 00:02:59,450 All right. 39 00:02:59,450 --> 00:03:04,470 So inside of our try we can't tend to make our request with axes to sign our user in. 40 00:03:04,790 --> 00:03:09,680 So I'll say con's response is a weight tracker API. 41 00:03:09,680 --> 00:03:16,450 And then remember to sign a user in with our API we make a post request to slash sign in and we have 42 00:03:16,450 --> 00:03:21,330 to provide the email and password that the user wants to sign in with. 43 00:03:21,660 --> 00:03:27,470 As usual Notice I'm using the abbreviated syntax here for creating that object. 44 00:03:27,490 --> 00:03:33,100 Now when we get the response we're gonna do the exact same response we had inside of sign up so we want 45 00:03:33,100 --> 00:03:37,090 to make sure that we take the token we get back and story inside of async storage. 46 00:03:37,300 --> 00:03:43,620 So if a user once again ever restarts their device we can sign the user back in automatically so once 47 00:03:43,620 --> 00:03:51,210 we get that token back we'll call a wait async storage dot set item and we're going to set the same 48 00:03:51,210 --> 00:03:54,130 key as we did up here inside of sign up. 49 00:03:54,510 --> 00:04:01,950 So we're gonna set a key of token once again and then the actual value will be response dot data dot 50 00:04:01,950 --> 00:04:04,900 token. 51 00:04:04,910 --> 00:04:06,350 Now I'm using a wait right here. 52 00:04:06,350 --> 00:04:11,180 So rather than running into the same error message we saw last time to make sure I get the async keyword 53 00:04:11,240 --> 00:04:14,810 on that inner function right there right away. 54 00:04:14,830 --> 00:04:25,310 Next up add in our dispatch so I'll put in a dispatch of type sign in with a payload of response dot 55 00:04:25,310 --> 00:04:27,110 data dot token 56 00:04:31,210 --> 00:04:35,200 and then right after that we'll make sure that we navigate after we successfully sign in and go through 57 00:04:35,200 --> 00:04:38,360 all that work over to our track list. 58 00:04:38,380 --> 00:04:40,460 Once again. 59 00:04:40,520 --> 00:04:46,770 Now last thing we do is just make sure we add in a case to handle sign in so up inside of our dispatch 60 00:04:46,830 --> 00:04:54,580 where our reduced right here that gets called by dispatch we can add in a case of sign in and I will 61 00:04:54,580 --> 00:04:55,730 return. 62 00:04:55,750 --> 00:04:59,830 Well guess what it actually ends up their work basically going to return the same state as what we did 63 00:04:59,830 --> 00:05:00,250 up here. 64 00:05:00,910 --> 00:05:05,090 So do we really need to separate cases one for sign up and sign in. 65 00:05:05,110 --> 00:05:10,330 I think that realistically we can really just condense this down to one single case so rather than putting 66 00:05:10,330 --> 00:05:16,150 in two essentially identical cases I'm just going to rename the existing one to sign in. 67 00:05:16,150 --> 00:05:18,330 Because that's what you're really describing here. 68 00:05:18,400 --> 00:05:22,990 There's not really anything to distinguish between signing up and signing in in both cases we get a 69 00:05:22,990 --> 00:05:26,880 token and we store that token inside of our state object. 70 00:05:26,920 --> 00:05:31,470 So I'm just going to call both them sign in because that's really what is happening. 71 00:05:31,620 --> 00:05:36,370 I'm going to make sure that inside of my sign up action so your sign up rather than dispatching a action 72 00:05:36,370 --> 00:05:39,450 of type sign up I mean do sign in instead. 73 00:05:39,480 --> 00:05:43,860 Now let's make sure that I've got the exact same one down here inside of my sign in action as well. 74 00:05:43,860 --> 00:05:47,480 Yep definitely do okay. 75 00:05:47,520 --> 00:05:49,170 So I think this looks pretty good. 76 00:05:49,170 --> 00:05:50,350 So let's do a quick save. 77 00:05:50,370 --> 00:05:56,170 I can make sure I save everything inside of all these different files and we'll do a quick test so in 78 00:05:56,170 --> 00:05:59,600 this case we want to make sure that we sign into an existing account. 79 00:05:59,800 --> 00:06:03,250 If you've forgotten some of the different emails that you've used to create an account remember you 80 00:06:03,250 --> 00:06:07,510 can always hop back over to that Mongo console back over here. 81 00:06:07,510 --> 00:06:09,370 So here's my Mongo cluster. 82 00:06:09,370 --> 00:06:16,210 We go to our cluster we select the cluster we then hit collections and then we'll see our list of users 83 00:06:16,210 --> 00:06:18,130 if we tap on users right there. 84 00:06:18,130 --> 00:06:23,110 So we can see all the different user accounts we've created so we can attempt to sign in with any of 85 00:06:23,110 --> 00:06:24,570 these different emails. 86 00:06:24,670 --> 00:06:31,950 So we're going to try to sign in as the user with email of test one at test dot com so I'll do on my 87 00:06:32,010 --> 00:06:33,040 sign in screen. 88 00:06:33,060 --> 00:06:41,670 I'm going to go over to sign in and I'll do test one at test dot com and my password a password. 89 00:06:41,670 --> 00:06:46,580 I'll sign in and sure enough something went wrong so I'm sure I probably made another small typo. 90 00:06:46,590 --> 00:06:48,480 Hopefully it wasn't as bad as my last one. 91 00:06:48,480 --> 00:06:53,210 I'll check my terminal really quickly we'll do some live debugging this time around. 92 00:06:53,360 --> 00:06:55,540 So looks like nothing quite happened here. 93 00:06:56,760 --> 00:06:58,690 So a good way to catch this. 94 00:06:58,740 --> 00:07:02,390 Remember we inside of our sign in right here we've got our try catch. 95 00:07:02,430 --> 00:07:06,780 So right now if anything goes wrong inside of these statements right here we essentially get thrown 96 00:07:06,780 --> 00:07:11,820 down into the catch block since we're catching that air we didn't see any air appear inside of our console 97 00:07:11,940 --> 00:07:17,240 because that I'm very certain that something must be going wrong with that segment right there. 98 00:07:17,290 --> 00:07:21,960 So I got to put in a quick console log right here and a console log out the air that occurred during 99 00:07:21,960 --> 00:07:24,870 those lines and I'll try to sign in once more 100 00:07:28,690 --> 00:07:29,280 sign in. 101 00:07:29,380 --> 00:07:33,350 And now I should see a console describing exactly what occurred. 102 00:07:33,350 --> 00:07:38,470 So I'll flip back over and I can see that I got a status code of 22 which means that I'm definitely 103 00:07:38,470 --> 00:07:41,800 not sending across my email and password correctly. 104 00:07:41,800 --> 00:07:43,510 So what's going on here. 105 00:07:43,510 --> 00:07:46,850 Well I think unfortunately I'm going to do a little bit of debugging once again. 106 00:07:46,870 --> 00:07:47,860 I can't believe this. 107 00:07:47,860 --> 00:07:48,450 The same thing. 108 00:07:48,460 --> 00:07:52,000 Both times testing out sign up and sign and ran into an error both times. 109 00:07:52,000 --> 00:07:55,400 So once again I'm going to do a little bit of troubleshooting here figure out what's going on. 110 00:07:55,630 --> 00:07:56,410 Let's come back and less. 111 00:07:56,410 --> 00:07:59,770 Next video and figure out exactly what did not quite work. 112 00:07:59,880 --> 00:08:01,630 A quick pause and I'll see you in just a minute.