1 00:00:01,050 --> 00:00:07,020 Continue with our authentication wire up we need to also make sure that when we log in a user if the 2 00:00:07,020 --> 00:00:12,930 initial requests to authenticate them fails we want to make sure that we also attempt to create an account 3 00:00:12,930 --> 00:00:14,090 for the user as well. 4 00:00:14,310 --> 00:00:18,030 So we're going to follow the same methodology as in our authentication app. 5 00:00:18,210 --> 00:00:23,380 If an email and password combination fails we assume that the user needs a new account. 6 00:00:23,400 --> 00:00:26,940 So we will attempt to create the new account. 7 00:00:27,840 --> 00:00:34,100 So our start is going to be the same as the the we handled authentication previously. 8 00:00:34,350 --> 00:00:39,450 If we hit that then case that means that the user successfully logged in if the authentication with 9 00:00:39,450 --> 00:00:40,980 email and password fails. 10 00:00:41,010 --> 00:00:43,330 That means we're going hit a catch case. 11 00:00:43,410 --> 00:00:48,630 So we had a catch case in here and whenever we had this catch case we're going to do a couple of different 12 00:00:48,630 --> 00:00:50,130 things. 13 00:00:50,130 --> 00:01:00,630 First off we're going to call firebase dot off God creates user with e-mail and password and it'll pass 14 00:01:00,630 --> 00:01:02,870 in the email and password that were entered. 15 00:01:05,690 --> 00:01:11,230 And then after that we'll get back a user model and inside of there. 16 00:01:11,270 --> 00:01:14,730 We will also dispatch the logging user success action. 17 00:01:14,810 --> 00:01:21,190 So type log in user success with a payload of user. 18 00:01:21,190 --> 00:01:22,080 All right. 19 00:01:22,100 --> 00:01:24,090 Now I want to point out to you. 20 00:01:24,540 --> 00:01:28,440 She has this is some really nasty looking code here. 21 00:01:28,470 --> 00:01:33,860 We're only really doing you know two things more or less like sign in the user with the email and password. 22 00:01:33,860 --> 00:01:36,360 Failing that try to create a new account. 23 00:01:36,440 --> 00:01:39,530 But this is one nasty block of code right here. 24 00:01:39,620 --> 00:01:43,520 And I would not be surprised if I have a hidden typo somewhere in here. 25 00:01:43,520 --> 00:01:46,890 This is where yes Lent is definitely very helpful. 26 00:01:46,970 --> 00:01:50,880 So clearly very nasty that we're working with here. 27 00:01:50,990 --> 00:01:53,880 This is the absolute downside of redux. 28 00:01:53,990 --> 00:01:59,720 So it can really add to or really contribute to some kind of Pyramid's of doom as you see like this 29 00:01:59,720 --> 00:02:00,260 right here. 30 00:02:00,260 --> 00:02:06,680 I mean we're using promises which cleans up things a little bit but still at it's kind of nasty. 31 00:02:06,920 --> 00:02:12,560 The best solution that we can come up to handle this is to make a couple of helper functions for processing 32 00:02:12,560 --> 00:02:17,360 the callbacks that we have in here specifically the success cases. 33 00:02:17,510 --> 00:02:24,560 So you can see that if we successfully sign in with email and password or if we successfully create 34 00:02:24,560 --> 00:02:29,060 a user in both cases we are dispatching an identical action. 35 00:02:29,270 --> 00:02:34,240 So I would suggest that we create a new function called log in user success. 36 00:02:34,460 --> 00:02:38,810 It will take the dispatch method and automatically dispatch our success. 37 00:02:38,810 --> 00:02:40,280 Action for us. 38 00:02:40,280 --> 00:02:45,290 So essentially we're going to make a helper method to clean up this log in user action creator. 39 00:02:45,320 --> 00:02:47,640 Let's see what it looks like. 40 00:02:47,720 --> 00:02:53,000 You'll notice that I'm not going to export this new method because I really do not intend to call it 41 00:02:53,000 --> 00:02:55,620 directly from anywhere else inside my application. 42 00:02:55,700 --> 00:03:03,250 It's really just for use to support the log in user action creator well-define log in users success. 43 00:03:03,310 --> 00:03:08,220 It will be called directly with a dispatch method and the user that just signed in. 44 00:03:08,960 --> 00:03:14,180 And then inside of here we will dispatch our log in user success action. 45 00:03:14,180 --> 00:03:24,150 So all of a type of log in users success in a tight or excuse me a payload of the user. 46 00:03:24,180 --> 00:03:29,460 So now we can clean up our code just a little bit inside of the logon user action creator. 47 00:03:29,460 --> 00:03:36,900 First off I'm going to delete both the dispatch statements so delete dispatch number one and then delete 48 00:03:36,930 --> 00:03:38,540 dispatch number two. 49 00:03:38,700 --> 00:03:40,740 Now I've got two vans. 50 00:03:40,980 --> 00:03:47,240 Each of them just have kind of dangling federal function with the user as an argument. 51 00:03:47,490 --> 00:03:53,730 Then inside of both these I would call log in users success and I need to pass along the dispatch method 52 00:03:53,760 --> 00:03:58,620 so that it can actually do a dispatch and also the user. 53 00:03:58,980 --> 00:04:04,420 So log in user success with the user. 54 00:04:04,560 --> 00:04:10,320 So again one more time just because I want to make sure that we get the right code on the screen here 55 00:04:10,650 --> 00:04:16,630 I have two events both of them are identical and they should both take a user and call log and use their 56 00:04:16,630 --> 00:04:20,380 success with dispatch and the user. 57 00:04:20,380 --> 00:04:25,380 So this is a little bit better but definitely two more things here we need to make sure that we bubble 58 00:04:25,380 --> 00:04:30,420 up an error message to handle the case in which the user just like puts in the wrong credentials. 59 00:04:30,420 --> 00:04:35,830 And we also need to make sure that we send across an action when the action creator is first called. 60 00:04:35,940 --> 00:04:38,270 So we noticed start up the spinner right. 61 00:04:38,280 --> 00:04:44,320 We need to make sure that we notice start up the spinner when the action crater's first called So let's 62 00:04:44,320 --> 00:04:46,970 start with the handling the fail case first. 63 00:04:46,990 --> 00:04:51,670 And I think that we should also put the fey handling the fail inside of a helper method as well because 64 00:04:51,670 --> 00:04:54,250 it's really going to clean up the file a little bit. 65 00:04:54,370 --> 00:05:01,910 So I'm going to make another helper method helper function called log in user fail. 66 00:05:01,980 --> 00:05:10,710 This will be called with the dispatch method and all it has to do is dispatch an action of type log 67 00:05:10,710 --> 00:05:12,470 in user fail. 68 00:05:12,810 --> 00:05:14,360 Now we don't have that type yet. 69 00:05:14,360 --> 00:05:16,410 So let's go create it. 70 00:05:16,410 --> 00:05:24,210 I'm going to go up to the top of this file and I'm going to import log in user fail at the top. 71 00:05:24,300 --> 00:05:27,570 I wanted to make sure that our type file has that as well. 72 00:05:27,840 --> 00:05:35,190 So I'm going to find my type's file inside the actions directory and I'll add an export Konst log in 73 00:05:35,280 --> 00:05:36,220 user fail 74 00:05:40,910 --> 00:05:41,260 OK. 75 00:05:41,290 --> 00:05:46,960 So everything that we're doing this section again it's all about making this log in user action creator. 76 00:05:47,050 --> 00:05:51,570 Much more robust to handle the spin or handle the error messages handle is success. 77 00:05:51,580 --> 00:05:59,250 So we have now added in code to handle success of signing with the email and password. 78 00:05:59,380 --> 00:06:05,770 Success of creating a new account both those were handled by the log in users access method and then 79 00:06:05,770 --> 00:06:11,660 we also just created log in user fail which dispatches an action of type log and user failed. 80 00:06:11,860 --> 00:06:18,520 So we will definitely want to make sure that we eventually hop over to our reducer as well and pick 81 00:06:18,520 --> 00:06:22,830 up on this you know pick up on the log in user fail message right here. 82 00:06:22,870 --> 00:06:24,620 So doesn't want to take that as well. 83 00:06:24,620 --> 00:06:29,050 The last thing we have to do is make sure that we actually call log in use or fail at some point. 84 00:06:29,140 --> 00:06:35,620 So we're only going to fail the user out of this flow after we have both tried to sign with email and 85 00:06:35,620 --> 00:06:39,270 password and create a new user with the email and password. 86 00:06:39,550 --> 00:06:45,780 So after we attempt to create a user with the email and password I'm going to add on a catch case notice 87 00:06:45,780 --> 00:06:50,790 that I'm going to take off the semi-clean on the van here and take it off. 88 00:06:51,640 --> 00:07:02,410 And if that fails I want to catch and directly call log in user fail with the dispatch method and then 89 00:07:02,410 --> 00:07:05,980 nothing will be responsible for dispatching the fail type. 90 00:07:06,440 --> 00:07:06,700 OK. 91 00:07:06,720 --> 00:07:09,860 So again a really nasty chunk of code right here. 92 00:07:09,880 --> 00:07:14,420 This is definitely where redux thung starts to get you know a little bit painful. 93 00:07:14,680 --> 00:07:20,080 Let's continue on the next section and make sure that we are handling starting a spinner showing error 94 00:07:20,080 --> 00:07:23,050 messages and all that kind of good stuff inside of our producer